Python Scripting - Week 6 Flashcards
What is namespace in python ?
It means scope of an variable where it belongs to ?
How many name spaces in python ?
LEGB
Local - Local Scope
Enclosed - Function within Function,Nested Function
Global - A variable which have scope everywhere.
Built-ins - Builtin Functions
It corresponds to function
How to print python builtins ?
dir(__builtins__)
Builtins are loaded on startup bydefault,when python program starts
How to get help of builtin ?
help(ArithmeticError)
How to get type of builtin ?
typer(ArithmeticError)
How to print globals ?
globals()
How to print the all globals in table format ?
whos
In which namespace should every varaible is stored ?
Every variable is stored in the globals
How python searches for variable, what is rthe thumb rule ?
Python follows LEGB rule ?
Local
Enclosed
Global
Builtin
How to return multiple variables in function ?
Use comma seperator
What is returned by-default by function, when we return multiple values from function ?
Tuple is returned by default
Multiple Values packed in tuple by-default
How to return list from function ?
Enclose the return values in list then return
return [“This is a string”, “another one”,10]
What is unpacking ?
Returning multiple values , and collect them in multiple variables .Most important thing is no. of of return values & collecting variables should be in same
def create_something():
return [“This is a string”, “another one”,10]
a,b,c=create_something()
print(a,b,c)
What is the default return type of function ?
If no return statement is given, then function Returns
NoneType
What is EGB ?
EGB is an namespace
E - Enclosed
G- Global
B- Builtin
How many types of arguments in a function ?
They are of two types:
1. Positional arguments - just listed variable names x,y,z
2. Keyword arguments - variable with an assignment operator a=10
Where to get keyword arguments ?
All keyword arguments must follow all position arguments
def xyz(a,b,c,d=10)
What is the benefit of named variable in function argument ?
It is beneficial while calling a function, you do not need to remember the position of the argument, you can use it by name in any order
Write a function with return combine named variable arguments ?
def combineName(firstName='',lastName=''): return firstName+" "+lastName print(combineName(lastName='Kumar',firstName='Hemant'))
What are Document String in function ?
Doct strings allow us to add some text info to function, when we hover on function it shows that text, it is very helpful to know about the function.
How to add doct string to an function ?
Doct can be added in trtiple quotes
~~~
def combineName(firstName=’‘,lastName=’’):
“"”This function Combine Names “””
return firstName+” “+lastName
~~~
How to make iter ?
a = iter(‘Hello’)
next(a)
What is lazy iteration ?
we have to use yield keyword,yield keyword is used to make generator object.
def gen_count_by_twos(): num=0 while True: yield num num+=2 for i in gen_count_by_twos(): print(i) if i>20: break
What is exhaustion of generator ?
When it finishes itertation ?
How to make a generator object of 100 numbers ?
We have to use list comprehension
count_100=(i for i in range(100))
for i in count_100:
print(i)
Can we use generators multiple times ?
No , generators can only be used once, they are exhausted when iteration is finished.
You have to recreate them to use
What is the keyword argument in print function ?
end=”” is the keyword argument in print method()
print function will always append a \n to the end of the string ,This is so when you make your next call to print(), it will print on the next line.
How to make a generator of 100 numbers using comprehension ?
We have to use parenthesis for generator comprehension.
count_100=(i for i in range(100))
type(count_100
Is range() object is an generator, iterator or sequence ?
Its a sequence.
How to get memory size of object ?
import sys
sys.getsizeof(count_100)