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

What's hot (20)

Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Stack
StackStack
Stack
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Strings in C
Strings in CStrings in C
Strings in C
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 

Similar to Python Unit 3 - Control Flow and Functions

C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
2.overview of c#
2.overview of c#2.overview of c#
2.overview of c#
Raghu nath
 

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
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
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
 

Recently uploaded

Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 

Recently uploaded (20)

Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 

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