There are three conditional statement in python;
- if
- if-elif
- if-elif-else
if
Syntax
Python
if condition:
statement-1
statement-2
statement-3
- If condition is true then statements will be executed.
Example
Python
name=input("Enter Name:")
if name=="amit" :
print("Hello Amit Good Morning")
print("How are you!!!")
Output
PowerShell
Enter Name:amit
Hello Amit Good Morning
How are you!!!
PowerShell
Enter Name:Ravi
How are you!!!
if-else
Syntax
Python
if condition:
statement-1
else:
statement-2
- if condition is true then statement-1 will be executed otherwise statement-2 will be executed.
Example
Python
name=input("Enter Name:")
if name=="amit" :
print("Hello amit Good Morning")
else:
print("Hello Guest Good Moring")
print("How are you!!!")
Output
PowerShell
Enter Name:amit
Hello amit Good Morning
How are you!!!
PowerShell
Enter Name:ravi
Hello Guest Good Moring
How are you!!!
if-elif-else
Syntax
Python
if condition-1:
statement-1
elif condition-2:
statement-2
elif condition-3:
statement-3
elif condition-4:
statement-4
...
...
else:
statement-n
- Based on condition the corresponding action will be executed.
Example
Python
brand=input("Enter Your name: ")
if brand=="amit" :
print("amit you are principal")
elif brand=="manish":
print("manish you are student")
elif brand=="ajay":
print("ajay you are student")
else :
print("you are not from school")
Output
PowerShell
Enter Your Favourite Brand: amit
amit you are principal
PowerShell
Enter Your Favourite Brand: manish
manish you are student
PowerShell
Enter Your Favourite Brand: sanjay
you are not from school
Note
- else part is always optional.
- There is no switch statement in Python.
Question
Write a program to find biggest of given 2 numbers from the command prompt?
Python
num1=int(input("Enter First Number:"))
num2=int(input("Enter Second Number:"))
if num1>num2:
print("Biggest Number is:",num1)
else :
print("Biggest Number is:",num2)
Output
PowerShell
Enter First Number:8
Enter Second Number:9
Biggest Number is: 9
Question
Write a program to find biggest of given 3 numbers from the command prompt?
Python
num1=int(input("Enter First Number:"))
num2=int(input("Enter Second Number:"))
num3=int(input("Enter Third Number:"))
if num1>num2 and num1>num3:
print("Biggest Number is:",num1)
elif num2>num3:
print("Biggest Number is:",num2)
else :
print("Biggest Number is:",num3)
Output
PowerShell
Enter First Number:9
Enter Second Number:8
Enter Third Number:7
Biggest Number is: 9
PowerShell
Enter First Number:10
Enter Second Number:20
Enter Third Number:30
Biggest Number is: 30
Question
Write a program to check whether the given number is in between 1 and 10?
Python
num=int(input("Enter Number:"))
if num>=1 and num<=10 :
print("The number",num,"is in between 1 to 10")
else:
print("The number",num,"is not in between 1 to 10")
Output
PowerShell
Enter Number:1
The number 1 is in between 1 to 10
PowerShell
Enter Number:11
The number 11 is not in between 1 to 10
Question
Write a program to take a single digit number from the key board and print is value in English word?
Python
n=int(input("Enter a digit from o to 9:"))
if n==0 :
Print("ZERO")
elif n==1:
print("ONE")
elif n==2:
print("TWO")
elif n==3:
print("THREE")
elif n==4:
print("FOUR")
elif n==5:
print("FIVE")
elif n==6:
print("SIX")
elif n==7:
print("SEVEN")
elif n==8:
print("EIGHT")
elif n==9:
print("NINE")
else:
print("PLEASE ENTER A DIGIT FROM 0 TO 9")
Output
PowerShell
Enter a digit from o to 9:1
ONE
PowerShell
Enter a digit from o to 9:9
NINE
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!