Open In Colab

Conditional Statements and Loops

Comparision Operations

  • greater than: >
  • less than: <
  • greater than or equal to: >=
  • less than or equal to: <=
  • equal to: ==
  • not equal to: !=
  • identical object: is
  • not identical object: is not
In [1]:
name = 'sandstone'
name == 'sandstone'
Out[1]:
True
In [2]:
name != 'basalt'
Out[2]:
True
In [3]:
1>3
Out[3]:
False

Combining conditionals and If Statements

  • x and y: Both x and y must evaluate to True for true-result.
  • x or y: If x or y is true, result is true.
  • not x: Negate the value of x (True becomes False and vice versa).
In [4]:
a = 77
b = 200
if b>a:
    print('b is greater than a.')
b is greater than a.
In [5]:
a = 77
b = 200
if b>a:
    print('b is greater than a.')
elif a == b:
    print('a and b are equal.')
b is greater than a.
In [6]:
a = 77
b = 200
if b>a:
    print('b is greater than a.')
elif a == b:
    print('a and b are equal.')
else:
    print('a is greater than b.')
b is greater than a.
In [7]:
a = 77
b = 200
print('A') if a>b else print('B') # one line if else statement
B
In [8]:
a = 220
b = 220
print('A') if a>b else print('=') if a == b else print('B') # one line if else statement with 3 conditions.
=
In [9]:
a = 200
b = 77
c = 700
if a>b and c>a:
    print('Both conditions are True.')
Both conditions are True.
In [10]:
a = 70
b = 22
c = 770
if a>b or a>c:
    print('At least one of the conditions is True.')
At least one of the conditions is True.
In [11]:
x = 77
if x>10:
    print('Above ten,')
    if x>20: #nested if
        print('and also above 20!')
    else:
        print('but not above 20.')
Above ten,
and also above 20!
In [12]:
a = 77
b = 220
if b>a:
    pass # the pass statement

For Loops

  • A for loop is used for iterating over a sequence.
  • The for loop does not require an indexing variable to set beforehand.
In [13]:
rocks = ['sandstone', 'basalt', 'breccia']
for x in rocks:
    print(x) # Print each rock in a rock list.
sandstone
basalt
breccia
In [14]:
rocks = ['sandstone', 'basalt', 'breccia']
for x in rocks:
    print(x)
    if x == 'basalt':
        break # Exit the loop
sandstone
basalt
In [15]:
rocks = ['sandstone', 'basalt', 'breccia']
for x in rocks:
    if x == 'basalt':
        break # Exit the loop when  is 'basalt', but the break comes before the print.
    print(x)
sandstone
In [16]:
rocks = ['sandstone', 'basalt', 'breccia']
for x in rocks:
    if x == 'basalt':
        continue
    print(x)
sandstone
breccia

The range( ) Function

  • The built-in range( ) is used to produce a sequence of numbers.
  • range(start, stop, step)
  • start: (optional) The start index is an integer, and if not given, the default value is 0.
  • stop: The stop index decides the value at which the range function has to stop. It is a mandatory input to range function. The last value will be always 1 less than the stop value.
  • step: (optional).The step value is the number by which the next number is range has to be incremented, by default, it is 1.
  • The range( ) function defaults to increment the sequence by 1; however if is possible to specify the increment value by adding a third parameter.
  • range(2, 7) means values from 2 to 6 (but not including 6).
In [17]:
for x in range(7):
    print(x)
0
1
2
3
4
5
6
In [18]:
for x in range(2,7):
    print(x)
2
3
4
5
6
In [19]:
for x in range(2, 27, 3):
    print(x)
2
5
8
11
14
17
20
23
26
In [20]:
print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [21]:
for i in range(6):
    print(i)
0
1
2
3
4
5
In [22]:
for i in range(2, 20, 2):
    print(i)
2
4
6
8
10
12
14
16
18
In [23]:
startvalue = range(5)[0] 
print(startvalue) 

secondvalue = range(5)[1] 
print(secondvalue) 

lastvalue = range(5)[-1]
print(lastvalue)
0
1
4

Else in For Loop

  • The else keywod in a for loop specifies a block of code to be executed when the loop is finished.
  • The else block will not be executed if the loop is stopped by a break statement.
In [24]:
for x in range(7):
    print(x)
else:
    print('Finished.')
0
1
2
3
4
5
6
Finished.
In [25]:
for x in range(7):
    if x == 3: break
    print(x)
else:
    print('Finished.')
0
1
2
In [26]:
for x in range(1, 7):
    print(x)
    break
else:
    print('No break.')
1

Nested Loops

In [27]:
types = ['igneous', 'sedimentary', 'metamorphic']
rocks = ['basalt', 'sandstone', 'gneiss']

for x in types:
    for y in rocks:
        print(x, y)
igneous basalt
igneous sandstone
igneous gneiss
sedimentary basalt
sedimentary sandstone
sedimentary gneiss
metamorphic basalt
metamorphic sandstone
metamorphic gneiss

While Loops

The while loop executes a set of statements as long as a condition is true.

In [28]:
i = 1
while i < 7:
    print(i)
    i +=1
1
2
3
4
5
6
In [29]:
i = 1
while i < 7:
    print(i)
    if i == 3:
        break
    i +=1
1
2
3
In [30]:
i = 1
while i < 7:
    i +=1
    if i == 3:
        continue
    print(i)
2
4
5
6
7
In [31]:
i = 0
while i < 7:
    print(i)
    i +=1
else:
    print('i is no longer less than 7.')
0
1
2
3
4
5
6
i is no longer less than 7.