= ['Hydrogen', 'Helium', 'Lithium', 'Beryllium']
list_of_elements
for element in list_of_elements:
print(element)
Hydrogen
Helium
Lithium
Beryllium
Iterations play a crucial role in Python, allowing us to execute code repeatedly which can save a significant amount of time and effort. In Python, the most common forms of iteration are for
and while
loops. However, Python offers an even more concise and readable way to perform iterations, especially on lists — this is through list comprehensions. A list comprehension consists of brackets containing an expression followed by a for
statement, and possible if
clauses. It provides a neat way to take an existing list, perform an operation on each element, and return the results in a new list. It is a very Pythonic way to generate or transform lists in a single line of code.
In Python, “iteration” means executing the same block of code over and over, potentially many times. Structures used for iteration are “loops”. Python provides two basic types of loop - for
loop and while
loop.
A for
loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). Each item in the sequence is assigned to the iteration variable, and the statement(s) are executed until the entire sequence is processed.
Here is an example of a for
loop that iterates over a list of elements:
= ['Hydrogen', 'Helium', 'Lithium', 'Beryllium']
list_of_elements
for element in list_of_elements:
print(element)
Hydrogen
Helium
Lithium
Beryllium
For each iteration, the value of element
will be updated to the next value in the list. So, the print()
function will print each element from the list.
A while
loop repeatedly tests an expression (the loop condition), and keeps executing the loop body while the expression is True
. Here is an example of a while
loop:
= 25
temperature
while temperature > 20:
print(f"{temperature} °C : The reaction is too fast!")
= temperature - 1 temperature
25 °C : The reaction is too fast!
24 °C : The reaction is too fast!
23 °C : The reaction is too fast!
22 °C : The reaction is too fast!
21 °C : The reaction is too fast!
The loop will continue to print “The reaction is too fast!” and subtract 1 from the temperature, as long as the temperature is strictly greater than 20.
Loop control statements change the execution of a loop from its normal sequence. Python provides the following loop control statements:
break
statement: Terminates the loop and transfers execution to the statement immediately following the loop.continue
statement: Causes the loop to skip the rest of its body for this iteration and immediately retest its condition prior to reiterating.for
loop, print the atomic number for each element in your list of five elements from the previous exercise.while
loop, keep dividing a pressure of 100 atm by 2 until it is less than 1 atm, and print the current pressure at each step.while
loop above, add a break
statement to exit the loop when the pressure is less than 5 atm.while
loop above, add a continue
statement to skip the loop iteration when the pressure is exactly 10 atm and not print the pressure at this step.Solution 1:
= ['Hydrogen', 'Helium', 'Lithium', 'Beryllium']
list_of_elements = [1, 2, 3, 4]
list_of_atomic_numbers
for i in range(len(list_of_elements)):
print("The atomic number of", list_of_elements[i], "is", list_of_atomic_numbers[i])
The atomic number of Hydrogen is 1
The atomic number of Helium is 2
The atomic number of Lithium is 3
The atomic number of Beryllium is 4
or
= ['Hydrogen', 'Helium', 'Lithium', 'Beryllium']
list_of_elements = [1, 2, 3, 4]
list_of_atomic_numbers
for element, atomic_number in zip(list_of_elements, list_of_atomic_numbers):
print("The atomic number of", list_of_elements[i], "is", list_of_atomic_numbers[i])
The atomic number of Beryllium is 4
The atomic number of Beryllium is 4
The atomic number of Beryllium is 4
The atomic number of Beryllium is 4
Solution 2:
= 100
pressure
while pressure >= 1:
print(pressure)
= pressure / 2 pressure
100
50.0
25.0
12.5
6.25
3.125
1.5625
Solution 3:
= 100
pressure
while pressure >= 1:
if pressure < 5:
break
print(pressure)
= pressure / 2 pressure
100
50.0
25.0
12.5
6.25
Solution 4:
= 100
pressure
while pressure >= 1:
= pressure / 2
pressure if pressure == 10:
continue
print(pressure)
50.0
25.0
12.5
6.25
3.125
1.5625
0.78125
By controlling flow in Python, you can execute complex algorithms that can respond to varied conditions and perform intricate tasks at a high level.
List comprehension is an elegant and concise way to create a new list from an existing list in Python.
A list comprehension consists of an expression followed by a for
statement inside square brackets.
Here is an example to make a list with each item being increasing power of 2.
= [2 ** x for x in range(10)]
powers_of_two print(powers_of_two)
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
We can also create more advanced list comprehensions which include a conditional statement on the iterable. Let’s square only the positive numbers in the following list.
= [1, -2, 3, -4, -5, 6, -7]
numbers = [n ** 2 for n in numbers if n > 0]
squared print(squared)
[1, 9, 36]
In the context of Chemistry, let’s say we had a list of various elements and we wanted a new list which only included the elements with an atomic number less than 3.
= [('Hydrogen', 1), ('Helium', 2), ('Lithium', 3), ('Beryllium', 4), ('Boron', 5)]
elements = [name for name, atomic_number in elements if atomic_number < 3]
light_elements print(light_elements)
['Hydrogen', 'Helium']
Solution 1:
= [-1, 2, -3, 2, 1, -2]
charges = [charge ** 2 for charge in charges]
squared_charges print(squared_charges) #Output: [1, 4, 9, 4, 1, 4]
[1, 4, 9, 4, 1, 4]
Solution 2:
= [1.0079, 4.0026, 6.94, 9.0122, 10.81]
weights = [weight for weight in weights if weight > 2]
weights_gt_2 print(weights_gt_2) #Output: [4.0026, 6.94, 9.0122, 10.81]
[4.0026, 6.94, 9.0122, 10.81]
List comprehension is a powerful tool that makes your code more efficient and easier to read. It can be used whenever you need to transform or filter a list in Python.