SlideShare a Scribd company logo
1
Unit 2
Python Operators and Control flow
Statements
2
Python divides the operators in the following groups:
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Identity operators
•Membership operators
•Bitwise operators
3
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
4
#the floor division // rounds the result down to
the nearest whole number
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)
Arithmetic Operators Example:
5
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
6
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)
7
Comparison Operators
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Comparison operators are used to compare two values:
8
x = 6
y = 4
print(x == y)
print(x!=y)
print(x>y)
print(x>= y)
print(x<y)
print(x<=y)
Comparison Operators Example:
9
Logical Operators
Operator Description Example
and Returns True if both
statements are true
x < 5 and x < 10
or Returns True if one of the
statements is true
x < 5 or x < 4
not Reverse the result, returns
False if the result is true
not(x < 5 and x < 10)
Logical operators are used to combine conditional statements:
10
x = 5
print(x > 2 and x < 10)
print(x > 2 or x < 10)
print(not(x > 2 and x < 10))
Logical Operators Example:
11
Bitwise Operators
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
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.
12
Identity Operators
Operator Description Example
is Returns True if both
variables are the same
object
x is y
is not Returns True if both
variables are not the same
object
x is not y
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:
13
Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Operator Description Example
in Returns True if a sequence
with the specified value is
present in the object
x in y
not in Returns True if a sequence
with the specified value is
not present in the object
x not in y
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 integer and the other is of string type.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Example:
15
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.
Example:
16
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)
17
Example:
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.
a=55
b=500
if b>a:
print("b is greater than a")
18
If-Else Statement
19
Example:
age=int(input("Enter the age:"))
if age>=18:
print("You are eligible for vote")
else:
print("You are not eligible for vote")
20
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:
21
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")
Example:
22
While Loop
23
Example:
i=1
while(i<=10):
print(i)
i=i+1
24
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
25
For Loop
For loop provides a mechanism to repeat a task until a particular condition isTrue. 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.
26
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 zero.
If range() function is given a single argument, it produces an object with values from 0 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 sequence produced.
In this case, the third argument must be an integer. For example, range(1,20,3).
27
Examples:
for i in range(10):
print(i,end=" ")
for i in range(1,10):
print(i,end=" ")
for i in range(0,10,2):
print(i,end=" ")
Output:
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
0 2 4 6 8
28
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.
Loops should be properly indented to identify which statements are contained within each for statement.
Example:
for i in range(5):
print()
for j in range(5):
print("*",end=" ")
output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
29
The Break Statement
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
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:
30
Examples:
i=1
while i<=10:
if i==5:
break
print(i,end=" ")
i=i+1
Output
1 2 3 4
for i in range(1,10):
if i==5:
break
print(i,end=" ")
Output
1 2 3 4
31
The Continue Statement
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
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 portion of the nearest enclosing loop.
Example:
for i in
range(1,10):
if i==5:
continue
print(i,end=" ")
Output
1 2 3 4 6 7 8 9
32
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")
33
Write a program to print following Pattern
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
rows = 6
for num in range(1,rows):
for i in range(num):
print(num, end=" ") # print number
# line after each row to display pattern correctly
print(" ")
34
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:
def function(args):
pass
Example:
class abc:
pass
Ad

More Related Content

Similar to Chapter 2-Python and control flow statement.pptx (20)

Python programing
Python programingPython programing
Python programing
hamzagame
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
PhanMinhLinhAnxM0190
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
RohitSindhu10
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
AkashdeepBhattacharj1
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
Mohammed Sikander
 
1_Python Basics.pdf
1_Python Basics.pdf1_Python Basics.pdf
1_Python Basics.pdf
MaheshGour5
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
SahajShrimal1
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
Siddique Ibrahim
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
Dipankar Achinta
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
Green Ecosystem
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
R.K.College of engg & Tech
 
“Python” or “CPython” is written in C/C+
“Python” or “CPython” is written in C/C+“Python” or “CPython” is written in C/C+
“Python” or “CPython” is written in C/C+
Mukeshpanigrahy1
 
Token and operators
Token and operatorsToken and operators
Token and operators
Samsil Arefin
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
Guru Nanak Dev University, Amritsar
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
Devashish Kumar
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
AKANSHAMITTAL2K21AFI
 
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfBasic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Computer Programmer
 
