Conditional Statements in Python:
- The expression which returns either True or False value is known as conditions.
- Conditional statements are used to execute the block or skip the execution of the block or select the only one block among the multiple blocks based on the expression/condition evaluation value
- if (simple if)
- if else
- if elif else (here else is optional)
if :-
Syntax 1: by using single statement in if
if condition : statement
Syntax 2: by using multiple statements in if
if condition :
statement 1
statement 2
---------------
---------------
statement n
In python ,no curly brace concept.
python is follows the space indentation block concept.
Example1:
x=int(input("enter a positive number: "))
if x<10:
print("given number is 1 digit number")
Output 1:
enter a positive number: 7
given number is 1 digit number
Output 2:
enter a positive number: 25
if else :-
Syntax:
if condition :
statement 1
statement 2
--------------
--------------
else :
statement 1
statement 2
--------------
--------------
Example:
y=int(input("enter a positive number: "))
if x<10:
print("given number is 1 digit number")
else:
print("given number is >=2 digit number")
Output 1:
enter a positive number: 5
given number is 1 digit number
Output 2:
enter a positive number: 17
given number is >=2 digit number
if elif else :-
Syntax:
if condition:
statement 1
statement 2
---------------
---------------
elif condition:
statement 1
statement 2
---------------
---------------
else :
statement 1
statement 2
--------------
--------------
Example 1:
z=int(input("enter a positive number: "))
if z<10:
print("given number is 1 digit number")
elif z<100:
print("given number is 2 digit number")
elif z<1000:
print("given number is 3 digit number")
else:
print("given number is >=4 digit number")
Output 1:
enter a positive number: 5
given number is 1 digit number
Output 2:
enter a positive number: 27
given number is 2 digit number
Output 3:
enter a positive number: 345
given number is 3 digit number
Output 4:
enter a positive number: 4672
given number is >=4 digit number
Example 2:
name=input("Enter your name: ")
age=int(input("Enter your age: "))
if name=='siva':
print("hai siva, Hw r u")
elif age<18:
print("you are not major,so you are not eligible for voter registration")
else:
print("hai", name, "congrates! you are eligible for voter registration")
Output 1:
Enter your name: siva
Enter your age: 16
hai siva, Hw r u
Output 2:
Enter your name: krishna
Enter your age: 16
you are not major, so you are not eligible for voter registration
Output 3:
Enter your name: krishna
Enter your age: 20
hai krishna congrates! you are eligible for voter registration
No comments:
Post a Comment