Conditional Statements
If...else
If...else is the conditional statement to check for the condition and return the output based on the condition output.
Python relies on indentation to define the scope of the code.
if
if statement is return using teh if keyboard.
Example
if a > b:
print('a is greater than b)
Elif
elif keywords says that "if the previous conditions were not true, then try this condition".
Example
if a > b:
print('a is greater than b)
elif a == b:
print('a is equal to b)
Else
else keyword catches anything which isn't caught by the preceding conditions.
Example
if a > b:
print('a is greater than b)
elif a == b:
print('a is equal to b)
else:
print('a is less than b)
Short Hand If
Example
if a > b: print('a is greater than b')
Short Hand If...Else
Example
print("A") if a > b else print("B")
print("A") if a > b else print("=") if a == b else print("B")
Nested if
Example
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
Pass statement
When we don't have any operation to perform after checking the condition, we can use just pass keyword.
Example
if a > b:
pass
Loops
Python has two loop commands * While loop * For loop
While Loop
We can execute set of commands or statements as long as the condition is true
Example
x = 1
while (x < 5):
print(x)
x += 1
Break - stops the loop even if the while condition is true
Example
x = 1
while (x < 5):
print(x)
if x == 3:
break
x += 1
continue - continue statement stops the current iteration and continues with the next
Example
x = 1
while (x < 5):
x += 1
if x == 3:
continue
print(x)
For Loop
For loop iterates over an object
Example
mylist = [1,2,3]
for x in mylist:
print (x)
break - breaks the loop before it has looped all the elements
Example
mylist = [1,2,3]
for x in mylist:
if x == 2:
break
print (x)
continue - stops the current iteration of the loop and continues with the next
Example
mylist = [1,2,3]
for x in mylist:
if x == 2:
continue
print (x)
Useful Built-in functions
range function
range() - range function returns a sequence of numbers starting from 0 by default and increments by 1, and ends at a specific number
range(5) -- sequence of numbers from 0 to 4, excluding 5. range(2,10,2) -- first parameter will be start point, second indicates stop point, third indicates step size.
Example
mylist = list(range(5))
mylist
[0,1,2,3,4]
Using for loop
Example
for x in range(2,6):
print(x)
enumerate
enumerate function adds counter to the iterable
Example
word = 'abcde'
for item in enumerate(word):
print item
Result :
(0,a)
(1,b)
(2,c)
(3,d)
(4,e)
Example
word = 'abcde'
for item in enumerate(word, 2):
print item
Result :
(2,a)
(3,b)
(4,c)
(5,d)
(6,e)
Example
word = 'abcde'
for (index,letter) in enumerate(word):
print index
Result :
0
1
2
3
4
Zip function
Zip function returns a zip object. First iterable of the first object will be paired with first iterable of the second object.
Example
mylist1 = [1,2,3]
mylist2 = [a,b,c]
for item in zip(mylist1,mylist2):
print (item)
Result:
(1,a)
(2,b)
(3,c)
List Comprehensions
List Comprehensions is the way to define and create lists based on the existing lists
Example
myString = 'Hello'
mylist = []
for letter in myString:
mylist.append(letter)
print(mylist)
['H', 'e', 'l', 'l', 'o']
Using List Comprehensions
Example
mylist = [x for x in 'word']
print(mylist)
['w', 'o', 'r', 'd']
Example
mylist = [x for x in range(0,11) if x%2 == 0]
print(mylist)
[0, 2, 4, 6, 8, 10]
Example
mylist = [x*y for x in [2,4,6] for y in [1,10,100]]
print(mylist)
[2, 20, 200, 4, 40, 400, 6, 60, 600]