Python programing
Python programingPython programing
Python programing
hamzagame
 
1_Python Basics.pdf
1_Python Basics.pdf1_Python Basics.pdf
1_Python Basics.pdf
MaheshGour5
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
SahajShrimal1
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
Dipankar Achinta
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
Green Ecosystem
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
“Python” or “CPython” is written in C/C+
“Python” or “CPython” is written in C/C+“Python” or “CPython” is written in C/C+
“Python” or “CPython” is written in C/C+
Mukeshpanigrahy1
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
Devashish Kumar
 
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfBasic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Computer Programmer
 

Recently uploaded (20)

introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
Ad

Chapter 2-Python and control flow statement.pptx

  • 1. 1 Unit 2 Python Operators and Control flow Statements
  • 2. 2 Python divides the operators in the following groups: •Arithmetic operators •Assignment operators •Comparison operators •Logical operators •Identity operators •Membership operators •Bitwise operators
  • 3. 3 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
  • 4. 4 #the floor division // rounds the result down to the nearest whole number 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) Arithmetic Operators Example:
  • 5. 5 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
  • 6. 6 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)
  • 7. 7 Comparison Operators Operator Name Example == Equal x == y != Not equal x != y > Greater than x > y < Less than x < y >= Greater than or equal to x >= y <= Less than or equal to x <= y Comparison operators are used to compare two values:
  • 8. 8 x = 6 y = 4 print(x == y) print(x!=y) print(x>y) print(x>= y) print(x<y) print(x<=y) Comparison Operators Example:
  • 9. 9 Logical Operators Operator Description Example and Returns True if both statements are true x < 5 and x < 10 or Returns True if one of the statements is true x < 5 or x < 4 not Reverse the result, returns False if the result is true not(x < 5 and x < 10) Logical operators are used to combine conditional statements:
  • 10. 10 x = 5 print(x > 2 and x < 10) print(x > 2 or x < 10) print(not(x > 2 and x < 10)) Logical Operators Example:
  • 11. 11 Bitwise Operators © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 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.
  • 12. 12 Identity Operators Operator Description Example is Returns True if both variables are the same object x is y is not Returns True if both variables are not the same object x is not y 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:
  • 13. 13 Membership Operators Membership operators are used to test if a sequence is presented in an object: Operator Description Example in Returns True if a sequence with the specified value is present in the object x in y not in Returns True if a sequence with the specified value is not present in the object x not in y
  • 14. 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 integer and the other is of string type. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Example:
  • 15. 15 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. Example:
  • 16. 16 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)
  • 17. 17 Example: 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. a=55 b=500 if b>a: print("b is greater than a")
  • 19. 19 Example: age=int(input("Enter the age:")) if age>=18: print("You are eligible for vote") else: print("You are not eligible for vote")
  • 20. 20 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:
  • 21. 21 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") Example:
  • 24. 24 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
  • 25. 25 For Loop For loop provides a mechanism to repeat a task until a particular condition isTrue. 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.
  • 26. 26 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 zero. If range() function is given a single argument, it produces an object with values from 0 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 sequence produced. In this case, the third argument must be an integer. For example, range(1,20,3).
  • 27. 27 Examples: for i in range(10): print(i,end=" ") for i in range(1,10): print(i,end=" ") for i in range(0,10,2): print(i,end=" ") Output: 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0 2 4 6 8
  • 28. 28 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. Loops should be properly indented to identify which statements are contained within each for statement. Example: for i in range(5): print() for j in range(5): print("*",end=" ") output * * * * * * * * * * * * * * * * * * * * * * * * *
  • 29. 29 The Break Statement © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 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:
  • 30. 30 Examples: i=1 while i<=10: if i==5: break print(i,end=" ") i=i+1 Output 1 2 3 4 for i in range(1,10): if i==5: break print(i,end=" ") Output 1 2 3 4
  • 31. 31 The Continue Statement © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 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 portion of the nearest enclosing loop. Example: for i in range(1,10): if i==5: continue print(i,end=" ") Output 1 2 3 4 6 7 8 9
  • 32. 32 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")
  • 33. 33 Write a program to print following Pattern 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 rows = 6 for num in range(1,rows): for i in range(num): print(num, end=" ") # print number # line after each row to display pattern correctly print(" ")
  • 34. 34 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: def function(args): pass Example: class abc: pass