Python Shot Interview
Python Shot Interview
Basic Concepts:
o Python code is executed line by line by an interpreter, meaning Python doesn’t need
to be compiled before running like other languages (e.g., C or Java).
o Indentation means leaving spaces at the beginning of a line of code. In Python, this is
important because it tells Python which lines of code belong to which blocks, like
loops or functions. Without proper indentation, Python won’t understand your code.
o Data types are different kinds of values in Python. Some common data types are:
o We need different data types because different types of data behave differently. For
example, numbers can be added or multiplied, but strings (text) are used for writing
words or sentences.
o You can use the type() function to check the data type of a variable. Example: type(5)
will return int because 5 is a number.
o A set is a collection of unique items. It does not allow duplicates, and the items are
unordered, meaning the order doesn’t matter.
o A list is mutable (can be changed) and ordered, while a tuple is immutable (cannot
be changed) and also ordered. Lists use square brackets [], and tuples use
parentheses ().
o A dictionary stores data in key-value pairs. For example, {"name": "Alice", "age": 25}
is a dictionary where "name" is the key, and "Alice" is the value.
o You can access elements by their index. For example, my_list[0] will give you the first
element in my_list.
o You can use the int() function to convert a string to an integer. Example: int("5") will
give you the number 5.
o You can use the list() function or split() method. Example: "hello".split() will give ['h',
'e', 'l', 'l', 'o'].
Python Functions:
o Functions help you organize and reuse code. Instead of writing the same code
multiple times, you can write a function and use it whenever needed.
o Functions make your code cleaner, easier to manage, and prevent duplication, which
makes your program run more efficiently.
o Indexing refers to accessing an element in a list or string using its position. Example:
my_list[0] accesses the first element.
o Negative indexing lets you count from the end. Example: my_list[-1] gives you the
last element.
o Slicing allows you to extract parts of a list or string. Example: my_list[1:4] gives you a
new list with elements from index 1 to 3.
Operators:
Object-Oriented Programming:
o You check if the year is divisible by 4 but not by 100, unless it’s also divisible by 400.
You can use an if statement to do this.
o An Armstrong number is a number that is equal to the sum of its digits raised to the
power of the number of digits. Example: 153 is an Armstrong number because
13+53+33=1531^3 + 5^3 + 3^3 = 15313+53+33=153.
o A palindrome reads the same forward and backward. You can check if string ==
string[::-1].
o A prime number is only divisible by 1 and itself. You can use a loop to check if it has
any divisors other than 1 and itself.
Math Functions:
o The factorial of a number is the product of all numbers from 1 to that number. You
can calculate it using a loop or the math.factorial() function.
o You can use the random module to generate random numbers. Example:
random.randint(1, 10) generates a random integer between 1 and 10.
o The Fibonacci series starts with 0 and 1, and each next number is the sum of the
previous two numbers. You can print it using a loop or recursion.
o You can swap two elements like this: list[index1], list[index2] = list[index2],
list[index1].
Python Syntax:
o Indentation tells Python where blocks of code start and end. If indentation is wrong,
Python won’t understand the code.
o A module is a single Python file with functions and classes. A package is a collection
of modules in a folder.
Advanced Python:
33. What are decorators in Python, and how are they used?
o A decorator is a function that modifies the behavior of another function. It’s used to
add functionality without changing the original code.
o Exception handling helps you deal with errors in your program. You use try and
except blocks to catch and handle errors.
35. What is inheritance in Python?
o Inheritance allows a class to inherit properties and methods from another class. It
helps you reuse code.
o == checks if two values are equal. is checks if two variables point to the same object
in memory.
37. What are generators in Python, and why are they useful?
o Generators are functions that return values one at a time using the yield keyword.
They are useful for handling large data sets because they don’t store the entire result
in memory.
38. What is the difference between shallow copy and deep copy in Python?
o A shallow copy copies only the references to objects, while a deep copy creates a
new copy of the objects.
Python Expressions:
o Lambda expressions are small, anonymous functions written in one line. Example:
lambda x: x * 2.
o pass is a placeholder when you don’t want to write any code in a block but want to
avoid an error.
Common Tasks:
Numpy:
47. How do you access, delete, and update elements in a NumPy array?
o You access elements with array[index], delete with np.delete(), and update by
assigning new values to specific indices.
List Operations:
o You can combine two lists with the + operator. Example: list1 + list2.
49. How do you calculate the square of all elements in a list in Python?
String Operations:
o split() breaks a string into a list, while join() combines a list of strings into a single
string.