Professional Documents
Culture Documents
UNIT II Hint
UNIT II Hint
5) Statement: An instruction that the Python interpreter can execute. Assignment (=), import, while, for and if statement.
Statements don’t produce any result.
Example: y = 3.14
6) Expression: An expression is a combination of values, variables, operators, and calls to functions. If you type an
expression at the Python prompt, the interpreter evaluates it and displays the result:
Example: >>> 1 + 1
2
>>> len("hello")
5
7) Operator: Operators is a symbol that represent computations like addition, multiplication, division and string
concatenation (+ is operator) Operand: The values the operator uses are called operands. (a, b and c are operands)
Example: >>>a=10; b=20; c=a+b; print(c)
9) Comments in python
All characters after the hash sign # and up to the end of the physical line are part of the comment and the python
interpreter ignores them
Example: >>> # This is a comment
>>> print(“Hello python”)
10) Modules
A module is a file containing Python function definitions and statements. The filename is the module name with
the suffix.py appended. Inbuilt modules are stored in standard python library like import math, import string, import time
Composition
• It is ability to take small building blocks and compose them into larger chunks.
• A function can contain another function is called function composition.
Example: Firstly, we’ll do the four steps one at a time:
response = input("What is your radius? ")
r = float (response)
area = 3.14159 * r**2
print ("The area is ", area)
Example: Now let’s compose the first two lines into a single line of code, and compose the
second two lines into another line of code.
r = float (input ("What is your radius? "))
print ("The area is ", 3.14159 * r**2)
11) Function
• A function is a block of organized, reusable code that is used to perform a single, related action.
• Functions provide better modularity for your application and a high degree of code reusing.
Uses / Advantages of function
• Maximizing code reuse and minimizing redundancy
• Procedural decomposition
12) 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.
• A function call is like a detour in the flow of execution. Instead of going to the next statement, the flow jumps to
the function definition, executes all the statements there, and then comes back to pick up where it left off .
13) Types of function: 1. User defined function 2. Built in / Predefined function
User defined function:
• Function defined by users according to their requirements are called user defined functions
• The users can modify the functions according to their requirement
• The block of the function starts with a keyword def after which the function name with or without parameters
Elements of user defined function
• Function definition Example
• Function calling / invocation def add(a,b): # function definition
Defining a function Syntax c=a+b
def functionname(parameters): return c
body of the function result=add(10,20) #function calling
return expression print(“Answer :” , result)
Calling a function syntax Output
functionname(arguments) Answer: 30
Built in function
Functions already defined in the python programming language we can directly call them to perform a specific task
Examples: Mathematical function (sin, cos, tan, sqrt, pow), String function (lower, upper, sort, reverse...)
14) Parameters and Arguments
• Parameters and Arguments are the values or expressions passed to the functions between parentheses.
• The value of the argument is always assigned to a function variable known as parameter.
• At the time of function definition, we have to define some parameters if that function requires some arguments
to be passed at the time of calling.
Example
def add(a,b): # a and b are the parameters
c=a+b
return c
result=add(10,20) # 10 and 20 are the arguments
print(“Answer :” , result)
Output
Answer: 30
Four types of formal arguments:
• Required arguments: The number of arguments should match the defined number of parameters.
• Keyword arguments: The caller recognizes the arguments by the parameters names.
• Default arguments: We can assign a value to a parameter at the time of function definition. This value is
considered the default value to that parameter.
• Variable length arguments: There are many cases where we are required to process a function with more
number of arguments than we specified in the function definition.
Example for Required Example for Keyword Example for Default Example for Variable length
Arguments Arguments Arguments Arguments
def add(a,b): def add(a,b): def add(a,b=10): def add(a,*b):
c=a+b c=a+b c=a+b print(“Answer:”,a)
return c return c return c c=0
result=add(10,20) result=add(a=10,b=20) result=add(10,20) for i in b:
print(“Answer :” , result) print(“Answer :” , print(“Answer :” , result) c=c+b
result) result1=add(10) print(“Answer:”,c)
print(“Answer1:”,result1) print(10)
print(10,10,10)