Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Unit-2

Operators, Decision
Making and Loops

Prepared By:-
Prof. Chaitanya Kale
Types of Operators

• Operators are used to perform operations on variables and


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

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python Arithmetic Operators
• Arithmetic operators are used with numeric values to perform
common mathematical operations:

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python Assignment Operators
• Assignment operators are used to assign values to variables:

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python Comparison Operators
• Comparison operators are used to compare two values:

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python Logical Operators
• Logical operators are used to combine conditional statements:

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python 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:

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python Membership Operators

• Membership operators are used to test if a sequence is presented


in an object:

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python Bitwise Operators

• Bitwise operators are used to compare (binary) numbers:

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python If ... Else

• Python Conditions and If statements


• Python supports the usual logical conditions from mathematics:
– Equals: a == b
– Not Equals: a != b
– Less than: a < b
– Less than or equal to: a <= b
– Greater than: a > b
– Greater than or equal to: a >= b
• These conditions can be used in several ways, most commonly in "if
statements" and loops.
• An "if statement" is written by using the if keyword.

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python If ... Else

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
if statement

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Elif Statement

The elif keyword is pythons way of saying "if the previous conditions
were not true, then try this condition“.

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Else Statement

The else keyword catches anything which isn't caught by the preceding
conditions.

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Nested If Statement

You can have if statements inside if statements, this I


called nested if statements.

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python Loops
• Python has two primitive loop commands:
– while loops
– for loops

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
While Loop
With the while loop we can execute a set of statements as long as
a condition is true.

The break Statement


With the break statement we can stop the loop even if the while
condition is true:

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
The continue Statement

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python For Loops
• A for loop is used for iterating over a sequence (that is either a list, a
tuple, a dictionary, a set, or a string).
• This is less like the for keyword in other programming languages, and
works more like an iterator method as found in other object-orientated
programming languages.
• With the for loop we can execute a set of statements, once for each
item in a list, tuple, set etc.

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python For Loops Example

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
For loop Using range() function

• The range() function


• The range() function is used to generate the sequence of the
numbers. If we pass the range(10), it will generate the numbers
from 0 to 9.
• The syntax of the range() function is given below.
– range(start,stop,step size)

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
For loop Using range() function

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python break statement
• The break is a keyword in python which is used to bring the
program control out of the loop.
• The break statement breaks the loops one by one, i.e., in the case of
nested loops, it breaks the inner loop first and then proceeds to
outer loops.
• In other words, we can say that break is used to abort the current
execution of the program and the control goes to the next line after
the loop.
• The break is commonly used in the cases where we need to break
the loop for a given condition.
• The syntax of the break is given below
– #loop statements
– break;

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python break statement Example

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
Python Pass Statement
In Python, the pass keyword is used to execute nothing; it means,
when we don't want to execute code, the pass can be used to execute
empty. It is the same as the name refers to. It just makes the control to
pass by without executing any code. If we want to bypass any code
pass statement can be used.

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale
References

1. Python Programming John M Zelle Franklin Beedle-2018.


2. Python Machine Learning: Machine Learning and Deep Learning
with Python Sebastian Raschka Vahid Mirjalili scikit-learn, and
TensorFlow”, Packt- 20173

Unit-2 Operators, Decision Making and Loops Prepared by:- Prof. Chaitanya Kale

You might also like