Python is a versatile and beginner-friendly programming language that is a core part of the CBSE Computer Science (CS) and Informatics Practices (IP) curriculum for Class 11th and 12th. At ITXperts, we believe in strengthening the basics of students and building a strong foundation in Python programming.
Here are 50 essential Python questions and answers designed to help CBSE students ace their exams and deepen their understanding of core Python concepts:
1. What is Python?
Python is a high-level, interpreted, general-purpose programming language known for its simplicity and readability. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
2. What are variables in Python?
Variables are containers for storing data values. In Python, variables are dynamically typed, meaning you don’t need to declare their type.
Example:
x = 5
name = "John"
3. How do you take input from the user in Python?
You can use the input()
function to take input from the user.
Example:
name = input("Enter your name: ")
print("Hello, " + name)
4. What are data types in Python?
Common data types in Python include:
int
for integersfloat
for floating-point numbersstr
for stringsbool
for boolean valueslist
,tuple
,dict
,set
for collections
5. What is type casting in Python?
Type casting is the conversion of one data type to another, such as converting a string to an integer using int()
.
Example:
num = int(input("Enter a number: "))
6. What are comments in Python and how do you write them?
Comments are used to explain the code and are ignored by the interpreter. You can write a comment using the #
symbol.
Example:
# This is a comment
7. What is the difference between print()
and return
?
print()
displays the output on the screen, while return
is used inside a function to send a value back to the caller.
8. How do you define a function in Python?
You can define a function using the def
keyword.
Example:
def greet():
print("Hello!")
9. What is the difference between a local and a global variable?
A local variable is declared inside a function and can only be accessed within that function, while a global variable is declared outside all functions and can be accessed anywhere in the program.
10. How do you create a list in Python?
A list in Python can be created using square brackets []
.
Example:
my_list = [1, 2, 3, 4, 5]
11. How do you access elements in a list?
You can access elements using their index.
Example:
print(my_list[0]) # Output: 1
12. What is slicing in Python?
Slicing is used to get a subset of a list, string, or tuple by specifying a start and end index.
Example:
print(my_list[1:4]) # Output: [2, 3, 4]
13. What are loops in Python?
Loops allow you to execute a block of code repeatedly. Python supports for
and while
loops.
14. Write a simple for
loop to print numbers from 1 to 5.
for i in range(1, 6):
print(i)
15. What is the while
loop?
A while
loop repeats as long as a condition is true.
Example:
i = 1
while i <= 5:
print(i)
i += 1
16. How do you handle exceptions in Python?
You can handle exceptions using the try-except
block.
Example:
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input!")
17. What is a dictionary in Python?
A dictionary is a collection of key-value pairs.
Example:
my_dict = {"name": "John", "age": 25}
18. How do you add elements to a dictionary?
You can add elements using a new key.
Example:
my_dict["city"] = "New York"
19. How do you remove an element from a dictionary?
You can remove an element using the del
statement or pop()
method.
Example:
del my_dict["age"]
20. What are tuples in Python?
Tuples are immutable sequences, meaning their elements cannot be changed after creation.
Example:
my_tuple = (1, 2, 3)
21. What is the difference between a list and a tuple?
Lists are mutable, while tuples are immutable. You can modify elements in a list but not in a tuple.
22. How do you create a function with arguments in Python?
You can pass parameters inside the parentheses of the function definition.
Example:
def add(a, b):
return a + b
23. What are default arguments in Python functions?
Default arguments are values that are used if no argument is passed.
Example:
def greet(name="Guest"):
print("Hello, " + name)
24. What is a lambda function in Python?
A lambda function is a small anonymous function defined using the lambda
keyword.
Example:
x = lambda a: a + 10
print(x(5)) # Output: 15
25. What are conditional statements in Python?
Conditional statements (if
, elif
, else
) are used to perform different actions based on conditions.
Example:
if x > 10:
print("x is greater than 10")
else:
print("x is 10 or less")
26. What is the difference between is
and ==
in Python?
==
checks if the values of two objects are equal.is
checks if two objects refer to the same location in memory.
27. What are Python modules?
Modules are files containing Python code, including functions, classes, or variables that can be imported into other programs.
28. How do you import a module in Python?
You can import a module using the import
statement.
Example:
import math
29. What is recursion in Python?
Recursion is a process where a function calls itself.
Example:
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
30. What is the purpose of the break
statement in Python?
The break
statement is used to exit a loop prematurely.
31. What is the continue
statement?
continue
skips the current iteration and moves to the next iteration of the loop.
32. What is list comprehension in Python?
List comprehension is a concise way to create lists.
Example:
squares = [x**2 for x in range(5)]
33. What is the pass
statement in Python?
The pass
statement is a null statement that is used as a placeholder in loops, functions, or conditionals.
34. How do you find the length of a list in Python?
You can find the length using the len()
function.
Example:
print(len(my_list))
35. What is a set
in Python?
A set is an unordered collection of unique elements.
36. How do you convert a list to a set?
You can convert a list to a set using the set()
function.
Example:
my_set = set(my_list)
37. What is a file in Python, and how do you open one?
A file is a collection of data. You can open a file using the open()
function.
Example:
file = open("example.txt", "r")
38. How do you write to a file in Python?
You can write to a file using the write()
method.
Example:
file = open("example.txt", "w")
file.write("Hello, World!")
39. What is the purpose of the with
statement in file handling?
The with
statement automatically closes the file after the block of code is executed.
40. What are classes and objects in Python?
A class is a blueprint for creating objects, and an object is an instance of a class.
41. How do you define a class in Python?
You define a class using the class
keyword.
Example:
class MyClass:
def __init__(self, name
):
self.name = name
42. What is inheritance in Python?
Inheritance allows a class to inherit properties and methods from another class.
43. What is polymorphism in Python?
Polymorphism allows methods to do different things based on the object it is acting upon.
44. What is encapsulation in Python?
Encapsulation restricts access to certain data, ensuring that an object’s internal state is protected from outside interference.
45. What is a constructor in Python?
A constructor is a special method __init__()
that is automatically called when an object is created.
46. How do you check for membership in Python?
You can check membership using the in
keyword.
Example:
if 5 in my_list:
print("Found")
47. What is a virtual environment in Python?
A virtual environment is an isolated environment for Python projects, ensuring that dependencies are specific to the project.
48. What is pip in Python?
pip
is the package installer for Python, used to install and manage Python libraries.
49. What is the use of the dir()
function in Python?
The dir()
function returns a list of the attributes and methods of an object.
50. How do you handle multiple exceptions in Python?
You can handle multiple exceptions using multiple except
blocks or combining them in a tuple.
Example:
try:
num = int(input("Enter a number: "))
except (ValueError, TypeError):
print("Invalid input!")
By mastering these core Python concepts, students of CBSE Class 11th and 12th can build a strong foundation for programming and excel in their Computer Science and Informatics Practices courses. At ITXperts, we offer expert coaching to help students grasp these important concepts with clarity and confidence.