Python Scripting - Week 6 Flashcards

1
Q

What is namespace in python ?

A

It means scope of an variable where it belongs to ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How many name spaces in python ?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to print python builtins ?

A

dir(__builtins__)
Builtins are loaded on startup bydefault,when python program starts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to get help of builtin ?

A

help(ArithmeticError)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to get type of builtin ?

A

typer(ArithmeticError)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to print globals ?

A

globals()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to print the all globals in table format ?

A

whos

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In which namespace should every varaible is stored ?

A

Every variable is stored in the globals

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How python searches for variable, what is rthe thumb rule ?

A

Python follows LEGB rule ?
Local
Enclosed
Global
Builtin

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to return multiple variables in function ?

A

Use comma seperator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is returned by-default by function, when we return multiple values from function ?

A

Tuple is returned by default
Multiple Values packed in tuple by-default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to return list from function ?

A

Enclose the return values in list then return
return [“This is a string”, “another one”,10]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is unpacking ?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the default return type of function ?

A

If no return statement is given, then function Returns
NoneType

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is EGB ?

A

EGB is an namespace
E - Enclosed
G- Global
B- Builtin

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How many types of arguments in a function ?

A

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

17
Q

Where to get keyword arguments ?

A

All keyword arguments must follow all position arguments
def xyz(a,b,c,d=10)

18
Q

What is the benefit of named variable in function argument ?

A

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

19
Q

Write a function with return combine named variable arguments ?

A
def combineName(firstName='',lastName=''):
    return firstName+" "+lastName
print(combineName(lastName='Kumar',firstName='Hemant'))
20
Q

What are Document String in function ?

A

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.

21
Q

How to add doct string to an function ?

A

Doct can be added in trtiple quotes
~~~
def combineName(firstName=’‘,lastName=’’):
“"”This function Combine Names “””
return firstName+” “+lastName
~~~

22
Q

How to make iter ?

A

a = iter(‘Hello’)
next(a)

23
Q

What is lazy iteration ?

A

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
24
Q

What is exhaustion of generator ?

A

When it finishes itertation ?

25
Q

How to make a generator object of 100 numbers ?

A

We have to use list comprehension
count_100=(i for i in range(100))
for i in count_100:
print(i)

26
Q

Can we use generators multiple times ?

A

No , generators can only be used once, they are exhausted when iteration is finished.
You have to recreate them to use

27
Q

What is the keyword argument in print function ?

A

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.

28
Q

How to make a generator of 100 numbers using comprehension ?

A

We have to use parenthesis for generator comprehension.
count_100=(i for i in range(100))
type(count_100

29
Q

Is range() object is an generator, iterator or sequence ?

A

Its a sequence.

30
Q

How to get memory size of object ?

A

import sys
sys.getsizeof(count_100)

31
Q
A