0% found this document useful (0 votes)
0 views

lecture 9

The document explains operator precedence in Python, detailing the order of operations using the acronym PEMDAS and providing examples of how expressions are evaluated. It also discusses the concept of functions, including their necessity for code organization, types of functions (user-defined and built-in), and how to define and call them. Additionally, it covers the flow of execution in a program and the different types of function prototypes.

Uploaded by

vishal10thakwani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

lecture 9

The document explains operator precedence in Python, detailing the order of operations using the acronym PEMDAS and providing examples of how expressions are evaluated. It also discusses the concept of functions, including their necessity for code organization, types of functions (user-defined and built-in), and how to define and call them. Additionally, it covers the flow of execution in a program and the different types of function prototypes.

Uploaded by

vishal10thakwani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

OPERATOR PRECEDENCE

•When an expression contains more


than one operator, the order of
evaluation depends on the order of
operations.
Operator Description
** Exponentiation (raise to the power)
~+- Complement, unary plus and minus (method
names for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division .
+- Addition and subtraction
>> << Right and left bitwise shift.
& Bitwise 'AND'
^| Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
• -For mathematical operators, Python follows mathematical convention. -The acronym
PEMDAS (Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction) is a
useful way to remember the rules:
• Parentheses have the highest precedence and can be used to force an expression to evaluate
in the order you want.
• Since expressions in parentheses are evaluated first, 2 * (3-1)is 4, and (1+1)**(5-2) is 8.
• You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60,
even if it doesn’t change the result.
• Exponentiation has the next highest precedence, so 1 + 2**3 is 9, not 27, and 2 *3**2 is 18,
not 36.
• Multiplication and Division have higher precedence than Addition and Subtraction. So 2*3-1 is
5, not 4, and 6+4/2 is 8, not 5.
• Operators with the same precedence are evaluated from left to right (except
exponentiation).
EXAMPLES:-

a=9-12/3+3*2-1 A=2*3+4%5-3/2+6 find m=?


a=? A=6+4%5-3/2+6 m=-43||8&&0||-2
a=9-4+3*2-1 A=6+4-3/2+6 m=-43||0||-2
a=9-4+6-1 A=6+4-1+6 m=1||-2
a=5+6-1 A=10-1+6 m=1
a=11-1 A=9+6
a=10 A=15
a=2,b=12,c=1 a=2,b=12,c=1 a=2*3+4%5-3//2+6
d=a<b>c d=a<b>c-1 d=2<12>1-1 a=6+4-1+6
d=2<12>1 d=2<12>0 a=10-1+6
D=1>1 d=1>0 a=15
d=0 d=1
FUNCTIONS

• Function is a sub program which consists of


set of instructions used to perform a
specific task. A large program is divided into
basic building blocks called function.
NEED FOR FUNCTION

• When the program is too complex and large they are divided into parts. Each part is
separately coded and combined into single program. Each subprogram is called as
function.
• Debugging, Testing and maintenance becomes easy when the program is divided into
subprograms.
• Functions are used to avoid rewriting same code again and again in a program.
• Function provides code re-usability
• The length of the program is reduced.
TYPES OF FUNCTION

•Functions can be classified into two


categories:
• i) user defined function
•ii) Built in function
BUILT IN FUNCTIONS

• Built in functions are the functions that are


already created and stored in python.
• These built in functions are always available for
usage and accessed by a programmer. It cannot
be modified.
Built in function Description
>>>max(3,4) # returns largest element
4
>>>min(3,4) # returns smallest element
3
>>>len("hello") # returns length of an object
5
>>>range(2,8,1) #returns range of given values
[2, 3, 4, 5, 6, 7]
>>>round(7.8) #returns rounded integer of the given number
8.0
>>>chr(5) #returns a character (a string) from an integer
\x05'
>>>float(5) #returns float number from string or integer.
5.0
Built in function Description

>>>int(5.0) # returns integer from string or float


5
>>>pow(3,5) #returns power of given number
243
>>>type( 5.6) #returns data type of object to which it belongs
<type ’float’>
>>>t=tuple([4,6.0,7]) # to create tuple of items from list
(4, 6.0, 7)
>>>print("good morning") # displays the given object
Good morning
>>>input("enter name: ") # reads and returns the given string
enter name : George
USER DEFINED FUNCTIONS

• User defined functions are the functions that programmers create for their requirement and
use.
• These functions can then be combined to form module which can be used in other programs
by importing them.
• Advantages of user defined functions:
• Programmers working on large project can divide the workload by making different functions.
• If repeated code occurs in a program, function can be used to include those codes and
execute when needed by calling that function.
FUNCTION DEFINITION: (SUB PROGRAM)

• def keyword is used to define a function.


• Give the function name after def keyword followed by parentheses in which arguments
are given.
• End with colon (:)
• Inside the function add the program statements to be executed
• End with or without return statement
SYNTAX:

• def fun_name(Parameter1,Parameter2…Parameter n):


• statement1
• statement2…
• statement n
• return[expression]
EXAMPLE:

•def my_add(a,b):
• c=a+b
• return c
FUNCTION CALLING: (MAIN FUNCTION)

• Once we have defined a function, we can call it


from another function, program or even the
Python prompt.
• To call a function we simply type the function
name with appropriate arguments.
EXAMPLE:

•x=5
• y=4
•my_ add (x ,y )
FLOW OF EXECUTION

• The order in which statements are executed is called the flow of execution
• Execution always begins at the first statement of the program.
• Statements are executed one at a time, in order, from top to bottom.
• Function definitions do not alter the flow of execution of the program, but remember
that statements inside the function are not executed until the function is called.
• Function calls are like a bypass in the flow of execution. Instead of going to the next
statement, the flow jumps to the first line of the called function, executes all the
statements there, and then comes back to pick up where it left off.
NOTE:

• When you read a program, don’t read from top to


bottom. Instead, follow the flow of execution. This
means that you will read the def statements as you are
scanning from top to bottom, but you should skip the
statements of the function definition until you reach a
point where that function is called.
FUNCTION PROTOTYPES:

• Function without arguments and without return type


• ii. Function with arguments and without return type
• iii. Function without arguments and with return type
• iv. Function with arguments and with return type
1. FUNCTION WITHOUT ARGUMENTS AND
WITHOUT RETURN TYPE

• In this type no argument is passed through the function


call and no output is return to main function .
• The sub function will read the input values perform the
operation and print the result in the same block.
2.FUNCTION WITH ARGUMENTS AND WITHOUT
RETURN TYPE

•Arguments are passed through


the function call but output is
not return to the main function.
3.FUNCTION WITHOUT ARGUMENTS AND WITH
RETURN TYPE

•In this type no argument is passed


through the function call but output
is return to the main function.
4.FUNCTION WITH ARGUMENTS AND WITH
RETURN TYPE

•In this type arguments are


passed through the function call
and output is return to the main
function

You might also like