pymodule3
pymodule3
by
Karuna Bhalerao
Assistant Professor, VJTI, Matunga, Mumbai
400019
Contact: [email protected]
Mob: 8378919162
MODULE III
Arrays
"Arrays" in Python are not the arrays in conventional programming languages like C and
Java, but closer to lists. A list can be a collection of either homogeneous or heterogeneous
elements, and may contain ints, strings or other lists.
Individual elements can be accessed through indexes. Python arrays are zero-indexed.
Here is an example:
my_array = array('i', [1,2,3,4,5])
print(my_array[1])
#2
print(my_array[2])
#3
print(my_array[0])
#1
Introduction to Arrays
• An array is a data structure that stores values of same data type. In
Python, this is the main difference between arrays and lists.
• While python lists can contain values corresponding to different data
types, arrays in python can only contain values corresponding to
same data type.
• To use arrays in python language, you need to import the standard
array module. This is because array is not a fundamental data type
like strings, integer etc. Here is how you can import array module in
python :
• from array import *
• Once you have imported the array module, you can declare an
array. Here is how you do it:
• arrayIdentifierName = array(typecode, [Initializers])
Introduction to Arrays
• In the declaration above, arrayIdentifierName is the name of array, typecode lets
python know the type of arrayand Initializers are the values with which array is
initialized.
• Type codes are the codes that are used to define the type of array values or the type
of array. The table in the parameters section shows the possible values you can use
when declaring an array and it's type.
• Here is a real world example of python array declaration :
• my_array = array('i',[1,2,3,4])
• In the example above, typecode used is i. This typecode represents signed integer
whose size is 2 bytes.
• Here is a simple example of an array containing 5 integers
• from array import *
• my_array = array('i', [1,2,3,4,5])
• for i in my_array:
• print(i)
• #1
• #2
• #3
• #4
• #5
• Append any value to the array using append()
Method
def main():
message()
print (“Goodbye!”)
def message():
main()
2.Functions defined in modules
When we want to use module based functions in our
program, we need to import the corresponding module of that
particular function.
The functions available in math module are:
ceil(), floor(), fabs(), exp(), log(), pow(), sqrt() cos(), sin()etc.
Example:
ceil(x) returns the smallest integer not less than x
fabs(x) returns the absolute value of x,where x is a numeric value.
Example:
To work with the functions of math module, we must
import math module in our program.
sqrt() returns the square root of a number
>>>import math
>>>math.sqrt(49)
7.0
User defined function
Syntax of function
def function-name(parameters) :
#block of statement(s)
Example:
def hello_world(): #called function
print("hello world")
Output:
hello world
Defining functions in python
def functionName( list of parameters ): Top level statements
"_docstring"
function_block • In python program, generally all
return [expression] python definitions are given at
Top level statements the top followed by statements
which are not part of any
functions These statements are
not indented at all.
def userfunction (arg1, arg2, arg3 ...):
program statement1 • The non indented statements
program statement2 written after all the function
definitions are often called top
program statement3 level statements .
....
return
userfunction(arg1,arg2,arg3)
Function definition with example
• Keyword def marks the start of #python function to calculate the sum o
function header. f two variables
• A function name to uniquely identify
it. Name of the Function follows the #defining the function
same rule of naming the identifier.
def sum(a,b):
• Parameters (arguments) through '‘’takes a and b and return the
which we pass values to a function
are optional. sum'''
• A colon (:) is used to mark the end of return a+b;
function header.
• The string after the function header #taking values from the user
is called the docstring . It is briefly a = int(input("Enter a: "))
used to explain what a function does. b = int(input("Enter b: "))
comments are ignored by python
interpreter but docstrings can be
viewed when the program is running. #printing the sum of a and b
• One or more python statements form print("Sum = ",sum(a,b))
function body.All the statements of
the block should have same Output:
indentation level.
• A return statement is used to return Enter a: 10
value(s) from the function but it is Enter b: 20
always optional. Sum = 30
HOW A FUNCTION WORKS
Note:
1.Function which is called by another Function is called Called Function. The called
function contains the definition of the function and formal parameters are
associated with them.
2. The Function which calls another Function is called Calling Function and actual
paramaters are associated with them.
3.In python, a function must be defined before the function calling otherwise
python interpreter gives an error.
Lambda function
Python lambda function doesn’t Example:
have any return statement.It has only
a single expression which is always
returned by default. The Python
lambda function is anonymous as it is
• add =
a function without a def keyword lambda x, y
and name. To create a Python
lambda function, we have to use the :x+y
lambda keyword. print(add(10
The basic syntax of python lambda is , 20))
Lambda arguments : expression • print("\nResult from
a Function")
The Python lambda function
accepts any number of • def add_func(x, y):
arguments but use only one
expression.
• return x + y
For instance, lambda a, b: a + b. • print(add_func(10,
Here, a and b are the arguments
accepted by the lambda function. a +
b is the expression.
20))