Loading...

PYTHON

Python is a powerful, high-level programming language known for its simplicity and readability. It is widely used in web development, data science, automation, AI, and more.

Basic Level Questions

Q1: What is Python?
Python is a high-level, interpreted programming language known for its simple syntax and readability. It supports multiple programming paradigms.

Q2: What are variables in Python?
Variables are containers for storing data values.
Example: x = 5 stores the value 5 in the variable x.

Q3: What are Python's basic data types?
Python has int, float, str, bool, list, tuple, set, and dict as basic data types.

Q4: What is a list in Python?
A list is an ordered, mutable collection of items.
Example: my_list = [1, 2, 3]

Q5: What is a tuple?
A tuple is an ordered, immutable collection.
Example: my_tuple = (1, 2, 3)

Q6: What is a dictionary?
A dictionary stores data in key-value pairs.
Example: my_dict = {'name': 'John', 'age': 25}

Q7: What is a set?
A set is an unordered collection of unique elements.
Example: my_set = {1, 2, 3}

Q8: How do you write a comment in Python?
Using # for single-line comments.
Example: # This is a comment

Q9: What is indentation in Python?
Indentation defines blocks of code and is essential for syntax.

Q10: What is a function?
A function is a block of reusable code. Defined using def keyword.
Example:
def greet():
        print('Hello')

Q11: What is the use of the return statement?
It exits the function and returns a value to the caller.

Q12: How do you take user input?
Using input() function.
Example: name = input('Enter your name: ')

Q13: What is type casting?
Converting one data type to another.
Example: int('5') gives 5

Q14: What is the if-else statement?
Used to perform conditional execution of code.

Q15: Write a Python program to check if a number is even or odd.
Ans: num = 4
if num % 2 == 0:
         print('Even')
else:
         print('Odd')

Q16: What are loops in Python?
Loops are used to repeat a block of code. for and while loops are commonly used.

Q17: How do you write a for loop?
for i in range(5):
        print(i)

Q18: How do you write a while loop?
count = 0
while count < 5:
        print(count)
        count += 1

Q19: What is a break statement?
Used to exit the loop prematurely.

Q20: What is a continue statement?
Skips the current iteration and continues with the next one.

Intermediate Level Questions

Q1: What is a function argument?
Values passed to a function when calling it.

Q2: What are default arguments?
Function parameters that assume a default value if no argument is passed.

Q3: What is a lambda function?
An anonymous function defined with the lambda keyword.
Example: f = lambda x: x*2

Q4: What is recursion?
A function calling itself to solve smaller instances of a problem.

Q5: What are Python modules?
Files containing Python code. You can import them using import statement.

Q6: What is the difference between list and tuple?
Lists are mutable; tuples are immutable.

Q7: What is exception handling?
Mechanism to handle runtime errors using try, except blocks.

Q8: Write a program to handle division by zero exception.
try:
         x = 10 / 0
except ZeroDivisionError:
         print('Cannot divide by zero')

Q9: What is a class in Python?
A class is a blueprint for creating objects. It defines properties and behaviors.

Q10: How do you create an object of a class?
obj = ClassName()

Q11: What is __init__ method?
It is a constructor in Python classes, used for initialization.

Q12: What is inheritance?
A way to create a new class from an existing class.
Example: class Child(Parent):

Q13: What is polymorphism?
Ability to use functions or methods in different ways based on context.

Q14: What is encapsulation?
Hiding internal details of an object and only showing functionality.

Q15: What is a file in Python?
A file is used to store data permanently. Python supports reading/writing files.

Q16: How do you open a file?
Using open() function.
Example: f = open('data.txt', 'r')

Q17: How do you write to a file?
f = open('data.txt', 'w')
f.write('Hello')
f.close()

Q18: How do you read from a file?
f = open('data.txt', 'r')
data = f.read()
print(data)
f.close()

Q19: What is the difference between 'r' and 'w' mode in files?
'r' is for reading, 'w' is for writing (overwrites file).

Q20: What are Python packages?
A collection of modules organized in directories with __init__.py file.

Advanced Level Questions

Q1: What is the use of dir() function?
Lists all attributes and methods of an object.

Q2: What is the use of help() function?
Displays documentation of modules, classes, and functions.

Q3: What is the use of isinstance()?
Checks if an object is an instance of a specific class.

Q4: What is a generator?
A function that returns an iterator using yield keyword.

Q5: What is a decorator?
A function that modifies another function's behavior.

Q6: What is global and local scope?
Global is outside all functions, local is inside a function.

Q7: What is the difference between deep copy and shallow copy?
Shallow copies outer object; deep copy copies all nested objects too.

Q8: What is the use of pass statement?
A placeholder that does nothing.

Q9: What is the difference between None and 0?
None is a null value; 0 is an integer.

Q10: What are Python libraries?
Collections of modules and functions (e.g., NumPy, Pandas, Matplotlib).