SlideShare a Scribd company logo
1 of 55
Download to read offline
UNIT III CONTROL FLOW,
FUNCTIONS
Presented by,
S.DHIVYA,
Assistant Professor,
Kongunadu College of Engineering and Technology.
Operators
An operator is symbol that specifies an
operation to be performed on the operands.
Types of operator:
1. Arithmetic operator.
2. Relational Operator.
3. Logical Operator.
4. Assignment Operator.
5. Bitwise Operator.
6. Membership Operators
7. Identity Operators
Arithmetic operator
It provides some arithmetic operators which perform the
task of arithmetic operations in Python. Below some
arithmetic operators
+ , - ,* , / , %
Operator Name Example
+ Addition c=a + b
- Subtraction c=a – b
* Multiplication c=a * b
/ Division c=a / b
% Modulus c=a % b
1. Unary arithmetic:
It require one operand.
Example: +a , -b , -7
2. Binary arithmetic:
It require two operands.
Example: A+B
3. Integer arithmetic:
It require both operands are integer numbers.
Example: 10+20
4. Floating point arithmetic
It require both operands are floating point.
Example: 6.5+7.4
Example program:
a = int ( input (“Enter the a value”))
b= int ( input ( “enter the b value”))
sum = (a + b)
print("Sum of two number is :", sum)
These operators compare the values on either sides of them
and decide the relation among them. They are also known as
comparison operators .
Operator Name Example
< less than a < b
<= less than or equal to a <= b
> greater than a > b
>= greater than or equal to a >= b
== is equal to a == b
!= not equal to a != b
Relational operator
Example:
a= int ( input ( “enter a value”)
b= int (input (“enter b value”)
if (a>b):
print(“a is big”)
else:
print(“b is big”)
Logical operators
Python logical operator are used to combine two or
more conditions and perform the logical operations using
Logical AND, Logical OR and Logically NOT.
Logical and
Syntax:
(exp1) and (exp2)
Example:
(a>c) and (a>b)
Logical or
Syntax:
(exp1) or (exp2)
Example:
(a>b) or (a>d)
Logical NOT
29!=29
Example:
a= int ( input ( “enter a value”)
b= int (input (“enter b value”)
c = int (input (“enter c value”)
if (a>b) and (a>c):
print(“a is big”)
elif (b>c):
print(“b is big”)
else:
print(“c is big”)
Assignment operator
Assignment operator used assign a values of a
variable.
Syntax:
variable= expression or value
Example:
a=10 # assigning value 10 to variable a
b=20
c= a+b
print(“sum of two number is”,c)
Bitwise Operator
Bitwise operator used to manipulate the
data at bit level, it operates on integers only.
it not applicable to float or real.
operator meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Shift left
>> Shift right
~ One’s complement
Bitwise AND (&)
Here operate with two operand bit by bit.
Truth table for & is:
Bitwise OR ( |)
Bitwise exclusive OR (^)
& 0 1
0 0 0
1 0 1
| 0 1
0 0 1
1 1 1
^ 0 1
0 0 1
1 1 0
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
in - Evaluates to true if it finds a variable in
the specified sequence and false otherwise.
not in - Evaluates to true if it does not finds a
variable in the specified sequence and false
otherwise
>>> a=[39,24,1.5,33,67,22]
>>> 22 in a
True
>>> 5 not in a
True
Example
>>> s1="welcome"
>>> for word in s1:
print(s1)
Output:
welcome
welcome
welcome
welcome
welcome
welcome
welcome
Identity Operators
Identity operators compare the memory locations of two
objects. There are two Identity operators explained below:
• is
• is not
Example:1
x=20
y=20
if ( x is y):
print (“same identity”)
else:
print (“different identity”)
Example 2:
x=15
y=20
if ( x is not y):
print (“different identity”)
else:
print (“same identity”)
CONDITIONALS
Decision Making and Branching
i. Sequential structure:
Instruction are executed in sequence
i=i+1
j=j+1
ii. Selection structure:
Instructions are executed based on the condition
if(x>y):
i=i+1
else:
j=j+1
iii. Iteration structure (or) loop:
Statements are repeatedly executed. This
forms program loops.
Ex: for i in range(0,5,1):
Print(“hello”)
iv. Encapsulation structure:
It includes other compound structures
Ex: for i in range(0,5,1):
if (condition):
Print(“hello”)
else:
Print(“hai”)
Conditional if statement
If is a decision making statement or control
statement. It is used to control the flow of execution,
also to test logically whether the condition is True or
False.
Syntax:
if (condition is true) : False
True statements True
condition
True statement
Example:
a= int ( input ( “enter a:”))
b= int (input (“enter b:”))
if (a>b):
print(“a is big”)
Output:
enter a: 50
enter b: 10
a is big
Alternative If else statement
It is basically two way decision making statement,
Here it check the condition if the condition is true
means it execute the true statements, if it is false
execute another set of statement.
Syntax: true false
if(condition) :
true statement
else:
false statement
condition
True statement False statement
Example:
a= int ( input ( “enter a value”))
b= int (input (“enter b value”))
if (a>b):
print(“a is big”)
else:
print(“b is big”)
Chained conditionals (or)Nested if
statement
One conditional can nested with another.
Syntax:
if(condition 1) : False
if(condition 2) : True
True statement 2
else: True False
false statement 2
else:
false statement1
Condition
1
Condition
2
true statement 2
False statement
1
False statement
2
Example:
a= int ( input ( “enter a:”))
b= int (input (“enter b:”))
c = int (input (“enter c:”))
if (a>b) and (a>c): output:
print(“a is big”) enter a:20
elif (b>c): enter b: 30
print(“b is big”) enter c: 40
else: c is big
print(“c is big”)
Iteration /Looping statement
The loop is defined as the block of statements which
are repeatedly executed for a certain number of time.
the loop program consist of two parts , one is body of
the loop another one is control statement .
any looping statement ,would include following steps:
1. Initialization of a condition variable
2. Test the condition .
3. Executing body of the statement based on the condition.
4. Updating condition variable
Types:
1. while statement(entry check loop)(top tested loop).
2. for statement.
3. Nested looping.
while statement(entry check loop)
The while loop is an entry controlled loop
statement , means the condition as evaluated
first , if it is true then body of the loop is
executed.
Syntax: False
while(condition):
……. True
body of the loop
…….
condition
Body of the loop
Example:
n=int (input(“enter the number:”))
i=1
sum=0
while(i<=n):
sum=sum+i
i=i+1
print(“the sum of n number is:”, sum)
out put:
enter the number: 5
the sum of n number is : 15
The for loop
This is another one control structure, and it is
execute set of instructions repeatedly until the
condition become false.
Syntax:
for iterating_variable in sequence:
…… no item in sequence
body of the loop
…… True
execute
Statement
Item from
sequence
Example:
n=int (input(“enter the number:”))
sum=0
for i in range(0, n, 1):
sum=sum+i
print(“the sum of n number is:”, sum)
out put:
enter the number: 6
the sum of n number is : 15
Loop control statement
• Break
• Continue
• Pass
Break statement
• Break is a loop control statement .
• The break statement is used to terminate the
loop
• when the break statement enter inside a loop,
then the loop is exit immediately
• The break statement can be used in both while
and for loop.
Flow chart
Example:
for character in "ram":
if ( character==‘a’):
break
print(character)
Output:
r
continue
• The continue statement rejects condition
statement and print all the remaining
statements in the current iteration of the for
loop and moves the control back to the top of
the loop
• When the statement continue is entered into
any python loop control automatically passes
to the beginning of the loop
Flow chart
Example:
for character in "ram” :
if ( character == ‘a’ ):
continue
print(character)
Output:
r
m
Pass statement
The pass statement is a null operation;
nothing happens when it executes. The pass is
also useful in places where your code will
eventually go .
Example :
for character in "ram“ :
if ( character == 'a‘ ):
pass
print(character)
Output:
r
a
m
FRUITFUL FUNCTIONS
Functions that return values are
sometimes called fruitful functions. In many
other languages, a function that doesn’t
return a value is called a procedure.
Return:
• It is used to return a value to the function.
Syntax:
return [expression]
Example:
def add(x,y):
c=x+y
return c
a=10
b=20
result = add(a,b)
print("addition of two number is", result)
Local / Global variable
• Local variable:
a variable which is declared inside a
function is called as local variable.
• Global variable:
a variable which is declared outside a
function is called as global variable.
Example:
a=10
def dis():
b=20
return b
print(dis())
print(a)
Output:
20
10
Parameter
Parameter -- Data sent to one function to another.
• Formal parameter -- The parameter defined as part
of the function definition
• Actual Parameter -- The actual data sent from calling
function to called function. It happens while the
function calling.
Example:
def add(x,y):
c=x+y
print(“addition of two number is", c)
return
a=10
b=20
add(a,b)
Output:
addition of two number is 30
Function Composition
It is defined as,
• we can call one function within another
function. This ability is called composition.
• In the below example, sub() is a function
which is called within add() function.
• But the arguments are passed from one
function to another function while calling it.
Example:
def sub(x,y):
s=x-y
return s
def add():
a=10
b=20
c=a+b
print("subtraction of two no is", sub(50,25))
return c
print("addition of two no is", add())
• A string is sequence of character enclosed with single
or double or triple quotes.
• Ex:
a=“hello”
String access using index value:
>>> a=“mickey mouse”
>>> print(a[3])
k
String methods:
A method is similar to a function, it takes arguments
and returns a value.
String
• join() – to concatenate two strings
>>> print(",".join(["hi","hello"]))
hi , hello
• split() – to split the strings
>>> b=“mickey mouse”
>>> print(b.split())
['mickey', 'mouse‘]
• count() – to count the number of appearance of a character
>>> print(b.count(‘m’))
2
• swapcase() – returns a copy of the string in which all the characters
are case swapped.
>>> c="HI Friends“
>>> print(c.swapcase())
hi fRIENDS
String operations/Methods
• A segment of string is called a slice. String works on
slicing operator[:]
>>> a=“mickey mouse”
>>> print(a[0:4])
mick
>>> print(a[7:10])
mou
>>> print(a[:5])
micke
>>> print(a[3:])
key mouse
String slices
Strings are immutable
• We cannot change or edit or update a string
directly. So that string is immutable.
• It is tempting to use [ ] operator on the left
side of an assignment with the intension of
changing a character in a string
>>> a=“mickey mouse”
>>> b=‘minnie’ + a[6:12]
>>> print(b)
‘minnie mouse’
• To compare two strings. It works with if and
else condition.
• Example:
word=‘PSP’
if word = = ‘PSP’:
print(“matched”)
else:
print(“not matched”)
Strings Comparison
• This module contains a number of functions
to process standard python strings.
• To import the module (string) for processing
functions of string
• Ex:
import string
String Module
• Example program for performing multiple string
functions by using string module
import string
text=“Hello World”
print(“upper is:”, string.upper(text))
print(“lower is:”, string.lower(text))
print(“after split:”, string.split(text))
print(“replace:”,string.replace(text, “hello”, “hi”)
print(“count is:”, string.count(text, “l”))
• Python doesn’t have a native array data
structure, but it has the list, it can be used as a
multidimensional array
• Array- it is an ordered collection of elements
of a single type
• List- it is an ordered collection of any type of
elements
List as Array

More Related Content

What's hot

Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators pptViraj Shah
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in pythonJothi Thilaga P
 
Strings in C language
Strings in C languageStrings in C language
Strings in C languageP M Patil
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialKuntal Bhowmick
 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++Sachin Yadav
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaEdureka!
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming LanguageExplore Skilled
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentationVedaGayathri1
 

What's hot (20)

Python basics
Python basicsPython basics
Python basics
 
Python functions
Python functionsPython functions
Python functions
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators ppt
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in python
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorial
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
 
File in C language
File in C languageFile in C language
File in C language
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming Language
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 

Similar to Python Unit 3 - Control Flow and Functions

C programming session 02
C programming session 02C programming session 02
C programming session 02Dushmanta Nath
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Programming presentation
Programming presentationProgramming presentation
Programming presentationFiaz Khokhar
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdfMaryJacob24
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7REHAN IJAZ
 
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfGE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfAsst.prof M.Gokilavani
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzationKaushal Patel
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
2.overview of c#
2.overview of c#2.overview of c#
2.overview of c#Raghu nath
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01Abdul Samee
 

Similar to Python Unit 3 - Control Flow and Functions (20)

Operators
OperatorsOperators
Operators
 
What is c
What is cWhat is c
What is c
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
05 operators
05   operators05   operators
05 operators
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
C programming
C programmingC programming
C programming
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
 
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfGE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
2.overview of c#
2.overview of c#2.overview of c#
2.overview of c#
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Unit - 2 CAP.pptx
Unit - 2 CAP.pptxUnit - 2 CAP.pptx
Unit - 2 CAP.pptx
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
 

Recently uploaded

CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 

Recently uploaded (20)

CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 

Python Unit 3 - Control Flow and Functions

  • 1. UNIT III CONTROL FLOW, FUNCTIONS Presented by, S.DHIVYA, Assistant Professor, Kongunadu College of Engineering and Technology.
  • 2. Operators An operator is symbol that specifies an operation to be performed on the operands. Types of operator: 1. Arithmetic operator. 2. Relational Operator. 3. Logical Operator. 4. Assignment Operator. 5. Bitwise Operator. 6. Membership Operators 7. Identity Operators
  • 3. Arithmetic operator It provides some arithmetic operators which perform the task of arithmetic operations in Python. Below some arithmetic operators + , - ,* , / , % Operator Name Example + Addition c=a + b - Subtraction c=a – b * Multiplication c=a * b / Division c=a / b % Modulus c=a % b
  • 4. 1. Unary arithmetic: It require one operand. Example: +a , -b , -7 2. Binary arithmetic: It require two operands. Example: A+B 3. Integer arithmetic: It require both operands are integer numbers. Example: 10+20 4. Floating point arithmetic It require both operands are floating point. Example: 6.5+7.4
  • 5. Example program: a = int ( input (“Enter the a value”)) b= int ( input ( “enter the b value”)) sum = (a + b) print("Sum of two number is :", sum)
  • 6. These operators compare the values on either sides of them and decide the relation among them. They are also known as comparison operators . Operator Name Example < less than a < b <= less than or equal to a <= b > greater than a > b >= greater than or equal to a >= b == is equal to a == b != not equal to a != b Relational operator
  • 7. Example: a= int ( input ( “enter a value”) b= int (input (“enter b value”) if (a>b): print(“a is big”) else: print(“b is big”)
  • 8. Logical operators Python logical operator are used to combine two or more conditions and perform the logical operations using Logical AND, Logical OR and Logically NOT. Logical and Syntax: (exp1) and (exp2) Example: (a>c) and (a>b) Logical or Syntax: (exp1) or (exp2) Example: (a>b) or (a>d) Logical NOT 29!=29
  • 9. Example: a= int ( input ( “enter a value”) b= int (input (“enter b value”) c = int (input (“enter c value”) if (a>b) and (a>c): print(“a is big”) elif (b>c): print(“b is big”) else: print(“c is big”)
  • 10. Assignment operator Assignment operator used assign a values of a variable. Syntax: variable= expression or value Example: a=10 # assigning value 10 to variable a b=20 c= a+b print(“sum of two number is”,c)
  • 11. Bitwise Operator Bitwise operator used to manipulate the data at bit level, it operates on integers only. it not applicable to float or real. operator meaning & Bitwise AND | Bitwise OR ^ Bitwise XOR << Shift left >> Shift right ~ One’s complement
  • 12. Bitwise AND (&) Here operate with two operand bit by bit. Truth table for & is: Bitwise OR ( |) Bitwise exclusive OR (^) & 0 1 0 0 0 1 0 1 | 0 1 0 0 1 1 1 1 ^ 0 1 0 0 1 1 1 0
  • 13. 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 in - Evaluates to true if it finds a variable in the specified sequence and false otherwise. not in - Evaluates to true if it does not finds a variable in the specified sequence and false otherwise
  • 14. >>> a=[39,24,1.5,33,67,22] >>> 22 in a True >>> 5 not in a True Example
  • 15. >>> s1="welcome" >>> for word in s1: print(s1) Output: welcome welcome welcome welcome welcome welcome welcome
  • 16. Identity Operators Identity operators compare the memory locations of two objects. There are two Identity operators explained below: • is • is not Example:1 x=20 y=20 if ( x is y): print (“same identity”) else: print (“different identity”)
  • 17. Example 2: x=15 y=20 if ( x is not y): print (“different identity”) else: print (“same identity”)
  • 18. CONDITIONALS Decision Making and Branching i. Sequential structure: Instruction are executed in sequence i=i+1 j=j+1 ii. Selection structure: Instructions are executed based on the condition if(x>y): i=i+1 else: j=j+1
  • 19. iii. Iteration structure (or) loop: Statements are repeatedly executed. This forms program loops. Ex: for i in range(0,5,1): Print(“hello”) iv. Encapsulation structure: It includes other compound structures Ex: for i in range(0,5,1): if (condition): Print(“hello”) else: Print(“hai”)
  • 20. Conditional if statement If is a decision making statement or control statement. It is used to control the flow of execution, also to test logically whether the condition is True or False. Syntax: if (condition is true) : False True statements True condition True statement
  • 21. Example: a= int ( input ( “enter a:”)) b= int (input (“enter b:”)) if (a>b): print(“a is big”) Output: enter a: 50 enter b: 10 a is big
  • 22. Alternative If else statement It is basically two way decision making statement, Here it check the condition if the condition is true means it execute the true statements, if it is false execute another set of statement. Syntax: true false if(condition) : true statement else: false statement condition True statement False statement
  • 23. Example: a= int ( input ( “enter a value”)) b= int (input (“enter b value”)) if (a>b): print(“a is big”) else: print(“b is big”)
  • 24. Chained conditionals (or)Nested if statement One conditional can nested with another. Syntax: if(condition 1) : False if(condition 2) : True True statement 2 else: True False false statement 2 else: false statement1 Condition 1 Condition 2 true statement 2 False statement 1 False statement 2
  • 25. Example: a= int ( input ( “enter a:”)) b= int (input (“enter b:”)) c = int (input (“enter c:”)) if (a>b) and (a>c): output: print(“a is big”) enter a:20 elif (b>c): enter b: 30 print(“b is big”) enter c: 40 else: c is big print(“c is big”)
  • 26. Iteration /Looping statement The loop is defined as the block of statements which are repeatedly executed for a certain number of time. the loop program consist of two parts , one is body of the loop another one is control statement . any looping statement ,would include following steps: 1. Initialization of a condition variable 2. Test the condition . 3. Executing body of the statement based on the condition. 4. Updating condition variable Types: 1. while statement(entry check loop)(top tested loop). 2. for statement. 3. Nested looping.
  • 27. while statement(entry check loop) The while loop is an entry controlled loop statement , means the condition as evaluated first , if it is true then body of the loop is executed. Syntax: False while(condition): ……. True body of the loop ……. condition Body of the loop
  • 28. Example: n=int (input(“enter the number:”)) i=1 sum=0 while(i<=n): sum=sum+i i=i+1 print(“the sum of n number is:”, sum) out put: enter the number: 5 the sum of n number is : 15
  • 29. The for loop This is another one control structure, and it is execute set of instructions repeatedly until the condition become false. Syntax: for iterating_variable in sequence: …… no item in sequence body of the loop …… True execute Statement Item from sequence
  • 30. Example: n=int (input(“enter the number:”)) sum=0 for i in range(0, n, 1): sum=sum+i print(“the sum of n number is:”, sum) out put: enter the number: 6 the sum of n number is : 15
  • 31. Loop control statement • Break • Continue • Pass
  • 32. Break statement • Break is a loop control statement . • The break statement is used to terminate the loop • when the break statement enter inside a loop, then the loop is exit immediately • The break statement can be used in both while and for loop.
  • 34. Example: for character in "ram": if ( character==‘a’): break print(character) Output: r
  • 35. continue • The continue statement rejects condition statement and print all the remaining statements in the current iteration of the for loop and moves the control back to the top of the loop • When the statement continue is entered into any python loop control automatically passes to the beginning of the loop
  • 37. Example: for character in "ram” : if ( character == ‘a’ ): continue print(character) Output: r m
  • 38. Pass statement The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go .
  • 39. Example : for character in "ram“ : if ( character == 'a‘ ): pass print(character) Output: r a m
  • 40. FRUITFUL FUNCTIONS Functions that return values are sometimes called fruitful functions. In many other languages, a function that doesn’t return a value is called a procedure. Return: • It is used to return a value to the function. Syntax: return [expression]
  • 41. Example: def add(x,y): c=x+y return c a=10 b=20 result = add(a,b) print("addition of two number is", result)
  • 42. Local / Global variable • Local variable: a variable which is declared inside a function is called as local variable. • Global variable: a variable which is declared outside a function is called as global variable.
  • 44. Parameter Parameter -- Data sent to one function to another. • Formal parameter -- The parameter defined as part of the function definition • Actual Parameter -- The actual data sent from calling function to called function. It happens while the function calling.
  • 45. Example: def add(x,y): c=x+y print(“addition of two number is", c) return a=10 b=20 add(a,b) Output: addition of two number is 30
  • 46. Function Composition It is defined as, • we can call one function within another function. This ability is called composition. • In the below example, sub() is a function which is called within add() function. • But the arguments are passed from one function to another function while calling it.
  • 47. Example: def sub(x,y): s=x-y return s def add(): a=10 b=20 c=a+b print("subtraction of two no is", sub(50,25)) return c print("addition of two no is", add())
  • 48. • A string is sequence of character enclosed with single or double or triple quotes. • Ex: a=“hello” String access using index value: >>> a=“mickey mouse” >>> print(a[3]) k String methods: A method is similar to a function, it takes arguments and returns a value. String
  • 49. • join() – to concatenate two strings >>> print(",".join(["hi","hello"])) hi , hello • split() – to split the strings >>> b=“mickey mouse” >>> print(b.split()) ['mickey', 'mouse‘] • count() – to count the number of appearance of a character >>> print(b.count(‘m’)) 2 • swapcase() – returns a copy of the string in which all the characters are case swapped. >>> c="HI Friends“ >>> print(c.swapcase()) hi fRIENDS String operations/Methods
  • 50. • A segment of string is called a slice. String works on slicing operator[:] >>> a=“mickey mouse” >>> print(a[0:4]) mick >>> print(a[7:10]) mou >>> print(a[:5]) micke >>> print(a[3:]) key mouse String slices
  • 51. Strings are immutable • We cannot change or edit or update a string directly. So that string is immutable. • It is tempting to use [ ] operator on the left side of an assignment with the intension of changing a character in a string >>> a=“mickey mouse” >>> b=‘minnie’ + a[6:12] >>> print(b) ‘minnie mouse’
  • 52. • To compare two strings. It works with if and else condition. • Example: word=‘PSP’ if word = = ‘PSP’: print(“matched”) else: print(“not matched”) Strings Comparison
  • 53. • This module contains a number of functions to process standard python strings. • To import the module (string) for processing functions of string • Ex: import string String Module
  • 54. • Example program for performing multiple string functions by using string module import string text=“Hello World” print(“upper is:”, string.upper(text)) print(“lower is:”, string.lower(text)) print(“after split:”, string.split(text)) print(“replace:”,string.replace(text, “hello”, “hi”) print(“count is:”, string.count(text, “l”))
  • 55. • Python doesn’t have a native array data structure, but it has the list, it can be used as a multidimensional array • Array- it is an ordered collection of elements of a single type • List- it is an ordered collection of any type of elements List as Array