This is the sixth of Python Tutorials for Beginners. You can see the other Python tutorials in this blog here. This Python beginner tutorial explains the if statement in Python with multiple examples. You can use the if statement to run a block of code depending on a condition. You can write a Python condition using Python comparison operators or Python logical operators. Please view the Python video tutorial 6 or read on...
In Python programming language, the block of code is formed by indenting it by a few spaces, typically 4 spaces. The if statements in Python programming have three formats, the if statement, the if else statement and the if elif else statement. Now let us see these formats with Python examples.
Note : the formats are followed by Python samples
In the first format, the condition is evaluated. If the condition is True, then the code block is run by Python. If the condition is False, nothing happens.
if condition:
code block
# Python code example
# input function request a string from the user
name = input('Enter a name : ')
# len function returns the length of the string, name
if len(name) > 5:
print(name, ' has the length', len(name))
code block1
else:
code block2
# Python code example
name = input('Enter a name : ')
if len(name) > 5:
print('The length is more than 5 letters.')
else:
print('The length is less than or equal to 5 letters.')
code block1
elif condition2:
code block2
else:
code block3
# Python code example
# datetime module helps you know the current day
import datetime
# weekday function returns an integer between 0 and 6 inclusive
currentDay = datetime.datetime.today().weekday()
# in the following print statement, end ensures printing continues on the same line
print ('Have a good ', end = '')
if currentDay == 0:
print ('Monday')
elif currentDay == 1:
print ('Tuesday')
elif currentDay == 2:
print ('Wednesday')
elif currentDay == 3:
print ('Thursday')
elif currentDay == 4:
print ('Friday')
elif currentDay == 5:
print ('Saturday')
elif currentDay == 6:
print ('Sunday')
else:
print ('day')
Note : the formats are followed by Python samples
In the first format, the condition is evaluated. If the condition is True, then the code block is run by Python. If the condition is False, nothing happens.
if condition:
code block
# Python code example
# input function request a string from the user
name = input('Enter a name : ')
# len function returns the length of the string, name
if len(name) > 5:
print(name, ' has the length', len(name))
In the second format, the condition is evaluated. If the condition is True, the code block1 is run by Python. If the condition is False, the code block2 is run.
if condition:code block1
else:
code block2
# Python code example
name = input('Enter a name : ')
if len(name) > 5:
print('The length is more than 5 letters.')
else:
print('The length is less than or equal to 5 letters.')
In the third format of the if statement, the condition1 is evaluated. If the condition1 is True, the if block i.e. code block1 is run by Python. But if the condition1 is False, the condition2 is evaluated. If the condition2 is True the elif (meaning else if) block is run. But if the condition2 is False, the else block i.e. code block3 is run. Instead of just one elif block, there can be multiple elif blocks in this format of the Python if statement.
if condition1:code block1
elif condition2:
code block2
else:
code block3
# Python code example
# datetime module helps you know the current day
import datetime
# weekday function returns an integer between 0 and 6 inclusive
currentDay = datetime.datetime.today().weekday()
# in the following print statement, end ensures printing continues on the same line
print ('Have a good ', end = '')
if currentDay == 0:
print ('Monday')
elif currentDay == 1:
print ('Tuesday')
elif currentDay == 2:
print ('Wednesday')
elif currentDay == 3:
print ('Thursday')
elif currentDay == 4:
print ('Friday')
elif currentDay == 5:
print ('Saturday')
elif currentDay == 6:
print ('Sunday')
else:
print ('day')
Want to see the types of if statements with Python samples in action? View my Python video tutorial 6. Thank you.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.