Python Branching and Loops
Python Branching and Loops
if condition:
statement1
statement2
a_number = 34
if a_number % 2 == 0:
print("we're inside an if block")
print("The given number {} is even".format(a_number))
aother_number = 33
if another_number % 2 = 0:
print('The given number {} is even.'.format(another_number))
if condition:
statement1
statement2
else:
statement4
statement5
If condition evaluates to True , the statements in the if block are executed. If it evaluates to False , the
statements in the else block are executed.
a_number = 34
if a_number % 2 == 0:
print('The given number {} is even'.format(a_number))
else:
print ('The given number {} is odd'.format(a_number))
if a_candidate in the_3_names:
print('{} is a student'.format(a_candidate))
else:
print('{} is not a student'.format(a_candidate))
today = "Wednesday"
if today == 'Sunday':
print("Today is the day of the sun.")
elif today == 'Monday':
print("Today is the day of the moon.")
elif today == 'Tuesday':
print("Today is the day of Tyr, the god of war.")
elif today == 'Wednesday':
print("Today is the day of Odin, the supreme diety.")
elif today == 'Thursday':
print("Today is the day of Thor, the god of thunder.")
elif today == 'Friday':
print("Today is the day of Frigga, the goddess of beauty.")
elif today == 'Saturday':
print("Today is the day of Saturn, the god of fun and feasting.")
a_number = 49
if a_number % 2 == 0:
print("{} is divisible by 2". format(a_number))
elif a_number % 3 == 0:
print('{} is divisible by 3'.format(a_number))
elif a_number % 5 == 0:
print('{} is divisible by 5'.format(a_number))
else:
print('All checks failded!')
print('{} is not divisible 2,3 and 5'.format(a_number))
if '':
print('The condition evaluted to True')
else:
print('The condition evaluted to False')
if 'Hello':
print('The condition evaluted to True')
else:
print('The condition evaluted to False')
a_number = 15
if a_number % 2 == 0:
print('{} is even'.format(a_number))
if a_number % 3 == 0:
print('{} is also divisible by 3'.format(a_number))
else:
print('{} is not divisible by 3'.format(a_number))
else:
print('{} is odd'.format(a_number))
if a_number % 5 == 0:
print('{} is also divisible by 5'.format(a_number))
else:
print('{} is not divisible by 5'.format(a_number))
15 is odd
15 is also divisible by 5
jovian.commit(project='python-branching-and-loops')