


































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
this is for freshers student ...it is python interview question
Typology: Cheat Sheet
Uploaded on 11/13/2022
5
(1)1 document
1 / 74
This page cannot be seen from the preview
Don't miss anything!
LIST
Tuple
tuples = ('a', 'b', 'c', 1, 2)
2. What is Decorator? Explain With Example.
def decorator_func(func): def wrapper_func(): print("wrapper_func Worked") return func() print("decorator_func worked") return wrapper_func
def show(): print("Show Worked") decorator_show = decorator_func(show) decorator_show()
2. What is Decorator? Explain With Example.
def decorator_func(func): def wrapper_func(): print("wrapper_func Worked") return func() print("decorator_func worked") return wrapper_func
def show(): print("Show Worked") decorator_show = decorator_func(show) decorator_show()
Output: decorator_func worked wrapper_func Worked Show Worked
def decorator_func(func): def wrapper_func(): print("wrapper_func Worked") return func() print("decorator_func worked") return wrapper_func
def show(): print("Show Worked") decorator_show = decorator_func(show) decorator_show()
#Alternative @decorator_func def display(): print('display worked') display()
2. What is Decorator?
Explain With Example.
Output: decorator_func worked wrapper_func Worked Show Worked decorator_func worked wrapper_func Worked display worked
List Comprehension
Syntax:
[expression for item in iterable if conditional]
Dict Comprehension
Syntax :
{key:value for (key,value) in iterable if conditional}
List Comprehension
Using List Comprehension:
ls = [i for i in range(10) if i%2] print(ls)
Output: [1, 3, 5, 7, 9]
Dict Comprehension
Using Dict Comprehension:
d1={n:n*n for n in range(1,10)} print (d1)
Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
List Comprehension
Syntax:
[expression for item in iterable if conditional]
Example: Common Way: l = [] for i in range(10): if i%2: l.append(i) print(l)
Using List Comprehension: ls = [i for i in range(10) if i%2] print(ls)
Output: [1, 3, 5, 7, 9]
Dict Comprehension
Syntax :
{key:value for (key,value) in iterable if conditional}
Example: Common Way: d = {} for i in range(1,10): sqr = ii d[i] = ii print(d)
Using Dict Comprehension: d1={n:n*n for n in range(1,10)} print (d1)
Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
ITERATOR
● An iterator is an object which contains a countable number of values and it is used to iterate over iterable objects like list, tuples, sets, etc. ● Iterators are used mostly to iterate or convert other objects to an iterator using iter() function. ● Iterator uses iter() and next() functions. ● Every iterator is not a generator.
GENERATOR
● Generators are iterators which can execute only once. ● Generator uses “ yield ” keyword. ● Generators are mostly used in loops to generate an iterator by returning all the values in the loop without affecting the iteration of the loop. ● Every Generator is an iterator
ITERATOR
Example:
iter_list = iter(['A', 'B', 'C']) print(next(iter_list)) print(next(iter_list)) print(next(iter_list))
Output: A B C
GENERATOR
EXAMPLE:
def sqr(n): for i in range(1, n+1): yield i*i a = sqr(3) print(next(a)) print(next(a)) print(next(a))
Output: 1 4 9
6. What is ‘init’ Keyword In Python?
class Person:
def init(self, name): self.name = name
def say_hi(self): print('Hello, my name is', self.name)
p = Person('Nitin') p.say_hi() (^) Output:
Hello, my name is Nitin
The init.py file lets the Python interpreter know
that a directory contains code for a Python module. It can
be blank. Without one, you cannot import modules from
another folder into your project.
The role of the init.py file is similar to the
init function in a Python class. The file essentially
the constructor of your package or directory without it
being called such. It sets up how packages or functions will
be imported into your other files.
7. Difference Between Modules and Packages in Python
The module is a simple Python file that contains collections of functions and global variables and with having a .py extension file. It is an executable file and to organize all the modules we have the concept called Package in Python.
A module is a single file (or files) that are imported under one import and used. E.g. import <my_module>
Import numpy
8. Difference Between Range and Xrange?
Parameters Range() Xrange()
Return type It returns a list of integers. It returns a generator object.
Memory Consumption Since range() returns a list of elements, it takes more memory.
In comparison to range(), it takes less memory.
Speed Its execution speed is slower. Its execution speed is faster.
Python Version Python 2, Python 3 xrange no longer exists.
Operations Since it returns a list, all kinds of arithmetic operations can be performed.
Such operations cannot be performed on xrange().
9. What are Generators. Explain it with Example.
def sqr(n): for i in range(1, n+1): yield i*i a = sqr(3)
print("The square are : ") print(next(a)) print(next(a)) print(next(a))
Output: The square are : 1 4 9