0% found this document useful (0 votes)
3 views6 pages

Python Control Statements

The document explains Python's if-else statements, which are essential for decision-making in programming. It covers the syntax and usage of if, if-else, elif, and nested if statements, along with examples for each. Additionally, it highlights the importance of indentation in Python to define code blocks.

Uploaded by

Surya K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Python Control Statements

The document explains Python's if-else statements, which are essential for decision-making in programming. It covers the syntax and usage of if, if-else, elif, and nested if statements, along with examples for each. Additionally, it highlights the importance of indentation in Python to define code blocks.

Uploaded by

Surya K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python If-else statements

Decision making is the most important aspect of almost all the


programming languages. Here, the decisions are made on the validity of the
particular conditions. Condition checking is the backbone of decision
making.

Statement Description

If Statement The if statement is used to test a specific condition. If the


condition is true, a block of code (if-block) will be
executed.

If - else The if-else statement is similar to if statement except the


Statement fact that, it also provides the block of the code for the false
case of the condition to be checked. If the condition
provided in the if statement is false, then the else statement
will be executed.

Nested if Nested if statements enable us to use if ? else statement


Statement inside an outer if statement.

Indentation in Python

In Python, indentation is used to declare a block. If two statements are at


the same indentation level, then they are the part of the same block.

Generally, four spaces are given to indent the statements which are a
typical amount of indentation in python.

Indentation is the most used part of the python language since it declares
the block of code. All the statements of one block are intended at the same level
indentation.

The if statement

Syntax:

if expression:
statement
Eg 1:-
#if statement
a=int(input("Enter the value: "))
if a%2==0:
print("THe number is even")
else:
print("it is odd")

Eg 2:-
a = int(input("Enter a? "))
b = int(input("Enter b? "))
c = int(input("Enter c? "))
if a>b and a>c:
print("a is largest")
if b>a and b>c:
print("b is largest")
if c>a and c>b:
print("c is largest

The if-else statement

Syntax:

if condition:
#block of statements
else:
#another block of statements (else-block)

Eg 1:-
a=10
b=20
if a>b:
print("A is greater")
else:
print("B is greater")

Eg 2:-
age = int (input("Enter your age? "))
if age>=18:
print("You are eligible to vote !!")
else:
print("Sorry! you have to wait !!")

The elif statement

The elif statement enables us to check multiple conditions and execute the
specific block of statements depending upon the true condition among them. We
can have any number of elif statements in our program depending upon our
need.
Syntax:
if expression 1:
# block of statements

elif expression 2:
# block of statements

elif expression 3:
# block of statements

else:
# block of statements
Eg 1:-
number = int(input("Enter the number?"))
if number==10:
print("number is equals to 10")
elif number==50:
print("number is equal to 50");
elif number==100:
print("number is equal to 100");
else:
print("number is not equal to 10, 50 or 100");

Eg 2:-
marks = int(input("Enter the marks? "))

if marks > 85 and marks <= 100:


print("Congrats ! you scored grade A ...")

elif marks > 60 and marks <= 85:


print("You scored grade B + ...")

elif marks > 40 and marks <= 60:


print("You scored grade B ...")

elif (marks > 30 and marks <= 40):


print("You scored grade C ...")

else:
print("Sorry you are fail ?")
Eg 3:-
choice=int(input("[Link] Withdrawl\n [Link]\n [Link] Pin\n [Link]
Address\n Enter Your Choice: "))
if choice==1:
print("Cash Withdrawl")
elif choice==2:
print("Depoosit")
elif choice==3:
print("Change Pin")
elif choice==4:
print("Change Address")
else:
print("Invalid Choice")

Nested If Statement:

Eg :

u=input("Enter the User Name: ")

if u=="Livewire":

print("Username is correct")

j=input("Enter the Password: ")

if j=="Lw":

print("Password is correct")

else:

print("Password is incorrect")

else:
print("Username and Password is not available in the database")

You might also like