Professional Documents
Culture Documents
Topices:: Operators Input Function
Topices:: Operators Input Function
Topices:
❖ Operators
❖ input Function
❖ Indentation
❖ Conditional Statements
● If ,
● If- else,
● Nested if-else,
● Looping,
◆ For,
◆ While,
◆ Nested loops
Python - Basic Operators
❖ Python language supports following type of operators.
➢ Arithmetic Operators
➢ Comparision Operators
➢ Logical (or Relational) Operators
➢ Assignment Operators
➢ Bitwise Operators
➢ Conditional Operators
Operat
Description Example
or
& Binary AND Operator copies a bit to the (a & b) will give 12
result if it exists in both operands. which is 0000 1100
| Binary OR Operator copies a bit if it exists (a | b) will give 61
in either operand. which is 0011 1101
^ Binary XOR Operator copies the bit if it is (a ^ b) will give 49
set in one operand but not both. which is 0011 0001
~ Binary Ones Complement Operator is unary (~a ) will give -60
and has the effect of 'flipping' bits. which is 1100 0011
<< Binary Left Shift Operator. The left a << 2 will give 240
operands value is moved left by the which is 1111 0000
number of bits specified by the right
operand.
>> Binary Right Shift Operator. The left a >> 2 will give 15
operands value is moved right by the which is 0000 1111
number of bits specified by the right
operand.
#Python Bitwise Operators:
# &,|,^,~,<<,>>
a=5
b=6
c=a&b
d=a|b
e=a^b
f=~a
g=a<<1 Output:
h=a>>4
4
print(c)
print(d)
7
print(e) 3
print(f) -6
print(g) 10
print(h)
0
Python Logical Operators:
Operat
Description Example
or
and Called Logical AND operator. If both the (a and b) is true.
operands are true then then condition
becomes true.
or Called Logical OR Operator. If any of the (a or b) is true.
two operands are non zero then then
condition becomes true.
not Called Logical NOT Operator. Use to not(a and b) is false.
reverses the logical state of its operand. If a
condition is true then Logical NOT operator
will make false.
# Python Logical Operators
(and,or,not)
a=9
b=8
c=8
d=0 Output:
print((a>b) and (b==c)) True
print((a<b) and (b==c)) False
print((a<b) or (b==c)) True
print((a>b) and (b!=c))
False
print((a>b) and (not(b!=c)))
True
print(not (a))
print(not (d))
False
not (not (d)) True
False
Python Membership Operators:
In addition to the operators discussed previously, Python has
membership operators, which test for membership in a
sequence, such as strings, lists, or tuples.
–Example:
age = int(input("How old are you? "))
print("Your age is", age)
print(60 - age, "years to retirement")
Output:
How old are you? 46
Your age is 46
14 years to retirement
Indentation
Python uses indentation to define control and loop constructs. Indentation in Python
refers to the (spaces and tabs) that are used at the beginning of a statement. The
statements with the same indentation belong to the same group called a
suite.Python uses the colon symbol (:) and indentation for showing where blocks
of code begin and end. That is, blocks in Python, such as functions, loops, if clauses
and other constructs, have no ending identifiers. All blocks start with a colon and
then contain the indented lines below it.
if expression: if expression:
statement-1 statement(s)
statement-2 else:
statement-3 statement(s)
Indentation and If statement in Python
#Indentation in Python
no=1
a=5
b=8
if no==1:
print("I will print one number")
print(a)
if no==2:
print("I will print two numbers")
print(a)
print(b)
print('end')
IF …. ELSE Statement
no=2
a=5
b=8
if no==1:
print("I will print one number")
print(a)
else:
print("I will print two numbers")
print(a)
print(b)
print('end')
OUTPUT:
I will print two numbers
5
8
end
if...elif...else Construct
Syntax of if...elif...else
num = 3
if expression:
Body of if
# Try these two variations as well:
elif expression: # num = 0
Body of elif # num = -2.5
else:
Body of else
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
# Nested IF
’’’test using positive/ negative number and zero’’’
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
The Nested if...elif...else Construct
Example:
var = 100
if var < 200:
print ("Expression value is less than 200")
if var == 150:
print ("Which is 150")
elif var == 100:
print ("Which is 100")
elif var == 50:
print ("Which is 50")
elif var < 50:
print ("Expression value is less than 50")
else:
print ("Could not find true expression")
expression=1
if ( expression == 1 ) : print('Value of expression is 1')
5. Python - while Loop
Statements
• The while loop is one of the looping constructs available in Python. The
while loop continues until the expression becomes false. The
expression has to be a logical expression and must return either a true
or a false value
The syntax of the while loop is:
while expression:
statement(s)
Example:
i = 1
while i < 6:
print(i)
i += 1
output:
1
2
3
4
5
The Infinite Loops:
• You must use caution when using while loops because of the
possibility that this condition never resolves to a false value.
This results in a loop that never ends. Such a loop is called an
infinite loop.
• An infinite loop might be useful in client/server programming
where the server needs to run continuously so that client
programs can communicate with it as and when required.
Output:
0
1
2
3
4
Breaking from loop
The continue Statement:
• The continue statement in Python returns the control to the
beginning of the loop. The continue statement rejects all
the remaining statements in the current iteration of the loop
and moves the control back to the top of the loop.
Output: Output:
0 0
1
1
2
2 done
done
The pass Statement:
• pass is a null statement for when a statement is required by
Python syntax (such as within the body of a for or
• while loop), but no action is required or desired by the
programmer. This can be useful as a placeholder for code
• that is yet to be written.
•
Example:
x=3
for x in range(10): y=5
pass
while x==y:
pass
Thanks for today