curriculum/challenges/english/blocks/review-loops-and-sequences/67f39b91583dec1e1eddbb9e.md
cities = ['Los Angeles', 'London', 'Tokyo']
cities list, you can reference its index number in the sequence:cities = ['Los Angeles', 'London', 'Tokyo']
cities[0] # Los Angeles
-1 as the index number:cities = ['Los Angeles', 'London', 'Tokyo']
cities[-1] # Tokyo
Negative indexing is used to access elements starting from the end of the list instead of the beginning at index 0.
Creating Lists Using the list() constructor: Lists can also be created using the list() constructor. The list() constructor is used to convert an iterable into a list:
developer = 'Jessica'
print(list(developer))
# Result: ['J', 'e', 's', 's', 'i', 'c', 'a']
len() function to get the length of a list:numbers = [1, 2, 3, 4, 5]
len(numbers) # 5
programming_languages = ['Python', 'Java', 'C++', 'Rust']
programming_languages[0] = 'JavaScript'
print(programming_languages) # ['JavaScript', 'Java', 'C++', 'Rust']
IndexError:programming_languages = ['Python', 'Java', 'C++', 'Rust']
programming_languages[10] = 'JavaScript'
"""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
"""
del keyword:developer = ['Jane Doe', 23, 'Python Developer']
del developer[1]
print(developer) # ['Jane Doe', 'Python Developer']
in keyword can be used to check if an element exists in a list:programming_languages = ['Python', 'Java', 'C++', 'Rust']
'Rust' in programming_languages # True
'JavaScript' in programming_languages # False
developer = ['Alice', 25, ['Python', 'Rust', 'C++']]
2 since lists are zero-based indexed.developer = ['Alice', 25, ['Python', 'Rust', 'C++']]
developer[2] # ['Python', 'Rust', 'C++']
1:developer = ['Alice', 25, ['Python', 'Rust', 'C++']]
developer[2][1] # Rust
developer list into new variables called name, age and job like this:developer = ['Alice', 34, 'Rust Developer']
name, age, job = developer
If the number of variables on the left side of the assignment operator doesn't match the total number of items in the list, then you will receive a ValueError.
Collecting Remaining Items From a List: To collect any remaining elements from a list, you can use the asterisk (*) operator like this:
developer = ['Alice', 34, 'Rust Developer']
name, *rest = developer
:. To slice a list that starts at index 1 and ends before index 3, you can use the following syntax:desserts = ['Cake', 'Cookies', 'Ice Cream', 'Pie']
desserts[1:3] # ['Cookies', 'Ice Cream']
numbers = [1, 2, 3, 4, 5, 6]
numbers[1::2] # [2, 4, 6]
append() method to add the number 6 to this numbers list:numbers = [1, 2, 3, 4, 5]
numbers.append(6)
print(numbers) # [1, 2, 3, 4, 5, 6]
append() method can also be used to add one list at the end of another:numbers = [1, 2, 3, 4, 5]
even_numbers = [6, 8, 10]
numbers.append(even_numbers)
print(numbers) # [1, 2, 3, 4, 5, [6, 8, 10]]
6, 8, and 10 to the end of the numbers list:numbers = [1, 2, 3, 4, 5]
even_numbers = [6, 8, 10]
numbers.extend(even_numbers)
print(numbers) # [1, 2, 3, 4, 5, 6, 8, 10]
insert() method:numbers = [1, 2, 3, 4, 5]
numbers.insert(2, 2.5)
print(numbers) # [1, 2, 2.5, 3, 4, 5]
remove() method will only remove the first occurrence of an item in the list:numbers = [1, 2, 3, 4, 5, 5, 5]
numbers.remove(5)
print(numbers) # [1, 2, 3, 4, 5, 5]
numbers = [1, 2, 3, 4, 5]
numbers.pop(1) # The number 2 is returned
pop method, then the last element is removed.numbers = [1, 2, 3, 4, 5]
numbers.pop() # The number 5 is returned
numbers = [1, 2, 3, 4, 5]
numbers.clear()
print(numbers) # []
sort() method is used to sort the elements in place. Here is an example of sorting a random list of numbers in place:numbers = [19, 2, 35, 1, 67, 41]
numbers.sort()
print(numbers) # [1, 2, 19, 35, 41, 67]
numbers = [19, 2, 35, 1, 67, 41]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # [1, 2, 19, 35, 41, 67]
print(numbers) # [19, 2, 35, 1, 67, 41]
numbers = [6, 5, 4, 3, 2, 1]
numbers.reverse()
print(numbers) # [1, 2, 3, 4, 5, 6]
programming_languages = ['Rust', 'Java', 'Python', 'C++']
programming_languages.index('Java') # 1
index() method, then the result will be a ValueError.developer = ('Alice', 34, 'Rust Developer')
TypeError:programming_languages = ('Python', 'Java', 'C++', 'Rust')
programming_languages[0] = 'JavaScript'
"""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: "tuple" object does not support item assignment
"""
developer = ('Alice', 34, 'Rust Developer')
developer[1] # 34
numbers = (1, 2, 3, 4, 5)
numbers[-2] # 4
IndexError:numbers = (1, 2, 3, 4, 5)
numbers[7]
"""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
"""
tuple() constructor. Within the constructor, you can pass in different iterables like strings, lists and even other tuples.developer = 'Jessica'
print(tuple(developer))
# Result: ('J', 'e', 's', 's', 'i', 'c', 'a')
in keyword like this:programming_languages = ('Python', 'Java', 'C++', 'Rust')
'Rust' in programming_languages # True
'JavaScript' in programming_languages # False
developer = ('Alice', 34, 'Rust Developer')
name, age, job = developer
*) operator like this:developer = ('Alice', 34, 'Rust Developer')
name, *rest = developer
pie and cookies can be sliced into a separate tuple:desserts = ('cake', 'pie', 'cookies', 'ice cream')
desserts[1:3] # ('pie', 'cookies')
TypeError as tuples are immutable:developer = ('Jane Doe', 23, 'Python Developer')
del developer[1]
"""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: "tuple" object doesn't support item deletion
"""
count(): Used to determine how many times an item appears in a tuple. For example, you can check how many times the language 'Rust' appears in the tuple:programming_languages = ('Rust', 'Java', 'Python', 'C++', 'Rust')
programming_languages.count('Rust') # 2
count() method is not present at all in the tuple, then the return value will be 0:programming_languages = ('Rust', 'Java', 'Python', 'C++', 'Rust')
programming_languages.count('JavaScript') # 0
If no arguments are passed to the count() method, then Python will return a TypeError.
index(): Used to find the index where a particular item is present in the tuple. Here is an example of using the index() method to find the index for the language 'Java':
programming_languages = ('Rust', 'Java', 'Python', 'C++', 'Rust')
programming_languages.index('Java') # 1
If the specified item cannot be found, then Python will return a ValueError.
You can pass an optional start index to the index() method to specify where to start searching for the item in the tuple:
programming_languages = ('Rust', 'Java', 'Python', 'C++', 'Rust', 'Python')
programming_languages.index('Python', 3) # 5
index() method to specify where to stop searching for the item in the tuple:programming_languages = ('Rust', 'Java', 'Python', 'C++', 'Rust', 'Python', 'JavaScript', 'Python')
programming_languages.index('Python', 2, 5) # 2
sorted(): Used to sort the elements in any iterable and return a new sorted list. Here is an example of creating a new list of numbers using the sorted() function:numbers = (13, 2, 78, 3, 45, 67, 18, 7)
sorted(numbers) # [2, 3, 7, 13, 18, 45, 67, 78]
reverse and key arguments. Here is an example of using the key argument to sort items in a tuple by length:programming_languages = ('Rust', 'Java', 'Python', 'C++', 'Rust', 'Python')
sorted(programming_languages, key=len)
# Result
# ['C++', 'Rust', 'Java', 'Rust', 'Python', 'Python']
reverse argument like this:programming_languages = ('Rust', 'Java', 'Python', 'C++', 'Rust', 'Python')
print(sorted(programming_languages, reverse=True))
# Result
# ['Rust', 'Rust', 'Python', 'Python', 'Java', 'C++']
Definition: Loops are used to repeat a block of code for a set number of times.
for loop: Used to iterate over a sequence (like a list, tuple or string) and execute a block of code for each item in that sequence. Here is an example of using a for loop to iterate through a list and print each language to the console:
programming_languages = ['Rust', 'Java', 'Python', 'C++']
for language in programming_languages:
print(language)
"""
Result
Rust
Java
Python
C++
"""
for loop to loop through the string code and print out each character:for char in 'code':
print(char)
"""
Result
c
o
d
e
"""
for loops can be nested. Here is an example of using a nested for loop:categories = ['Fruit', 'Vegetable']
foods = ['Apple', 'Carrot', 'Banana']
for category in categories:
for food in foods:
print(category, food)
"""
Result
Fruit Apple
Fruit Carrot
Fruit Banana
Vegetable Apple
Vegetable Carrot
Vegetable Banana
"""
while loop: Repeats a block of code until the condition is False. Here is an example of using a while loop for a guessing game:secret_number = 3
guess = 0
while guess != secret_number:
guess = int(input('Guess the number (1-5): '))
if guess != secret_number:
print('Wrong! Try again.')
print('You got it!')
"""
Result
Guess the number (1-5): 2
Wrong! Try again.
Guess the number (1-5): 1
Wrong! Try again.
Guess the number (1-5): 3
You got it!
"""
break and continue statements: Used in loops to modify the execution of a loop.
The break statement is used to exit the loop immediately when a certain condition is met. Here is an example of using the break statement for a list of developer_names:
developer_names = ['Jess', 'Naomi', 'Tom']
for developer in developer_names:
if developer == 'Naomi':
break
print(developer)
continue statement is used to skip that current iteration and move onto the next iteration of the loop. Here is an example to use the continue statement instead of a break statement:developer_names = ['Jess', 'Naomi', 'Tom']
for developer in developer_names:
if developer == 'Naomi':
continue
print(developer)
for and while loops can be combined with an else clause, which is executed only when the loop was not terminated by a break:words = ['sky', 'apple', 'rhythm', 'fly', 'orange']
for word in words:
for letter in word:
if letter.lower() in 'aeiou':
print(f"'{word}' contains the vowel '{letter}'")
break
else:
print(f"'{word}' has no vowels")
range() function: Used to generate a sequence of integers.range(start, stop, step)
stop argument is an integer(non-inclusive) that represents the end point for the sequence of numbers being generated. Here is an example of using the range() function:for num in range(3):
print(num)
start argument is not specified, then the default will be 0. By default the sequence of integers will increment by 1. You can use the optional step argument to change the default increment value. Here is an example of generating a sequence of even integers from 2 up to but not including 11 (i.e., includes 10)for num in range(2, 11, 2):
print(num)
If you don't provide any arguments to the range() function, then you will get a TypeError.
The range() function only accepts integers for arguments and not floats. Using floats will also result in a TypeError:
ERROR!
Traceback (most recent call last):
File "<main.py>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
step argument to generate a sequence of integers in decrementing order:for num in range(40, 0, -10):
print(num)
range() function can also be used to create a list of integers by using it with the list constructor. The list constructor is used to convert an iterable into a list. Here is an example of generating a list of even integers between 2 and 10 inclusive:numbers = list(range(2, 11, 2))
print(numbers) # [2, 4, 6, 8, 10]
enumerate() and zip() functions in Pythonenumerate(): used to iterate over a sequence and keep track of the index for each item in that sequence. The enumerate() function takes an iterable as an argument and returns an enumerate object that consist of the index and value of each item in the iterable.languages = ['Spanish', 'English', 'Russian', 'Chinese']
for index, language in enumerate(languages):
print(f'Index {index} and language {language}')
# Result
# Index 0 and language Spanish
# Index 1 and language English
# Index 2 and language Russian
# Index 3 and language Chinese
enumerate() function can also be used outside of a for loop:languages = ['Spanish', 'English', 'Russian', 'Chinese']
print(list(enumerate(languages)))
# [(0, 'Spanish'), (1, 'English'), (2, 'Russian'), (3, 'Chinese')]
The enumerate() function also accepts an optional start argument that specifies the starting value for the count. If this argument is omitted, then the count will begin at 0.
zip() : Used to iterate over multiple iterables in parallel. Here's an example using the zip() function to iterate over developers and ids:
developers = ['Naomi', 'Dario', 'Jessica', 'Tom']
ids = [1, 2, 3, 4]
for name, id in zip(developers, ids):
print(f'Name: {name}')
print(f'ID: {id}')
"""
Result
Name: Naomi
ID: 1
Name: Dario
ID: 2
Name: Jessica
ID: 3
Name: Tom
ID: 4
"""
even_numbers = [num for num in range(21) if num % 2 == 0]
print(even_numbers)
filter(): Used to filter elements from an iterable based on a condition. It returns an iterator that contains only the elements that satisfy the condition. Here is an example of creating a new list of just words longer than four characters:words = ['tree', 'sky', 'mountain', 'river', 'cloud', 'sun']
def is_long_word(word):
return len(word) > 4
long_words = list(filter(is_long_word, words))
print(long_words) # ['mountain', 'river', 'cloud']
map(): Used to apply a function to each item in an iterable and return a new iterable with the results. Here is an example of using the map() function to convert a list of celsius temperatures to fahrenheit:celsius = [0, 10, 20, 30, 40]
def to_fahrenheit(temp):
return (temp * 9/5) + 32
fahrenheit = list(map(to_fahrenheit, celsius))
print(fahrenheit) # [32.0, 50.0, 68.0, 86.0, 104.0]
sum(): Used to get the sum from an iterable like a list or tuple. Here is an example of using the sum() function:numbers = [5, 10, 15, 20]
total = sum(numbers)
print(total) # Result: 50
start argument which sets the initial value for the summation. Here is an updated example using the start argument as a positional argument:numbers = [5, 10, 15, 20]
total = sum(numbers, 10) # positional argument
print(total) # 60
start argument as a keyword argument like this instead:numbers = [5, 10, 15, 20]
total = sum(numbers, start=10) # keyword argument
print(total) # 60
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # [2, 4]
Review the Loops and Sequences topics and concepts.