[go: up one dir, main page]

0% found this document useful (0 votes)
21 views20 pages

Conditionals and Iterations

The document discusses logical, identity, membership, and bitwise operators in Python. It also covers conditional execution using if, elif, and else statements to control program flow based on conditions. Examples are provided of taking keyboard input, checking conditions, and writing programs that perform different actions depending on conditional logic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

Conditionals and Iterations

The document discusses logical, identity, membership, and bitwise operators in Python. It also covers conditional execution using if, elif, and else statements to control program flow based on conditions. Examples are provided of taking keyboard input, checking conditions, and writing programs that perform different actions depending on conditional logic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Conditionals

and
Iterations
Logical operators

• There are three logical operators:


 and,
 or
 not
• For example, x > 0 and x < 10 is true only if x is greater than 0 and less
than 10.
• n%2 == 0 or n%3 == 0
• not(x > y) is true if (x > y) is false, that is, if x is less than or equal to y.
Example: and, or, not
Identity operators

• Identity operators compare the memory locations of two objects.


There are two Identity operators as explained below
Example of (is)Identity operators
Example of (is not)Identity operators
Bitwise Operators
Example:-Bitwise Operators
Membership Operators
Example of Membership Operators
Continue…

• Any nonzero number is interpreted as “true."


>>> x = 5
>>> x and 1
1
>>> y = 0
>>> y and 1
0
Keyboard Input
• input(): built in function to get data from keyboard.
• Takes data in the form of string.
• Eg:
>>> input1 = input ()
What are you waiting for?
>>> print input1
What are you waiting for?
•Before calling input, it is a good idea to print a message telling the user what to input. This
message is called a prompt.
•A prompt can be supplied as an argument to input.
• Eg:
>>> name = input ("What...is your name? ")
What...is your name? Arthur, King of the Britons!
>>> print name
Arthur, King of the Britons!
• If we expect the response to be an integer, then type conversion needs
to be done.
• Eg:
prompt = "What is the airspeed velocity of an unladen swallow?"
speed =int(input(prompt))
Conditional Execution

• To write useful programs we need the ability to check conditions and


change the behaviour of the program accordingly.
• Different conditional statements in python are:
– IF
– IF ---Else (Alternative Execution)
– IF--- ELIF---- ELSE (Chained Conditionals)
– Nested Conditionals
If Condition
• If x > 0:
print "x is positive“
• The Boolean expression after the if statement is called the condition. If it is true,
then the indented statement gets executed. If not, nothing happens.
• Structure of If
– HEADER:
FIRST STATEMENT
...
LAST STATEMENT
If Condition
• There is no limit on the number of statements that can appear in the
body of an if statement, but there has to be at least one.
• Occasionally, it is useful to have a body with no statements (usually as a
place keeper for code you haven't written yet). In that case, you can use
the pass statement, which does nothing.
Alternative Execution
• A second form of the if statement is alternative execution, in which there
are two possibilities and the condition determines which one gets executed.
• Eg:
if x%2 == 0:
print x, "is even"
else:
print x, "is odd“
• The alternatives are called branches.
Continue….

• Since the condition must be true or false, exactly one of the


alternatives will be executed. The alternatives are called branches,
because they are branches in the flow of execution.
Chained Conditionals
• Sometimes there are more than two possibilities and we need more
than two branches.
if x < y:
print x, "is less than", y
elif x > y:
print x, "is greater than", y
else:
print x, "and", y, "are equal“
NOTE: There is no limit of the number of elif statements, but the last
branch has to be an else statement
Exercise
1. Write a program that asks for two numbers. If the sum of the numbers is greater
than 100, print "That is a big number" and terminate the program.
2. Write a python program to check whether numbers is divisible by 7 and multiples
of 5,
3. Write a Python program to check a triangle is equilateral, isosceles or
scalene.
Note :An equilateral triangle is a triangle in which all three sides are equal.
A scalene triangle is a triangle that has three unequal sides.
An isosceles triangle is a triangle with (at least) two equal sides.

You might also like