Python Conditions and If statements
An "if statement" is written by using the if keyword.
Example
a = 33
b = 200
if b > a:
print("b is greater than a") #indent required
Indentation
Python depend on on indentation (whitespace at the beginning of a line) to define scope in the code.
Other programming languages like C, C++ often use curly-brackets for this purpose.
Else
The else keyword catches anything which isn't caught by the preceding conditions.
a = 200
b = 33
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")
Elif
The elif keyword is Python's way of saying "if the previous conditions were not true, then try this
condition".
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Nested If
We can have if statements inside if statements, this is called nested if statements.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
S. SHANAWAZ BASHA 3
Python Loops
Python has two primitive loop commands:
1. while loops 2. for loops
The while Loop
With the while loop we can execute a set of statements as long as a condition is true.
Example
i=1
while i < 5:
print(i)
i += 1
Result: The above code Prints i as long as i is less than 5.
The break Statement
With the break statement we can stop the loop even if the while condition is true.
Example
i=1
while i < 6:
print(i)
if i == 3:
break
i+=1
The loops will break when i is 3.
Python For Loop
A for loop is used for iterating over a sequence (either a list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works similarly like an iterator
method as in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
Example
city = ["Hyderabad", "Delhi", "Banglore"]
for x in city:
print(x)
Output:
Hyderabad
Delhi
Banglore
S. SHANAWAZ BASHA 4
Looping Through a String
Using for loop we can iterate inside a string, the strings contain a sequence of characters.
Example
for x in "Delhi":
print(x)
Output:
D
e
l
h
i
The continue Statement
With the help of continue statement we can stop the current iteration of the loop, and continue with the
next iteration.
Example
city= ["Hyderabad", "Delhi", "Banglore"]
for x in city:
if x == "Delhi":
continue
print(x)
Output:
The loop do not prints the city – Delhi and moves to the next iteration.
The range() Function
The range function helps to execute a loop a specified number of times. The range() function returns a
sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a
specified number.
Example
for x in range(10):
print(x)
Output: The above code prints numbers from 0 to 9
The range() function defaults to 0 as a starting value, however it is possible to specify the starting value
by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6).
syntax
for x in range(2, 9):
print(x)
Output:
The above code prints the values starting from 2 to 8.
S. SHANAWAZ BASHA 5
Adding Parameters to the range()
The range() function by default increments the sequence by 1, however it is possible to specify the
increment value by adding a third parameter: range(2, 30, 3):
For example Increment the sequence with 3.
Syntax
for x in range(2, 30, 3):
print(x)
Output: The above code prints the values 2 5 8 11 14 17 20 23 26 29
Nested Loops
A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each iteration of
the "outer loop":
For Example Print each the capital of each state.
Program
capital = ["Hyderabad", "New Delhi", "Banglore"]
state = ["Telangana", "Delhi", "Karnataka"]
for x in capital:
for y in state:
print(x, y)
Output: The above code will result in
Hyderabad Telangana
New Delhi Delhi
New Delhi Karnataka
Banglore Karnataka
The pass Statement
for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass
statement to avoid getting an error.
Example
for x in [0, 1, 2]:
pass
S. SHANAWAZ BASHA 6