2 New | Code Avengers Answers Python
Tuples are ordered, immutable collections of items.
# create a tuple
my_tuple = (1, 2, 3, 4, 5)
# access elements
print my_tuple[0] # prints 1
# trying to modify a tuple will result in an error
try:
my_tuple[0] = 10
except TypeError:
print "Tuples are immutable"
The biggest change in the new Python 2 course is the introduction of range() with three arguments and nested loops. code avengers answers python 2 new
Prompt:
“Print the multiplication table for numbers 1 through 5, from 1 to 10. Use nested for loops. Output should be formatted as '2 x 3 = 6'.” Tuples are ordered, immutable collections of items
Answer:
for i in range(1, 6):
for j in range(1, 11):
print(f"i x j = i*j")
print("---") # Separator between tables
Common mistake: Forgetting the separator line or using break incorrectly. The biggest change in the new Python 2