Nearby lessons
100 of 108Python - Nonlocal Keyword
Detailed Tutorial Notes
Eg:
1) def f1(n1,*s):
2) print(n1)
3) for s1 in s:
4) print(s1)5)
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10 10 20 30 40 10 A 30 B
Note: After variable length argument,if we are taking any other arguments then we
should provide values as keyword arguments.
Eg:
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
f1("A","B",n1=10)
Output
A
B
10
f1("A","B",10) ==>Invalid
TypeError: f1() missing 1 required keyword-only argument: 'n1'Note: We can declare key word variable length arguments also.
For this we have to use **.
def f1(**n):We can call this function by passing any number of keyword arguments. Internally these
keyword arguments will be stored inside a dictionary.
Eg:
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
n1 = 10 n2 = 20 n3 = 30 rno = 100 name = Durga marks = 70 subject = Java
Case Study:
def f(arg1,arg2,arg3=4,arg4=8):
print(arg1,arg2,arg3,arg4)
1. f(3,2) ==> 3 2 4 8
2. f(10,20,30,40) ===>10 20 30 40
3. f(25,50,arg4=100) ==>25 50 4 100
4. f(arg4=2,arg1=3,arg2=4)===>3 4 4 2
5. f()===>Invalid
TypeError: f() missing 2 required positional arguments: 'arg1' and 'arg2'
6. f(arg3=10,arg4=20,30,40) ==>Invalid
SyntaxError: positional argument follows keyword argument[After keyword arguments we should not take positional arguments]
7. f(4,5,arg2=6)==>Invalid
TypeError: f() got multiple values for argument 'arg2'
8. f(4,5,arg3=5,arg5=6)==>Invalid
TypeError: f() got an unexpected keyword argument 'arg5'Note: Function vs Module vs Library:
- A group of lines with some name is called a function
- A group of functions saved to a file , is called Module
- A group of Modules is nothing but Library Types of Variables Python supports 2 types of variables.
- Global Variables
- Local Variables
- Global Variables The variables which are declared outside of function are called global variables. These variables can be accessed in all functions of that module. Eg:
1) a=10 # global variable
2) def f1():
3) print(a)4)
5) def f2():
6) print(a)7)
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10 10
Function 1
Function 2
Function 3
Module 1
Function 1
Function 2
Function 3
Module 2
Library
- ----------------
- ----------------
- ----------------
- ----------------
- ----------------
- ----------------
- ----------------
- ----------------
- ----------------
- Function
- Local Variables: The variables which are declared inside a function are called local variables. Local variables are available only for the function in which we declared it.i.e from outside of function we cannot access. Eg:
1) def f1():
2) a=10
3) print(a) # valid4)
5) def f2():
6) print(a) #invalid7)
8) f1()
9) f2()10)
11) NameError: name 'a' is not definedglobal keyword:
We can use global keyword for the following 2 purposes:
- To declare global variable inside function
- To make global variable available to the function so that we can perform required modifications Eg 1:
1) a=10
2) def f1():
3) a=777
4) print(a)5)
6) def f2():
7) print(a)8)
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
777 10
🧠 Test Your Knowledge
5 QuestionsProgress: 0 / 5
Keep Going!Python - Random Module