Working with Functions In Python with computer science 2021 ( notes) -
Syllabus :-
Functions :- scope, parameter passing, mutable and immutable properties of data objects , passing strings, lists, tuples ,dictionaries to functions, default parameters ,positional parameters , return values.
Functions using libraries :
mathematical and string functions
______________________________________
Content covered in this post -
Working with functions|| Sumita Arora || Class 12 || Computer science with python|| Solutions||
Working with functions sumita arora solution || Computer science with Python class 12 || Working with functions class 12 sumita arora solution || Working with functions class 12 sumita arora || Working with functions sumita arora || sumita arora working with functions || sumita arora working with functions solution || Working with functions sumita arora practical solution || sumita arora practical solution
Functions are the subprograms that perform specific task and often returns value.
Types of functions :
1. Built in functions (Library functions):
Pre-defined or already built in the library of python.
Ex: len(), type(), int(), input() etc.
2. Functions defined in module:
Function in particular module, when needed have to import that module.
Ex: sin(), sqrt() in module math. randint() in module random.
3.User defined functions: Functions defined by the user.
User defined function :
Syntax:
def function name(parameters):
Statement(s)
Keyword def marks the start of function header.A function name is uniquely identify it and follow the rules of writing identifiers in python.Parameter (arguments) through which we pass values to a function and are optional.
colon (:) to mark end of function header or start of a block.One or more valid python statements that makes up the function body, must have same indentation level and an optional return statement may be used to return value from the function body.
Function calling statement :
A function definition is executed through a calling statement written in top level statement (main program) with no indentation. By default python name this segment as _main_ (a built in variable).
Example of calling statement :
Function-name() – when no return statement is used.
Variable name= Function-name()-when return statement is used.
Example :
def sum(x,y):
s=x+y
return s
res=sum(12,15)
print(res)
Function Parameters (Arguments):
Values can be passed to functions via parameters.
There are two types of parameters/arguments.
1. Actual Parameter – The values being passed through a function-call.
Ex- 12,15 in res=sum(12,15)
2. Formal Parameter- the values received in the function definition/ header.
Ex- x,y in def sum(x,y):
Python supports 3 types of formal arguments/parameters.
a. Positional arguments (required arguments)
b. Default arguments
c. Keyword (or named arguments)
When the function call statement must match the number an order of arguments are defined in the function definition, this is called the positional arguments.
A parameter having default value in the function header is known as default parameter. It can be skipped in the function call statement.
Keyword arguments are the named arguments with assigned values being passed in the function call statements.
return Statement :
The return statement is used to exit a function and go back to the place from where it was called. It is an optional statement.
Ex-
def sum(x,y):
z=x+y
return z #return the value/result
x,y=4,5
r=sum(x,y) #x, y are actual arguments
print(r)
Note : a function that returns a non-empty value is a non-void function.
Scope Of Variables :
There are 3 types of variables with the view of scope.
1.Local variable –accessible only inside the functional block where it is declared.
2.Global variable –variable which is accessible among whole program using global keyword.
3.Non local variable –accessible in nesting of functions, using nonlocal keyword.
Mutable/Immutable Properties Of Passed Data :
Objects :
Everything in python is an object and every objects in python can be either mutable or immutable.
Mutable objects : list, dict, set, byte array.
Immutable objects : int, float, complex, string, tuple, frozen set, bytes.
Mutability of arguments/parameter affects the change of value in caller function. We can say that :
Changes in immutable types are not reflected in the caller function at all.
Changes, if any, in mutable types
- are reflected in called function if its name is not assigned a different variable or data type.
- are not reflected in the caller function if it is assigned a different variable or data type.
Pass Arrays/List To Function :
Python arrays more often talks about Python lists.
Array of numeric values are supported in Python by the array module.
e.g.
def dosomething(thelist):
for element in thelist:
print(element)
dosomething(['1','2','3'])
alist=['red','green','blue']
dosomething(alist)
OUTPUT:
1
2
3
red
green
blue
Function Using Libraries :
Mathematical functions are available under math module. To use mathematical functions under this module, we have to import the module using import math.
E.g.
Import math
r=math.sqrt(4)
print(r)
String functions are available in python standard module.These are always availble to use.
Ex-
capitalize() function Converts the first character of string to upper case.
s="i love programming"
r=s.capitalize()
print(r)
- capitalize() : Converts the first character to upper case.
- casefold(): Converts string into lower case
- center() :Returns a centered string
- count() :Returns the number of times a specified value occurs in a string.
- encode() :Returns an encoded version of the string.
- endswith() :Returns true if the string ends with the specified value.
- find() :Searches the string for a specified value and returns the position of where it was found.
- format() :Formats specified values in a string.
- index() :Searches the string for a specified value and returns the position of where it was found.
- isalnum() :Returns True if all characters in the string are alphanumeric.
- isalpha(): Returns True if all characters in the string are in the alphabet
- isdecimal() :Returns True if all characters in the string are decimals
- isdigit() :Returns True if all characters in the string are digits
- isidentifier(): Returns True if the string is an identifier
- islower() :Returns True if all characters in the string are lower case
- isnumeric(): Returns True if all characters in the string are numeric.
- isprintable() :Returns True if all characters in the string are printable.
- isspace() :Returns True if all characters in the string are whitespaces.
- istitle() :Returns True if the string follows the rules of a title.
- isupper(): Returns True if all characters in the string are upper case.
- join() :Joins the elements of an iterable to the end of the string.
- ljust(): Returns a left justified version of the string.
- lower() :Converts a string into lower case.
- lstrip() :Returns a left trim version of the string.
- partition(): Returns a tuple where the string is parted into three parts.
- replace() :Returns a string where a specified value is replaced with a specified value.
- split(): Splits the string at the specified separator, and returns a list.
- splitlines() :Splits the string at line breaks and returns a list.
- startswith() :Returns true if the string starts with the specified value.
- swapcase(): Swaps cases, lower case becomes upper case and vice versa.
- title() :Converts the first character of each word to upper case.
- translate(): Returns a translated string.
- upper(): Converts a string into upper case.
- zfill() Fills the string with a specified number of 0 values at the beginning.
This comment has been removed by a blog administrator.
ReplyDelete