0% found this document useful (0 votes)
1 views14 pages

Python - Advanced-Conditional Statements

The document provides an overview of conditional statements in Python, explaining their role in decision-making within programs. It covers the syntax and usage of 'if', 'if...else', and 'if...elif...else' statements, along with practical examples and programs to illustrate their application. Additionally, it touches on variable assignment and string concatenation.

Uploaded by

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

Python - Advanced-Conditional Statements

The document provides an overview of conditional statements in Python, explaining their role in decision-making within programs. It covers the syntax and usage of 'if', 'if...else', and 'if...elif...else' statements, along with practical examples and programs to illustrate their application. Additionally, it touches on variable assignment and string concatenation.

Uploaded by

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

Grade 7

Python - Advanced

Conditional Statements

In programming languages, conditional statements also known as decision-making


statements cause the program control to transfer to specific location spending on the
outcome of the conditional expression. Every decision involves a choice between two
alternatives, ‘True’ and ‘False’. If the conditional statement is true, then one set of
statements is executed, otherwise the other set of statements is executed.
In programming, conditional or decision-making statements help to make a decision based
on a condition. The output or result of the program depends on the outcome of the condition.

Let us take an example. You need to have a valid driving licence for driving any vehicle. To
apply for a driving licence, your age should be greater than or equal to 18.

Condition Plan of Action


If your age should be greater than or equal You are eligible to apply for a driving
to 18. licence.
If your age is less than 18 You are not eligible to apply for a driving
licence.

Before you start writing programs based on conditions, it is important to understand


how conditional programming works. In conditional programming, the most important
element is the condition on which the decision is based. The conditional statements check the
condition and execute the statements accordingly. These statements can be represented in
many forms:

Conditional statements include:

● if statement
● if…else statement
● if…elif…else statement

1) if Statement
The If statement is used when you have to evaluate only one condition. It performs a course of
action if the condition evaluates to true, it skips the statements.
For example, your parents allow you to go out for playing only if you complete your homework.
Syntax:
If <condition>:
Statement 1
Test False
Statement 2 Expression
…………….
Remember after the if condition, there is a colon True
and the condition body starts with an indentation
of a tab space. Body of if

Statement just below if

Program 1:
# how to use an if statement to check if a number is positive.
num = 5
if num > 0:
print("The number is positive.")

The if statement is used to test a specific condition. If the condition is true, the block of code
under the if section will be executed.
2) if….else Statement
The if…. else control statement is used when either of the two different actions to be
performed depending upon the result of the conditional expression. It contains two blocks of
statements. If the conditional expression evaluates to true, the statements in the if block get
executed, and if the result is false, then the statements in the else block get executed.
For example, you can go out to play if it does not rain, or else you have to play indoor
games.
Syntax:
If <condition>:
Statement Set 1
else:
Statement set 2

This statement is the same as the if statement.


The only difference is that this statement also
implements the code for the condition that does
not hold true. This means that when the
statement related to a condition is:

● true, then the code under the if section gets executed.


● false, the code under the else section gets executed.

Program 2:
#Write a program in python to check whether the number accepted by user is positive or
negative.
num=int(input("Enter a number: "))
if num>0:
print(num," is a positive number.")
else:
print(num," is a negative number.")

Program
Output

3) if …elif…else Statements
Sometimes you need to work with multiple conditions. In this case, using only if-else
construct does not serve the purpose. The if…elif…else statements provide a compact way to
perform multiple tests on a condition.
For example, when you visit a bank, you go to the counter according to service you want to
avail. If you want to deposit cash, you go to counter 1. If you want to inquire about the
check, you go to counter 2. If you have to inquire for a savings plan, you go to counter 3 and
so on. Similar concept can be implemented in Python using if…elif…else statements.

