Ternary operator: It is a one line way to write the if-else condition.
Syntax: True value if (condition) else False value
Ex: To find a given number is odd or even
if num%2 ==0:
print("given number is even")
else:
print("given number is odd")
Now, it can be written as
var = "num is even" if(num%2 ==0) else "num is odd"
print (var)
while loop: A while loop is used when you want to repeat the code until it satisfies the condition as true.
Ex: Print a number from 1 to 10
num = 1
while num<=10:
print(num)
num = num+1
Output:
1
2
3
4
5
6
7
8
9
10
Ex: To print a table with given variable
num = int(input("enter a number "))
var = 1
while var<=5:
print(num*var)
var = var+1
Output:
enter a number 3
3
6
9
12
15
Top comments (0)