An if else statement is a conditional statement that executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed.
if(condition):
statement(s)
else:
statement(s)
year = int(input("Enter the year: "))
if year%4 == 0:
print(year," is leap year.")
else:
print(year," is not a leap year.")
Enter the year: 2019 2019 is not a leap year.
The elif allows to check for multiple expressions. If the condition for if is false, it checks the condition of the elif. If the condition for elif is true, it executes the block of code. If all the conditions are false, the else condition is executed.
if(condition):
statement(s)
elif(condition):
statement(s)
else:
statement(s)
a = 20
b = 20
if a > b:
print('a is greater than b.')
elif a < b:
print('b is greater than a.')
else:
print('a equals to b.')
a equals to b.
Using a while loop we can execute a set of statements as long as a condition is true.
while condition:
statement(s)
n = 0
sum = 0
while n <= 10:
sum = sum+n
n+=1
print("The sum of first 10 natural numbers is ",sum)
The sum of first 10 natural numbers is 55
For loop executes the sequence of code according to the specified times and conditions.
for a variable in sequence:
statement(s)
n = 10
for i in range(1,6):
print(n*i)
10 20 30 40 50
Using 'Continue' statement we can stop the current iteration, and continues with the next iteration. Using 'Break' statement we can stop the loop even if the while condition is true.
list = [10,20,30,40,50,60,70,50]
for i in range(0,len(list)):
if list[i] == 50:
break
i+=1
print('Index is ',i)
Index is 4
list = [15,30,45,78,90,45,78]
for i in range(0,len(list)):
if list[i]%2 == 0:
print(list[i])
continue
i+=1
30 78 90 78