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

Chapter 2

Uploaded by

vidyadevi6996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Chapter 2

Uploaded by

vidyadevi6996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Unit 2

Python Operators and Control


flow Statements

1
Python divides the operators in the following groups:
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Identity operators
•Membership operators
•Bitwise operators

2
Arithmetic Operators
Operator Name Example

+ Addition x+y

- Subtraction x-y

* Multiplication x*y
/ Division x/y

% Modulus x%y

** Exponentiation x ** y

// Floor division x // y

3
Arithmetic Operators Example:
x=5
y=3
print(x + y)
print(x-y)
print(x*y)
print(x/y)
print(x%y)
print(x**y)
print(x//y)
#the floor division // rounds the result
down to the nearest whole number
4
Assignment Operators:
Assignment operators are used to assign values to variables:
Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3
5
Assignment Operators Example:
x=5
x+=3
print(x)
x=5
x-=3
print(x)
x=5
x*=3
print(x)
x=5
x/=3
print(x)
x=5
x%=3
print(x)
6
Comparison Operators
Comparison operators are used to compare two values:

Operator Name Example

== Equal x == y

!= Not equal x != y
> Greater than x>y

< Less than x<y

>= Greater than or equal x >= y


to
<= Less than or equal to x <= y

7
Comparison Operators Example:

x=6
y=4

print(x == y)
print(x!=y)
print(x>y)
print(x>= y)
print(x<y)
print(x<=y)

8
Logical Operators
Logical operators are used to combine conditional statements:

Operator Description Example

and Returns True if both x < 5 and x < 10


statements are true

or Returns True if one of x < 5 or x < 4


the statements is true

not Reverse the result, not(x < 5 and x < 10)


returns False if the
result is true
9
Logical Operators Example:

x=5
print(x > 2 and x < 10)
print(x > 2 or x < 10)
print(not(x > 2 and x < 10))

10
Bitwise Operators
As the name suggests, bitwise operators perform operations at the bit level. These
operators include bitwise AND, bitwise OR, bitwise XOR, and shift operators. Bitwise
operators expect their operands to be of integers and treat them as a sequence of bits.
The truth tables of these bitwise operators are given below.

11

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:

Operator Description Example

is Returns True if both x is y


variables are the
same object

is not Returns True if both x is not y


variables are not the
same object

12
Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Operator Description Example

in Returns True if a x in y
sequence with the
specified value is
present in the object
not in Returns True if a x not in y
sequence with the
specified value is not
present in the object

13
Type Conversion
In Python, it is just not possible to complete certain operations that involves different
types of data. For example, it is not possible to perform "2" + 4 since one operand is an
Exampl and the other is of string type.
integer
e:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
Indentation
Whitespace at the beginning of the line is called indentation. These whitespaces or the
indentation are very important in Python. In a Python program, the leading whitespace
including spaces and tabs at the beginning of the logical line determines the
indentation level of that logical line.
Exampl
e:

15
Conditional Statements
Conditional execution (if…)
CONDITIONAL EXECUTION (IF…)
Syntax:
if condition:
do_something
Condition must be statement that evaluates
to a boolean value (True or False)

16
Example:

a=55
b=500
if b>a:
print("b is greater than a")

Indentation:
Python relies on indentation (whitespace at the beginning of a line) to define
scope in the code. Other programming languages often use curly-brackets {}
for this purpose.
By default Python puts 4 spaces but it can be changed by programmers.
17
If-Else Statement

18
Example
:
age=int(input("Enter the age:"))
if age>=18:
print("You are eligible for vote")
else:
print("You are not eligible for vote")

19
If-elif-else Statement
Python supports if-elif-else statements to test additional conditions apart from the
initial test expression. The if-elif-else construct works in the same way as a usual if-
else statement. If-elif-else construct is also known as nested-if construct.

Example:

20
Example
:
num=int(input("Enter any number:"))
if num==0:
print("The value is equal to zero")
elif num>0:
print("The number is positive")
else:
print("The number is negative")
21
While Loop

22
Example:

i=1
while(i<=10):
print(i)
i=i+1

23
Python end parameter in print()
By default python’s print() function ends with a newline. Python’s print() function comes with a parameter
called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print
statement with any character/string using this parameter.

i=1
while(i<=10):
print(i,end=" ")
i=i+1

24
For Loop
For loop provides a mechanism to repeat a task until a particular condition is True. It is
usually known as a determinate or definite loop because the programmer knows exactly
how many times the loop will repeat.
The for...in statement is a looping statement used in Python to iterate over a sequence of
objects.

25
For Loop and Range() Function
The range() function is a built-in function in Python that is used to iterate over a
sequence of numbers. The syntax of range() is range(beg, end, [step])
The range() produces a sequence of numbers starting with beg (inclusive) and ending
with one less than the number end. The step argument is option (that is why it is placed
in brackets). By default, every number in the range is incremented by 1 but we can
specify a different increment using step. It can be both negative and positive, but not
If range() function is given a single argument, it produces an object with values from 0
zero.
to argument-1. For example: range(10) is equal to writing range(0, 10).
• If range() is called with two arguments, it produces values from the first to the
second. For example, range(0,10).
• If range() has three arguments then the third argument specifies the interval of the
26

sequence produced. In this case, the third argument must be an integer. For example,
Examples:

for i in range(10): Output:


print(i,end=" ") 0123456789

for i in range(1,10): 123456789


print(i,end=" ") 02468

for i in range(0,10,2):
print(i,end=" ")

27
Nested Loops
Python allows its users to have nested loops, that is, loops that can be placed inside
other loops. Although this feature will work with any loop like while loop as well as for
loop.
A for loop can be used to control the number of times a particular set of statements will
be executed. Another outer loop could be used to control the number of times that a
whole loop is repeated.
Example:
Loops should be properly indented to identifyoutput
which statements are contained within
for i in range(5):
each for statement. *****
print() *****
*****
for j in range(5):
*****
print("*",end=" ") ***** 28
The Break Statement
The break statement is used to terminate the execution of the nearest enclosing loop in
which it appears. The break statement is widely used with for loop and while loop. When
compiler encounters a break statement, the control passes to the statement that follows
the loop in which the break statement appears.
Example:

29

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
Examples:

i=1 for i in range(1,10):


while i<=10: if i==5:
if i==5: break
break print(i,end=" ")
print(i,end=" ")
i=i+1
Output
Output 1234
1234
30
The Continue Statement
Like the break statement, the continue statement can only appear in the body of a loop.
When the compiler encounters a continue statement then the rest of the statements in
the loop are skipped and the control is unconditionally transferred to the loop-
continuation
Example: portion of the nearest enclosing loop.

for i in
range(1,10):
if i==5:
continue
print(i,end=" ")

Output
12346789
31

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
write a program to check entered number is prime or not
num=int(input("Enter a number"))
i=2
while(num%i!=0):
i=i+1
if i==num:
print("Prime number")
else:
print("Not prime")

32
Write a program to print following Pattern
1
rows = 6 2 2
3 33
4 444
for num in range(1,rows): 5 5555

for i in range(num):

print(num, end=" ") # print number

# line after each row to display pattern correctly

print(" ")
33
Pass statement
In Python programming, the pass statement is a null statement. The difference between
a comment and a pass statement in Python is that while the interpreter ignores a comment
entirely, pass is not ignored.
However, nothing happens when the pass is executed. It results in no operation (NOP)
Suppose we have a loop or a function that is not implemented yet, but we want to implement
it in the future. They cannot have an empty body. The interpreter would give an error. So, we
use the pass statement to construct a body that does nothing.

Example: Example:
def function(args): class abc:
pass pass
34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy