0% found this document useful (0 votes)
0 views29 pages

Chapter 3

This document covers basic operators and control structures in Python, including arithmetic, comparison, assignment, logical, bitwise, and membership operators. It also explains conditional statements, such as if, elif, and else, as well as looping constructs like while and for loops, including control statements like break and continue. Additionally, it provides examples and a quiz to reinforce the concepts discussed.

Uploaded by

mejidkingo
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)
0 views29 pages

Chapter 3

This document covers basic operators and control structures in Python, including arithmetic, comparison, assignment, logical, bitwise, and membership operators. It also explains conditional statements, such as if, elif, and else, as well as looping constructs like while and for loops, including control statements like break and continue. Additionally, it provides examples and a quiz to reinforce the concepts discussed.

Uploaded by

mejidkingo
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/ 29

CHAPTER -THREE

Basic Operators
&
Control Structures
Python 3 – Basic Operators
 Operators are the constructs, which can manipulate the value
of operands.
 Python language supports the following types of operators
i. Arithmetic Operators
ii. Comparison (Relational) Operators
iii. Assignment Operators
iv. Logical Operators
v. Bitwise Operators
vi. Membership Operators

2
Cont’d …
Python Arithmetic Operators
 Assume variable a holds the value 10 and variable b holds the
value 21, then

3
Cont’d …
Python Comparison Operators
 These operators compare the values on either side of them and
decide the relation among them.
 They are also called Relational operators.
 Assume variable a holds the value 10 and variable b holds the value
20, then

4
Cont’d …

5
Cont’d …
Python Assignment Operators
 Assume variable a holds 10 and variable b holds 20, then-

6
Cont’d …
Python Bitwise Operators
 Bitwise operator works on bits and performs bit-by-bit operation.
 Assume if a = 60; and b = 13; Now in binary format they will be as follows
a= 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
 Python's built-in function bin() can be used to obtain binary
representation of an integer number.

7
Cont’d …
Python Logical Operators
 The following logical operators are supported by Python
language.
 Assume variable a holds True and variable b holds False then

8
Cont’d …
Python Membership Operators
 Python’s membership operators test for membership in a
sequence, such as strings, lists, or tuples.
 There are two membership operators as explained below-

9
Cont’d …

10
Conditionals

 In Python, True and False are Boolean objects of class 'bool' and they are immutable.
 Python assumes any non-zero and non-null values as True, otherwise it is False value.
 Python does not provide switch or case statements as in other languages.
 Syntax:
if Statement if..else Statement if..elif..else Statement

 Example:

11
Conditionals

 Using the conditional expression


Another type of conditional structure in Python, which is very convenient and easy to read.

12
Cont’d …

13
Cont’d …
The elif Statement
 The elif statement allows you to check multiple expressions
for TRUE
 Similar to the else, the elif statement is optional.
 However, unlike else, for which there can be at the most one
statement, there can be an arbitrary number of elif
statements following an if.

14
Cont’d …

15
Cont’d …
Nested IF Statements

16
Python 3 – Loops
 In general, statements are executed sequentially-
 The first statement in a function is executed first, followed by the
second, and so on.
 Python programming language provides the following types of
loops to handle looping requirements

17
Cont’d …
while Loop Statements
 A while loop statement in Python programming language
repeatedly executes a target statement as long as a given
condition is true.

 Here, statement(s) may be a single statement or a block of


statements with uniform indent.
 The condition may be any expression, and true is any non-zero
18
value.
 The loop iterates while the condition is true.
Cont’d …

19
Cont’d …
for Loop Statements
The for statement in Python has the ability to iterate over the items of
any sequence, such as a list or a string.

 The range() function


 The built-in function range() is the right function to iterate over a
sequence of numbers. It generates an iterator of arithmetic
progressions.

20
Cont’d …

21
Cont’d …

22
Loops …
 The For Loop

 The while Loop

23
Loops
Loop Control Statements
 Break :Terminates the statement and transfers execution to the statement immediately
following the loop.

 continue :Causes the loop to skip the remainder of its body and immediately retest
its condition prior to reiterating.

 pass :Used when a statement is required syntactically but you do not want
any command or code to execute.

24
Examples

25
Examples…

26
Examples…

27
QUIZ(10%)
1) Write python code that Accept two numbers from the user and calculate
multiplication ,subtraction and addition.
2) Given a Python list of numbers. Turn every item of a list into its square
List = [1, 2, 3, 4, 5, 6, 7]
Expected Output: [1, 4, 9, 16, 25, 36, 49]
3) Reverse the given” List = [100, 200, 300, 400, 500]” in Python.
Expected Output: [500, 400, 300, 200, 100]
4. Write Python program to insert a number to any position in a list.
for example numbers = [3,4,1,9,6,2,8]
5. Write a program in Python to print number ranging from 1 to 25 but excluding number
which is the multiples of 5.
6. Write a program to filter even and odd number from a list.
Hint 1
Given x = [10, 23, 24, 35, 65, 78, 90] 28
Examples…

29

You might also like