Programming With Python
Programming With Python
(2 marks questions)
1. List (list)
2. Tuple (tuple)
3. Dictionary (dict)
4. Set (set)
It helps to plot line graphs, bar charts, histograms, scatter plots, etc.
Data Abstraction → Hides complex details and shows only the necessary features.
(Example: Using a function without knowing its internal working.)
Data Hiding → Restricts access to internal data using private variables (__var).
(Example: Preventing direct modification of variables in a class.)
6) Syntax of fopen in Python (open function):
Modes:
"r" → Read
"w" → Write
"a" → Append
The elif keyword (short for else if) is used to check multiple conditions in an if-else
statement. It helps reduce the need for writing multiple if statements, making the
code more readable and efficient.
Example:
num = 10
if num > 10:
print("Greater")
elif num == 10:
print("Equal")
else:
print("Smaller")
Python uses indentation (spaces or tabs) to define blocks of code instead of {} like
other languages.
Incorrect indentation causes errors.
11) Define Data Hiding concept. Write two advantages of Data Hiding.
Data Hiding means restricting direct access to object data using private variables
(__var).
Advantages:
Example:
x = 5 # Global namespace
def func():
y = 10 # Local namespace
14) State the use of read() and readline() functions in Python file handling.
example:
content = file.read()
file.close()
Example:
line = file.readline()
file.close()
15) Explain two ways to add objects/elements to a list.
Example:
my_list = [1, 2, 3]
my_list.append(4) # [1, 2, 3, 4]
Example:
x = int("10") # 10
y = str(100) # "100"
19) Default Constructor in Python:
Example:
import os
"""
This is a
multiline comment
"""
2. Multiple # lines
# This is
# a multiline
# comment
2. Script Mode – Runs saved Python files (.py) using a terminal or IDE.
LIST TUPLE
1. Lists are mutable Tuples are immutable
2. Lists consume more memory Tuple consumes less memory as compared to
the list
3. Lists have several built-in methods Tuple does not have many built-in methods.
4. The list is better for performing operations, A Tuple data type is appropriate for accessing
such as insertion and deletion. the elements
5. The implication of iterations is Time- The implication of iterations is comparatively
consuming Faster