Syntax:
If <condition 1>:
Statement Set 1
elif < condition 2>:
Statement Set 2
elif < condition 3>:
Statement Set 3
else:
Statement set 4
Program 3:
#Write a program in python to check whether the number accepted by user is zero, positive
or negative.
num=int(input("Enter a number: "))
if num>0:
print(num," is a positive number.")
elif num==0:
print(num," is zero.")
else:
print(num," is a negative number.")
__________________________________________________________________________

Program

Output
Note :

In Python, once an if, if-else, or if-elif-else block has finished executing, program control
automatically continues with the next statement outside the block. If there are no statements
following the block, the program terminates.

Example:

x=0
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
print("Finished evaluation")

Output:

Zero
Finished evaluation

Practice programs
Program 4:
"""Write a code in python to check whether the entered number accepted by user
is even or odd."""
num=int(input("Enter a number: "))
if (num%2)==0:
print(num," is even number.")
else:
print(num," is odd number.")
__________________________________________________________________________
Program

Output
Program 5:
"""Write a code in python to enter a two-digit number and check whether it is less than 10,
less than 50 or greater than 50."""

num=int(input("Enter a two-digit number: "))


print("the entered number is: ",num)
if num<10:
print("The number is less than 10.")
elif num<=50:
print("The number is less than or equal to 50")
else:
print("The number is greater than 50.")
_________________________________________________________________________

Program

Output
Program 6:
"""Design a calculator in python. Ask the user to input two numbers
and also ask the operation to be performed.
For example, the following statements should appear on screen:
Press 1 for addition
Press 2 for subtraction
Press 3 for multiplication
Press any number other than 1,2&3 for division.
If the user enters 1, the program should perform addition and so on.
Hint: Use if, elif and else statements. """

n1=int(input("Enter number 1:"))


n2=int(input("Enter number 2:"))
print(" Press 1 for addition \n Press 2 for subtraction \n Press 3 for multiplication \n Press
any number other than 1,2&3 for division")
op=int(input("Please enter your choice for operation: "))
if op==1:
add=n1+n2
print("Addition of ",n1," and ",n2,"is: ",add)
elif op==2:
sub=n1-n2
print("Subtraction of ",n1," and ",n2,"is: ",sub)
elif op==3:
mul=n1*n2
print("Multiplication of ",n1," and ",n2,"is: ",mul)
else:
div=n1/n2
print("Division of ",n1," and ",n2,"is: ",div)
Program
Output
{

Output
Program 7:
#Write a code in python to assign a letter grade based on a numerical score.

score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print("Your grade is:", grade)

_______________________________________________________________________

Program

Output
Program 8:
""" To find the greatest of 3 numbers without AND operator"""
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
c=int(input("Enter third number: "))
if a>b:
if a>c:
print(a,"is the largest number.")
else:
print(c," is the largest number.")
else:
if b>c:
print(b," is the largest number.")
else:
print(c,"is the largest number.")
__________________________________________________________________________

Program
Output

Program 9:
""" To check whether the number is positive, negative or zero with if-else statement"""
num = 8
if num >= 0:
if num == 0:
print("zero")
else:
print("Positive number")
else:
print("Negative number")
Program 10

Program 11

Assigning Different Values to Multiple Variables


You can assign different values to multiple variables by separating the variables and their
corresponding values with commas. The number of variables must match the number of
values; otherwise, a ValueError will occur.

Syntax:
var1, var2, var3 = value1, value2, value3
Example:

x, y, name = 10, 20, "Python"


print(x) # Output: 10
print(y) # Output: 20
print(name) # Output: Python
Assigning the Same Value to Multiple Variables
You can assign the same value to multiple variables using consecutive
assignment operators (=).
Syntax:
var1 = var2 = var3 = value
Example:
x=y=z=0
print(x, y, z)
Output: 0 0 0

String Concatenation:
Using the + Operator: The + operator is the simplest way to combine two or a few strings.
str1 = "Hello"
str2 = " World!"
result = str1 + str2
print(result)
Output: Hello World!

*****

You might also like