0% found this document useful (0 votes)
15 views

Control Statements in Python

Uploaded by

247reignvibes
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)
15 views

Control Statements in Python

Uploaded by

247reignvibes
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
You are on page 1/ 14

Control statements in Python

#Write a program to check a number is even or


odd
num=int(input("enter a number"))
if num%2==0:
print("number is even",num)
else:
print("number is odd",num)
#Commision calculation
sal=int(input("enter sales amount"))
if sal>50000:
comm=sal*10/100
else:
comm=0
print("commission amount is ",comm)
The elif Statement
If your programs needs to check long list of
conditions then you need to use if-elif-else
statements. You can add as many elif condition as
the programs demands.

if <conditional expression>:
elif <conditional expression>:
elif <conditional expression>:
elif <conditional expression>:
else:
# Reads two numbers and arithmetic operator and display the
result.
num1=int(input("enter first number"))
num2=int(input("enter second number"))
op=input("enter arithmetic operator-\"+,- ,*,/\"")
if op=='+':
result=num1+num2
elif op=='-':
result=num1-num2
elif op=='*':
result=num1*num2
elif op=='/':
result=num1/num2
else:
result="please enter correct arithmetic operator"
print(num1,op,num2,"=",result)
Python range() Basics
range() function allows user to generate a series of
numbers within a given range.
range() takes mainly three arguments.
• start: integer starting from which the sequence of
integers is to be returned
• stop: integer before which the sequence of
integers is to be returned.
The range of integers end at stop – 1.
• step: integer value which determines the
increment between each integer in the sequence
• Syntax (lower limit , upper limit , step value)
When user call range() with one argument, user
will get a series of numbers that starts at 0 and
includes every whole number up to, but not
including, the number that user have provided
as the stop. For Example –
range(start, stop)
range(start, stop, step)
Operators in and not in
in
Example : 3 in [1,2,3,4,5]
Will return true as 3 is contained in the list
not in
Example: 6 not in [1,2,3,4,5]
Will return false as value 6 is not contained in
the list.
Iteration /looping statements
The iteration statements or repetition statements
allow a set of instructions to be performed
repeatedly until a certain condition is fulfilled.
Python provides two types of loops : for loop and
while loop
Categories of loops
Counting loop The loops that repeats , a certain
number of times.
Conditional loop The loops that repeats , until a
certain thing happen.
For loop
Syntax:
For <var> in <sequence>
Example:
For a in range (1,10)
print(a)
a will be assign each value of range one by one
till the last value-1.

You might also like