Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Chapter-II Python Operators and Control Flow Statement

Python PPT Chapter 2 msbte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Chapter-II Python Operators and Control Flow Statement

Python PPT Chapter 2 msbte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

PROGRAMMING WITH PYTHON

PWP - 22616

UNIT-II
PYTHON OPERATORS
AND
CONTROL FLOW STATEMENT

Mr. Naresh A. Kamble


POINTS TO BE COVERED
• INTRODUCTION
• BASIC OPERATORS
• CONTROL FLOW
• CONDITIONAL STATEMENTS
• LOOPING AND MANIPULATION IN PYTHON

2
INTRODUCTION
• WHAT ARE OPERATORS ?

• Operators are the constructs which can manipulate the value


of operands.

• Consider the expression a = b + c

• Here a, b and c are called as operands and ‘+’, ‘=‘ is called as


operators.

3
4
TYPES OF OPERATORS
• ARITHMETIC OPERATORS

• COMPARISION (RELATIONAL) OPERATORS

• ASSIGNMENT OPERATORS

• LOGICAL OPERATORS

• BITWISE OPERATORS

• MEMBERSHIP OPERATORS

• IDENTITY OPERATORS

• OPERATOR PRECEDENCE
5
TYPES OF OPERATORS
• ARITHMETIC OPERATORS
• Arithmetic operators are used for performing mathematical
operations like
• Addition ( + )
• Subtraction ( - )
• Multiplication ( * )
• Division ( / )
• Modulus ( % )
• Exponent ( ** )
• Floor Division ( // )
6
TYPES OF OPERATORS
• ARITHMETIC OPERATORS

7
TYPES OF OPERATORS
• REPRESENTING ARITHMETIC OPERATORS

8
TYPES OF OPERATORS
• COMPARISION (RELATIONAL) OPERATORS

• Comparison of Relational operators compares the values.

• It either returns True or False according to the condition.

9
TYPES OF OPERATORS
• COMPARISION (RELATIONAL) OPERATORS

10
TYPES OF OPERATORS
• REPRESENTING COMPARISION OPERATORS

11
TYPES OF OPERATORS
• ASSIGNMENT OPERATORS

• Assignment operators are used to assigning values to the


variables.

12
TYPES OF OPERATORS
• ASSIGMENT OPERATORS

13
TYPES OF OPERATORS
• REPRESENTING ASSIGNMENT OPERATORS

14
TYPES OF OPERATORS
• LOGICAL OPERATORS

• Logical operators perform

– Logical AND,

– Logical OR,

– Logical NOT operations.

• It is used to combine conditional statements.

15
TYPES OF OPERATORS
• LOGICAL OPERATORS

16
TYPES OF OPERATORS
• REPRESENTING LOGICAL OPERATORS

17
TYPES OF OPERATORS
• BITWISE OPERATORS

• Bitwise operators act on bits and perform the bit-by-bit


operations.

• These are used to operate on binary numbers (0 AND 1).

18
TYPES OF OPERATORS
• BITWISE OPERATORS

19
TYPES OF OPERATORS
• REPRESENTING BITWISE OPERATORS

20
TYPES OF OPERATORS
• MEMBERSHIP OPERATORS

• Membership operators are in and not in, which are used to


test whether a value or variable is in a sequence.

21
TYPES OF OPERATORS
• MEMBERSHIP OPERATORS

22
TYPES OF OPERATORS
• REPRESENTING MEMBERSHIP OPERATORS

23
TYPES OF OPERATORS
• IDENTITY OPERATORS

• is and is not are the identity operators both are used to check
if two values are located on the same part of the memory.

• Two variables that are equal do not imply that they are
identical.

24
TYPES OF OPERATORS
• IDENTITY OPERATORS

25
TYPES OF OPERATORS
• REPRESENTING IDENTITY OPERATORS

26
TYPES OF OPERATORS
• OPERATOR PRECEDENCE

• Operator Precedence is used in an expression with more than


one operator with different precedence to determine which
operation to perform first.

• Operator Precedence determines the priorities of the


operator.

27
TYPES OF OPERATORS

10 + 20 * 30 is calculated as 10 + (20 * 30)


and not as (10 + 20) * 30 28
TYPES OF OPERATORS
• REPRESENTING OPERATOR PRESEDENCE

29
30
FLOW CONTROL
• A program’s control flow is the order in which the program’s
code executes.

• The control flow of a Python program is regulated by


conditional statements, loops, and function calls.

• Python has three types of control structures:

• Sequential - default mode

• Selection - used for decisions and branching

• Repetition - used for looping, i.e., repeating a piece of code


multiple times.
31
FLOW CONTROL
• 1. SEQUENTIAL

• Sequential statements are a set of statements whose


execution process happens in a sequence.

• The problem with sequential statements is that if the logic has


broken in any one of the lines, then the complete source code
execution will break.

32
FLOW CONTROL
• REPRESENTING SEQUENTIAL FLOW

33
FLOW CONTROL
• 2. SELECTION / DECISION CONTROL STATEMENTS

• In Python, the selection statements are also known as


Decision control statements or branching statements.

• The selection statement allows a program to test several


conditions and execute instructions based on which
condition is true.

34
FLOW CONTROL
• 2. SELECTION / DECISION CONTROL STATEMENTS

• Some Decision Control Statements are:

• Simple if

• if-else

• nested if

• if-elif-else

35
FLOW CONTROL
• SIMPLE IF
• If statements are control flow statements that help us to run a particular
code, but only when a certain condition is met or satisfied. A simple if only
has one condition to check.

Syntax

If test expression:
statement(s)

36
FLOW CONTROL
• REPRESENTING SIMPLE IF

37
FLOW CONTROL
• EXAMPLE
• Write a program using if condition to check password is 123456789. if
password is 123456789, then print “Access Granted”.

• TAKE INPUT FROM USER

38
FLOW CONTROL
• IF-ELSE
• The if-else statement evaluates the condition and will execute the body of
if, if the test condition is True, but if the condition is False, then the body
of else is executed.

Syntax

if test expression:
Body of if
else:
Body of else

39
FLOW CONTROL
• REPRESENTING IF-ELSE

40
FLOW CONTROL
• EXAMPLE
• Write a program using if-else condition to check password is 123456789. if
password is 123456789, then print “Access Granted” else print “Access
Denied”

• TAKE INPUT FROM USER

41
FLOW CONTROL
• NESTED IF
• Nested if statements are an if statement inside another if statement.

42
FLOW CONTROL
• REPRESENTING NESTED-IF

43
FLOW CONTROL
• IF-ELIF-ELSE
• The if-elif-else statement is used to conditionally execute a statement or a
block of statements.

Syntax

if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else

44
FLOW CONTROL
• REPRESENTING IF-ELIF-ELSE

45
FLOW CONTROL
• EXAMPLE
• Write a program using if-elif-else condition to check the number is +ve, -ve
or zero

• TAKE INPUT FROM USER

46
FLOW CONTROL
• 3. REPETATION

• A repetition statement is used to repeat a group(block) of


programming instructions.

• In Python, we generally have two loops/repetitive statements:

• for loop

• while loop

47
FLOW CONTROL
• FOR LOOP
• A for loop is used to iterate over a sequence that is either a list, tuple,
dictionary, or a set. We can execute a set of statements once for each item
in a list, tuple, or dictionary.

Syntax

for item in sequence:


Body of for

48
FLOW CONTROL
• REPRESENTING FOR LOOP

49
FLOW CONTROL
• REPRESENTING FOR LOOP USING range() FUNCTION

50
FLOW CONTROL
• REPRESENTING FOR LOOP WITH ELSE

51
FLOW CONTROL
• EXAMPLES

• Write a program using for loop to display all letters from the
word ‘Program’

• Write a program using for loop and range() function to display


all flowers from a list.

• Write a program using for loop and if-else, to find the location
of the fruit (apple, mango, banana, cherry)
52
FLOW CONTROL
• WHILE LOOP

• In Python, while loops are used to execute a block of


statements repeatedly until a given condition is satisfied.

• Then, the expression is checked again and, if it is still true, the


body is executed again.

• This continues until the expression becomes false.

53
FLOW CONTROL
• WHILE LOOP

Syntax

while test expression:


Body of while

54
FLOW CONTROL
• REPRESENTING WHILE LOOP

55
FLOW CONTROL
• REPRESENTING WHILE LOOP USING ELSE

56
FLOW CONTROL
• EXAMPLES

• Write a program using while loop to find the sum of first 5


natural numbers.

• Write a program using while loop and if-else to display a string


on screen for only one time.

57
FLOW CONTROL
• NESTED LOOPS

• Placing a loop inside another loop is called as nested loop.

• Nested loops can be used for both while and for loop.

Syntax for nested for loop Syntax for nested while loop

for iterating_variable in sequence: while expression:


for iterating_variable in sequence: while expression:
statements(s) statements(s)
statements(s) statements(s)
58
FLOW CONTROL
• REPRESENTING NESTED LOOP USING FOR LOOP

59
FLOW CONTROL
• REPRESENTING NESTED LOOP USING WHILE LOOP

60
FLOW CONTROL
• REPRESENTING NESTED LOOP USING WHILE LOOP

61
62
LOOP MANIPULATION
• CONTROL STATEMENTS

• Control statements change the execution from normal


sequence.

• These are used to terminate the current iteration or even the


whole loop without checking test expression.

63
LOOP MANIPULATION
• CONTROL STATEMENTS

• Python supports 3 control statements

• break

• continue

• pass

64
LOOP MANIPULATION
• BREAK CONTROL STATEMENTS

• The break statement terminates the loop containing it.

• Control of the program flows to the statement immediately


after the body of the loop.

• If the break statement is inside a nested loop (loop inside


another loop), the break statement will terminate the
innermost loop.
65
LOOP MANIPULATION

66
FLOW CONTROL
• REPRESENTING BREAK CONTROL STATEMENT

67
LOOP MANIPULATION

68
FLOW CONTROL
• EXAMPLES

• Write a program for displaying a particular letter from word


‘Python’ using break statement.

69
LOOP MANIPULATION
• CONTINUE CONTROL STATEMENTS

• The continue statement is used to skip the rest of the code


inside a loop for the current iteration only.

• Loop does not terminate but continues on with the next


iteration.

70
LOOP MANIPULATION

71
FLOW CONTROL
• REPRESENTING CONTINUE CONTROL STATEMENT

72
LOOP MANIPULATION

73
FLOW CONTROL
• EXAMPLES

• Write a program for displaying a letters from word ‘Python’


using continue statement.

74
LOOP MANIPULATION
• PASS CONTROL STATEMENTS

• Pass statement is used to write empty loops.

• Pass is also used for empty control statement, function and


classes.

75
FLOW CONTROL
• REPRESENTING PASS CONTROL STATEMENT

76

You might also like