Unit 2
Unit 2
Python Datatypes ,
User Defined Function
MRS. H H DESAI[NARANLALA COLLEGE ,NAVSARI ]
Python Variable
❑Variable is the name of memory location, where are can
stored different types of values.
❑Python has no command for declaring a variable.
❑A variable is created the moment you first assign a value to
it.
Output::- amit
20
20000.00
Example:- O/P:-
print (tuple)
Mrs. H H Desai [ Naranlala college, Navsari ]
Python Data Type:-
5. Boolean:- Boolean is a common data type of all programming languages.
• Boolean returns two values.
• True
• False
Example:- O/P:-
a=(“learn python”) 12
len(a)
Example:- O/P:-
len(a)==12 True
Example:- O/P:-
len(a)!=12 False
◦ len() is a method. Which is used to find the length of the String.
Mrs. H H Desai [ Naranlala college, Navsari ]
Python Data Type:-
6. Dictionaries:- It contains pairs of key and value. Each key and its value are separated
with a colon (:) and the pair of key and value is separated with a comma (,). And all
the keys and values are kept inside curly braces { }.
Example:- O/P:-
dict={1:”H”, 5:”e”, 7:”l”, 8:”l”, 9:”o”} e
Print(dict[5]) o
Print(dict[9])
• We can pass any number of parameters in the function as per our need. And these parameters work as a variable for that
function.
• SYNTAX:-
def function_name(param1,param2,…,paramN):
#function body
#what the function does goes here
#write some action
return something. #optional
- Recursive Functions are most used in tree structure (reading or creating tree structure), where
we do not know whether the node has any children or not, then call the function.
- Secondly, to read file directories, because we do not know whether there will be all the files
inside a directory or a sub directory, and then sub-directories inside it and then inside that etc..
#call the function. - A condition is given that the function is called only
if the value of the variable is less than 100.
print_number(num)
def find_fact(num) :
if (num == 0) : •O/P:-
5*4*3*2*1*= 120
return 1 3*2*1*= 6
- In the example, code has been implemented
simply to find the factorial of a number.
print(num, end='*')
- In the example, a condition has been given that if
# Recursion. the value of the variable is less than 1, only 1 is
returned, otherwise the same function will be called.
result = (num*find_fact(num - 1) )
return result
print('=', find_fact(5))
print('=', find_fact(3))
Mrs. H H Desai [ Naranlala college, Navsari ]
User Defined Function in Python:-
• Lambda Function:- A lambda function is a small anonymous function.
• Lambda functions are very sort functions, means when we have to write a single line statement
function, instead of defining a complete function, we use lambda functions.
• SYNTAX:-