Shyam Technology Point -Get latest information

π™°πš‹πš˜πšžπš πšƒπšŽπšŒπš‘πš—πš˜πš•πš˜πšπš’ , π™½πšŽπš  π™ΆπšŠπšπšπšŽπšπšœ, π™΄πšŠπš›πš—πš’πš—πš πšƒπš’πš™πšœ , πšˆπš˜πšžπšƒπšžπš‹πšŽ, π™±πš’πš˜πšπš›πšŠπš™πš‘πš’, π™»πš’πšπšŽπšœπšπš’πš•πšŽ, π™΄πšŠπš›πš—πš’πš—πš πšŠπš™πš™πšœ, π™ΆπšŠπš–πš’πš—πš, π™½πšŽπš  π™΅πšŠπšŒπšπšœ, π™²πšŠπš›πšŽπšŽπš› , πš„πš™πšŒπš˜πš–πš’πš—πš πšƒπšŽπšŒπš‘πš—πš˜πš•πš˜πšπš’, πš‚πšπšŠπšπšžπšœ π™°πš—πš π™²πšŠπš™πšπš’πš˜πš—πšœ, π™·πšŽπšŠπš•πšπš‘ π™°πš—πš π™΅πš’πšπš—πšŽπšœπšœ, πš†πš˜πš›πš”πš˜πšžπš, π™Ώπš›πš˜πšπšžπšŒπš πšπšŽπšŸπš’πšŽπš πšœ, π™±πšŽπšœπš π™±πš•πš˜πšπšπš’πš—πš π™°πš—πš πš‚π™΄π™Ύ πšƒπš’πš™πšœ, π™Άπšžπš’πšπšŽ, πš‚πšπšžπšπš’ π™½πš˜πšπšŽπšœ 𝚎𝚝𝚌.

Sunday, 3 January 2021

Working with functions (notes) class 12 computer science with python

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

________________________________________

Function definition :

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) 

  1. capitalize() : Converts the first character to upper case. 
  2. casefold(): Converts string into lower case
  3. center() :Returns a centered string
  4. count() :Returns the number of times a specified value occurs in a string. 
  5. encode() :Returns an encoded version of the string. 
  6. endswith() :Returns true if the string ends with the specified value. 
  7. find() :Searches the string for a specified value and returns the position of where it was found. 
  8. format() :Formats specified values in a string. 
  9. index() :Searches the string for a specified value and returns the position of where it was found. 
  10. isalnum() :Returns True if all characters in the string are alphanumeric. 
  11. isalpha(): Returns True if all characters in the string are in the alphabet
  12. isdecimal() :Returns True if all characters in the string are decimals
  13. isdigit() :Returns True if all characters in the string are digits
  14. isidentifier(): Returns True if the string is an identifier
  15. islower() :Returns True if all characters in the string are lower case
  16. isnumeric(): Returns True if all characters in the string are numeric. 
  17. isprintable() :Returns True if all characters in the string are printable. 
  18. isspace() :Returns True if all characters in the string are whitespaces. 
  19. istitle() :Returns True if the string follows the rules of a title. 
  20. isupper(): Returns True if all characters in the string are upper case. 
  21. join() :Joins the elements of an iterable to the end of the string. 
  22. ljust(): Returns a left justified version of the string.
  23. lower() :Converts a string into lower case.
  24. lstrip() :Returns a left trim version of the string. 
  25. partition(): Returns a tuple where the string is parted into three parts. 
  26. replace() :Returns a string where a specified value is replaced with a specified value. 
  27. split(): Splits the string at the specified separator, and returns a list. 
  28. splitlines() :Splits the string at line breaks and returns a list. 
  29. startswith() :Returns true if the string starts with the specified value. 
  30. swapcase(): Swaps cases, lower case becomes upper case and vice versa. 
  31. title() :Converts the first character of each word to upper case. 
  32. translate(): Returns a translated string. 
  33. upper(): Converts a string into upper case. 
  34. zfill() Fills the string with a specified number of 0 values at the beginning. 

Question and Answers :

Q-1 What is python lamda?

Ans- A lambda function is a small anonymous function 
which can take any number of arguments, but can only have 
one expression.

Ex: x = lambda a, b : a * b 
 print(x(5, 6))

Q.2- Create a function that can 
accept two arguments name and age and print its value.

Ans- def demo(name, age):
 print(name, age)
 demo("Ben", 25)

Q.3- Write a function calculation() such that it can accept 
two variables and calculate the addition and subtraction 
of it. And also it must return both addition and 
subtraction in a single return call.

Ans- def calculation(a, b):
 return a+b, a-b
 res = calculation(40, 10)
 print(res)

Q.4- Write a function func1() such that it can accept a variable length of argument and print all arguments value.

Ans- def func1(*args):
 for i in args:
 print(i)
 func1(20, 40, 60)
 func1(80, 100)

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Thanks guys