You are on page 1of 27

Logical operators and loops

if...else statement
• Syntax:
if (logical expression):
statement(s)
else:
statement(s)

• The else statement is optional.


• Example:
ifelse1.py
m=int(input("Enter grades: "))
if(m >=60):
print ("First Division")
else:
print ("Second Division")

Output:
Enter grades: 75
First Division
Enter grades: 50
Second Division
The comparison operators
Operator Meaning
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
• Example:
ifelse2.py

m=int(input("Enter grades: "))


if(m >=60):
print ("First Division")
else:
if(m >=45):
print ("Second Division")
else:
print ("Third Division")

Output:
Enter grades: 75
First Division
Enter grades: 50
Second Division
Enter grades: 40
Third Division
if-elif-else statement
• Syntax:
if (logical expression):
statement(s)
elif (logical expression 1):
statement(s)
[elif (logical expression n):
statement(s)]
else:
statement(s)
• Example
ifelif.py
m=int(input("Enter grades: "))
if(m >=60):
print ("First Division")
elif (m >=45):
print ("Second Division")
else:
print ("Third Division")
Logical Operators
Logical Operator Description
and The logical expression connected with the
AND logical operator returns true if all the
logical expressions evaluate to true.
or The logical expression connected with the
or logical operator returns true if any of
the logical expressions evaluates to true.
not The logical expression preceded by the
not logical operator is negated.
That is, the logical expression that
evaluates to true becomes false when
preceded by the logical not operator and
vice versa.
• Example:
ifelse3.py
m=int(input("Enter grades: "))
if(m >=60):
print ("First Division")
if(m >=45 and m<60):
print ("Second Division")
if (m<45):
print ("Third Division")
Chaining Comparison Operators
• Consider the following comparison operators,
which are connected with the AND operator:
x<=y and y<=z
• These comparison operators can be chained
as:
x<= y <=z
• Example:
ifelschaining.py
m=int(input("Enter grades: "))
if(m >=60):
print ("First Division")
if(45 <= m <60):
print ("Second Division")
if(m<45):
print ("Third Division")
• Another example:
opr1.py
m=int(input("Enter a number between 1 and 10:
"))
if 1<= m <=10:
print ("Number is within range")
else:
print ("Number is out of range")

Output:
Enter a number between 1 and 10: 3
Number is within range
Enter a number between 1 and 10: 15
Number is out of range
The while Loop
Syntax:
while expression :
statement1
statement2
statement3

• Python uses indentation to express the block structure of a


program.
• Unlike other languages, Python does not use braces or
begin/end delimiters to denote blocks.
• Instead it uses indentation to represent blocks of statements.
• Example:
whileloop.py
k=1
while k <=10 :
print (k)
k=k+1

Output:
1
2
3
4
5
6
7
8
9
10
The break Statement
• The break statement terminates and exits
from the current loop and resumes execution
of the program from the statement following
the loop.
• It is typically used in an infinite loop.
• Example:
breakex1.py
k=1
while 1 :
print (k)
k=k+1
if(k>10):
break
Output:
1
2
3
4
5
6
7
8
9
10
The continue Statement
• The continue statement stops execution of the
current iteration by skipping the rest of the
loop and continuing to execute the loop with
the next iterative value.
• Example:
continueex.py
k=1
while k <=10 :
if k==7:
k+=1
continue
print (k)
k=k+1
Output:
1
2
3
4
5
6
8
9
10
The pass Statement
• The pass statement is used in Python to
indicate an empty block of statements.
• It is also used as a placeholder for code that
you want to write later and acts as a reminder
of where a program can be expanded.
• Example:
Pass_ex1.py

k=1
while k <=10 :
if k==7:
pass
else:
print (k)
k+=1

Output:
1
2
3
4
5
6
8
9
10
The range () Function
• The range() function generates and returns a
sequence of integers and is very commonly
used in looping statements.
• There are three variations of the range()
function, depending on the number of
parameters passed to it:
– range(x): Returns a list whose items are
consecutive integers from 0 (included) to x
(excluded).
– range(x, y): Returns a list whose items are consecutive
integers from x (included) to y (excluded). The result is
an empty list if x is greater than or equal to y.
– range(x, y, step): Returns a list of integers from x
(included) to y (excluded), and the difference between
each successive value is the value defined by step. If
step is less than 0, range counts down from x to y. If 0
is specified as the step value, the range() function
raises an exception. When step is not specified, its
default value is 1.
The for Loop

Syntax:
for iterating_var in sequence:
statement1
statement2
statement3
• Example:
forloop.py
for i in range(1,11):
print (i)

• This prints a sequence of numbers, which are


generated from 1 to 10 using the built-in
range() function.
• Since the value for step is not indicated, the
default value of 1 is the step value.
• Example:
forloop2.py

print ("Odd numbers between 1 and 10 are:")


for i in range(1,11,2):
print (i)

Output:
Odd numbers between 1 and 10 are:
1
3
5
7
9
Membership Operators
Operator Description
in Returns Boolean value true if it finds the
specified variable in the given sequence;
otherwise it returns false.
not in Returns Boolean value true if it does not
find the specified variable in the given
sequence; otherwise it returns false.
• Examples:

ab in abcde—Returns true because the string


ab is found in the string abcde.

2 in (10,3,5,2,1)—Returns true because the


value 2 exists in the tuple.

bob not in ab—Returns true because the


string bob is not found in the string ab.

You might also like