Subscribe to our Youtube Channel To Learn Free Python Course and More
If-Else
In this session, we will learn the decision making part of programming. Sometimes you want your code to run only when a certain condition is satisfied. We will see how you can do it in this chapter.
Basic if Statement
# Basic If syntax
x = 2
if x == 2:
print('x is:', x)
By looking at the above code you can understand that this code will print x only if x is equal to 2. In this code, ‘x’ will not get printed if it is anything else other than 2.
So if statement evaluates the condition first and executes the statement inside the body only when the condition is true.
if <condition>:
<statement>
If-Else Statement
If...else statement is used when you want to specify what a program should do when the condition is false. See the below syntax.
# if...else
if <condition>:
<statement>
else:
<statement>
In if...else syntax, if the condition is true, inside the body of if will be executed and if the condition is false then the inside the body of else will be executed.
See the below example
name = 'Kumar'
if name == 'Kumar':
print(f'Hello {name}')
else:
print("You are not 'Kumar'")
# Output: Hello Kumar
'''
In the above code snippet, first <if> evaluates the condition name == 'Kumar', which is true, so the <statement> inside the body of <if> got executed. If the condition would have been false then the interpreter would come to <else> statement.
'''
If--Elif--Else Statement
If you want to check multiple conditions then you can use if...elif...else syntax. Here elif is short for else if.
# if...elif...else
if <condition>:
<statement>
elif <condition>:
<statement>
else:
<statement>
As you know that and statements will execute only if the condition is true, if both the conditions are false then the statement will execute.
x = 5
if x == 2:
print('Inside the body of if')
elif x == 5:
print('Inside the body of elif')
else:
print('Inside the body of else')
# Output
# Inside the body of elif
'''
Because only <elif> condition is true
'''
For Loop
Now we will learn how to use for loop. For loop is used to iterate over data structures like String, List, Tuple and Dictionary.
If you are asked to print each value in a string or list, How would you do that? You might be thinking that using index can be one option. But it is a tiresome task. See the below code.
s = [1, 2, 3, 4, 5, 6]
print(s[0])
print(s[1])
print(s[2])
print(s[3])
print(s[4])
print(s[5])
# Output
# 1
# 2
# 3
# 4
# 5
# 6
For loop makes it easy to access each value of any sequence type data structure. We will see that how you can use For loop to get the value of String, List, Tuple and Dictionary.
String
String is a sequence of characters. To access each character, we can use For loop. See the below code.
# String
s = "Python is awesome"
for char in s:
print(char)
# Output
# P
# y
# t
# h
# o
# n
#
# i
# s
#
# a
# w
# e
# s
# o
# m
# e
In the For loop syntax, char is a variable that refers to characters in the string. The loop runs from the starting of the string and stops at the end of the string. In the first iteration, char refers to the first character, in next iteration char refers to the second character and keeps on accessing all the characters of the string.
Char is just a variable, you can name it according to yourself.
List
List is a sequence of different types of data values. See the below code to access list values using For loop.
# List
lst = [1, 2, True, 4.234, False, 'List']
for elm in lst:
print(elm)
'''
Output:
1
2
True
4.234
False
List
'''
In the above for loop syntax, the variable refers to each data value. So in each iteration each data value gets printed.
Tuple
Tuples are also the same as list but the difference is you can not change these values.
# Tuple
tup = (1, 2, True, 4.234, False, 'List')
for elm in lst:
print(elm)
'''
Output:
1
2
True
4.234
False
List
'''
Dictionary
In the dictionary, there are three ways you can iterate the dictionary.
- Iterating through all keys.
- Iterating through all the values.
- Iterating through all key-value pairs
Iterating through all keys
person = {'name':'Kumar', 'age':23, 'email': 'kumar.com'}
for key in person:
Person[key]
'''
# Output:
'Kumar'
23
'Kumar.com'
'''
Iterating through all the values
For iterating through values, there is a method called values() that we can use to access dictionary values.
person = {'name':'Kumar', 'age':23, 'email': 'kumar.com'}
for value in person.values():
print(value)
'''
# Output:
Kumar
23
Kumar.com
'''
Iterating through all the key-value pairs
If you want to iterate through all the key-value pairs then you have to use the method called items(). See the below code.
person = {'name':'Kumar', 'age':23, 'email': 'kumar.com'}
for key, value in person.items():
print(f'{key}:{value}')
'''
# Output:
name:Kumar
age:23
email:kumar.com
'''
Set
Using for loop we can access each element of the set.
s = {1, 2, 4, "hello", False}
for elm in s:
print(elm)
'''
# Output
False
1
2
4
hello
'''
Top comments (0)