Control Statements in Python
Control Statements in Python
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.