Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

python interview question, Cheat Sheet of Programming Languages

this is for freshers student ...it is python interview question

Typology: Cheat Sheet

2022/2023

Uploaded on 11/13/2022

unknown user
unknown user 🇮🇳

5

(1)

1 document

1 / 74

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
52 Python Developer Interview
Questions-Answers
Watch Full Video On Youtube:
https://youtu.be/YeupGcOW-3k
Connect with me:
Youtube: https://www.youtube.com/c/nitmantalks
Instagram: https://www.instagram.com/nitinmangotra/
LinkedIn: https://www.linkedin.com/in/nitin-mangotra-9a075a149/
Facebook: https://www.facebook.com/NitManTalks/
Twitter: https://twitter.com/nitinmangotra07/
Telegram: https://t.me/nitmantalks/
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a

Partial preview of the text

Download python interview question and more Cheat Sheet Programming Languages in PDF only on Docsity!

52 Python Developer Interview

Questions-Answers

Watch Full Video On Youtube:

https://youtu.be/YeupGcOW-3k

Connect with me:

Youtube: https://www.youtube.com/c/nitmantalks

Instagram: https://www.instagram.com/nitinmangotra/

LinkedIn: https://www.linkedin.com/in/nitin-mangotra-9a075a149/

Facebook: https://www.facebook.com/NitManTalks/

Twitter: https://twitter.com/nitinmangotra07/

Telegram: https://t.me/nitmantalks/

LIST

1. Lists are mutable

2. List is a container to contain different types of

objects and is used to iterate objects.

3. Syntax Of List

list = ['a', 'b', 'c', 1,2,3]

4. List iteration is slower

5. Lists consume more memory

6. Operations like insertion and deletion are better

performed.

  1. Difference Between List and Tuple

Tuple

  1. Tuples are immutable
  2. Tuple is also similar to list but contains immutable objects.
  3. Syntax Of Tuple

tuples = ('a', 'b', 'c', 1, 2)

  1. Tuple processing is faster than List.
  2. Tuple consume less memory
  3. Elements can be accessed better.

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

Decorator Coding Example

A Decorator is just a function that

takes another function as an

argument, add some kind of

functionality and then returns

another function.

All of this without altering the

source code of the original function

that you passed in.

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

  1. Difference Between List and Dict Comprehension

List Comprehension

Syntax:

[expression for item in iterable if conditional]

Dict Comprehension

Syntax :

{key:value for (key,value) in iterable if conditional}

  1. Difference Between List and Dict Comprehension

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}

  1. Difference Between List and Dict Comprehension

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}

  1. Difference Between Generators And Iterators

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

  1. Difference Between Generators And Iterators

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?

A Sample class with init method

class Person:

init method or constructor

def init(self, name): self.name = name

Sample Method

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.

init.py file^ init() function

The init method is similar to constructors in C++

and Java. Constructors are used to initialize the object’s

state.

7. Difference Between Modules and Packages in Python

Module

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.

● Generators are iterators which can execute only once.

● Every generator is an iterator.

● 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

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