CHAPTER 02 Python

You might also like

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

CHAPTER 02

Write a simple python program for the given arithmetic Write a Python Program using decision making structure for
expressions. two-way branching to solve the given problem.

1 2 3 4

Use Different Types of operators for writing the arithmetic Write a Python Program using decision
expressions. making structure for multi-way branching
to solve the given problem

Course Outcomes
 Arithmetic
 Comparison/Relational
 Assignment
Basic Operators  Logical
 Bitwise
 Membership
 Identity
 Python Operator Precedence
What are Operators in
Python?

 Operators are special symbols in Python that carry out


arithmetic or logical computation.

 The value that the operator operates on is called the


operand.

 For example:

 >>> 2+3

 5

 Here, + is the operator that performs addition. 2 and 3


are the operands and 5 is the output of the operation.
Arithmetic Operators

 Arithmetic operators are used to


perform mathematical operations like
addition, subtraction, multiplication,
etc.

 Assume variable a holds 10 and


variable b holds 20, then −
Python Comparison Operators

 These operators compare the values


on either sides of them and decide
the relation among them. They are
also called Relational operators.

 Comparison operators are used to


compare values. It returns either True
or False according to the condition.

 Assume variable a holds 10 and


variable b holds 20, then −
Assignment operators are used in Python to
assign values to variables.

Python Assignment a = 5 is a simple assignment operator that


Operators assigns the value 5 on the right to the variable a
on the left.

There are various compound operators in


Python like a += 5 that adds to the variable and
later assigns the same. It is equivalent to a = a +
5.
Python Bitwise Operators
 Bitwise operators act on operands as if they
were strings of binary digits. They operate bit
by bit, hence the name.
 For example, 2 is 10 in binary and 7 is 111.
 Following table lists out the bitwise operators
supported by Python language with an
example each in those, we use the above two
variables (a and b) as operands −
 a = 0011 1100
 b = 0000 1101
 -----------------
 a&b = 0000 1100
 a|b = 0011 1101
 a^b = 0011 0001
 ~a = 1100 0011
Python Logical Operators
Logical operators are the and, or, not operators.
A membership operator is used to identify membership in any sequence
(lists, strings, tuples).

in and not in are membership operators.

in returns True if the specified value is found in the sequence. Returns


False otherwise.

not in returns True if the specified value is not found in the sequence.
Returns False otherwise.

Python Membership Operators


Python Identity Operators

 Python language offers some special types of


operators like the identity operator or the
membership operator.

 is and is not are the identity operators in


Python.

 They are used to check if two values (or


variables) are located on the same part of the
memory.

 Two variables that are equal does not imply


that they are identical.
Python Operators Precedence
 Operator precedence affects how an expression is evaluated. Operators in the same box
evaluate left to right
Conditional Statements:

If
Control Flow
If….Else

Nested If
What are control flow statements in Python?
 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.
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.
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.
 Some Decision Control Statements are:
• Simple if
• if-else
• nested if
• if-elif-else
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.
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.
Nested if
Nested if statements are an if statement inside another if statement.
if-elif-else
 The if-elif-else statement is used to conditionally execute a statement or a
block of statements.
AGENDA
Python - Loops
 A loop statement allows us to Python programming language provides following types of loops to
execute a statement or group of handle looping requirements.
statements multiple times.
Sr.No. Loop Type & Description
 The following diagram illustrates a
1 while loop
loop statement −
Repeats a statement or group of statements while a given condition is
TRUE. It tests the condition before executing the loop body.

2 for loop
Executes a sequence of statements multiple times and abbreviates the
code that manages the loop variable.

3 nested loops
You can use one or more loop inside any another while, for or do..while
loop.
What is while loop in
Python?
 A while loop statement in Python
programming language repeatedly
executes a target statement as long as
a given condition is true.
 A while loop in python iterates till its
condition becomes False. In other
words, it executes the statements
under itself while the condition it
takes is True.
Syntax
The syntax of a while loop in Python programming language is −

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


The condition may be any expression, and true is any non-zero value. The loop
iterates while the condition is true.

When the condition becomes false, program control passes to the line
immediately following the loop.

In Python, all the statements indented by the same number of character spaces
after a programming construct are considered to be part of a single block of code.
Python uses indentation as its method of grouping statements.
Using else Statement with While Loop
Python supports to have an else statement associated with a loop statement.
If the else statement is used with a while loop, the else statement is executed when the condition becomes
false.
While loop with else Single Statement
 Same as with for loops, while loops can ➢ Similar to the if statement syntax, if your while
also have an optional else block. clause consists only of a single statement, it
 The else part is executed if the condition in may be placed on the same line as the while
the while loop evaluates to False. header.
 The while loop can be terminated with a
break statement. In such cases, the else
part is ignored. Hence, a while loop's else
part runs if no break occurs and the
condition is false.
 Using else Statement with While Loop
 Python supports to have an else statement
associated with a loop statement.
 If the else statement is used with
a while loop, the else statement is
executed when the condition becomes
false.
What is for loop in Python?
 The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable
objects. Iterating over a sequence is called traversal.
 Syntax of for Loop:
 Here, val is the variable that takes the value of the item inside the sequence on each
iteration.

 Loop continues until we reach the last item in the sequence. The body of for loop is separated
from the rest of the code using indentation.
Flowchart of for Loop

➢ If a sequence contains an expression list, it is evaluated first.

➢ Then, the first item in the sequence is assigned to the

iterating variable iterating_var.

➢ Next, the statements block is executed.

➢ Each item in the list is assigned to iterating_var, and the

statement(s) block is executed until the entire sequence is

exhausted.
Python Nested Loops
 Python programming language allows to use one loop inside another loop. Following
section shows few examples to illustrate the concept.
 Syntax

 The syntax for a nested while loop statement in Python programming language is as
follows −

 A final note on loop nesting is that you can put any type of loop inside of any other type
of loop. For example a for loop can be inside a while loop or vice versa.
LOOP CONTROL STATEMENTS
Python Break and Continue Statement

 Python Break Statement:

 It is sometimes desirable to skip some statements inside


the loop or terminate the loop immediately without
checking the test expression.

 In such cases we can use break statements in Python.

 The break statement allows you to exit a loop from any


point within its body, bypassing its
normal termination expression.
Syntax
 Syntax of break  The working of break statement in for
break loop and while loop is shown below.
Continue Statement

 When the program control reaches the continue statement, it skips the
statements after ‘continue’.
 It then shifts to the next item in the sequence and executes the block of
code for it.
 You can use it with both for and while loops.
 Syntax of Continue
continue
 Flowchart of continue  The working of the continue statement in for and
while loop is shown below.
pass statement
 In Python, we use the pass statement to implement stubs.
 When we need a particular loop, class, or function in our program, but don’t know what
goes in it, we place the pass statement in it.
 It is a null statement. The interpreter does not ignore it, but it performs a no-operation
(NOP).
 Syntax of pass
pass
Thank You

You might also like