Programming with Python Solution Sessional 1
1. Enlist the features of Python and explain any two.
Features of Python: (write any four)
Easy to Learn and Use
Interpreted Language
Dynamically Typed
Platform-Independent
Object-Oriented
Extensive Libraries
Open Source and Free
High-Level Language
Automatic Memory Management
Large Community Support
Explanation of Two Features: (All two)
• Interpreted Language – Python executes code line by line without compilation.
• Dynamically Typed – No need to declare variable types; they are assigned at runtime.
x = 10 # x is an integer
x = "Hello" # x is now a string
2. What is a comment? How do you apply single-line and multi-line comments in Python?
Single-line Comment: Starts with #, e.g., # This is a comment.
Multi-line Comment: Uses triple quotes (''' or """), e.g.,
'''This is a multi-line comment'''
3. Write the use of the elif keyword in Python.
Use of elif Keyword in Python
The elif keyword is used in conditional statements to check multiple conditions. It follows an if
statement and executes only if the previous conditions are False.
Page No. 1
Programming with Python Solution Sessional 1
Example:
num = 10
if num > 10:
print("Greater than 10")
elif num == 10:
print("Equal to 10")
else:
print("Less than 10")
4. Describe the role of indentation in Python.
Indentation in Python is used to define the blocks of code. Unlike other languages that use {} or ;,
Python relies on indentation for structure. Incorrect indentation leads to errors.
Example:
if True:
print("Hello") # Proper indentation
print("Python")
Incorrect Indentation (Error)
if True:
print("Hello") # IndentationError
5. Explain membership and identity operators in Python.
Membership and Identity Operators in Python
1. Membership Operators (in, not in)
These operators check whether a value exists in a sequence (like a list, tuple, or string).
Example:
x = [1, 2, 3, 4]
print(2 in x) # True
print(5 not in x) # True
2. Identity Operators (is, is not)
These operators check if two variables refer to the same memory location (object identity).
Example:
a = 10
b = 10
print(a is b) # True (same memory location)
print(a is not b) # False
Page No. 2
Programming with Python Solution Sessional 1
6. Explain any four built-in list functions in Python.
Function Description Example Output
append() Adds an element to the end of the list. lst.append(5) [3, 1, 4, 2, 5]
Removes the first occurrence of a specified
remove() lst.remove(1) [3, 4, 2, 5]
element.
sort() Sorts the list in ascending order. lst.sort() [2, 3, 4, 5]
pop() Removes and returns the last element. lst.pop() [2, 3, 4]
Example Code:
lst = [3, 1, 4, 2]
lst.append(5)
lst.remove(1)
lst.sort()
lst.pop()
print(lst) # Output: [2, 3, 4]
7. Enlist any four data structures used in Python.
Data Structure Description
An ordered, mutable collection that allows duplicate values. Example: lst = [1,
List
2, 3, 4]
An ordered, immutable collection that allows duplicate values. Example: tup =
Tuple
(1, 2, 3, 4)
Set An unordered collection of unique elements. Example: st = {1, 2, 3, 4}
A collection of key-value pairs, where keys are unique. Example: dict = {'a': 1,
Dictionary
'b': 2}
8. What is a dictionary in Python?
Dictionary in Python
A dictionary is a built-in data structure in Python that stores data in key-value pairs. It is unordered,
mutable, and does not allow duplicate keys.
Syntax:
dict_name = {key1: value1, key2: value2}
Example:
student = {"name": "Sayyed", "college": "Jamia", "age": 20}
print(student["name"]) # Output: Sayyed
print(student["college"]) # Output: Jamia
Page No. 3
Programming with Python Solution Sessional 1
9. Compare lists and tuples (any four points).
Feature List Tuple
Mutability Mutable (can be modified) Immutable (cannot be
modified)
Syntax Defined using [] Defined using ()
Performance Slower due to dynamic resizing Faster due to fixed size
Use Case Suitable for dynamic data Suitable for fixed data
Syntax: lst = [1, 2, 3] # List tup = (1, 2, 3) # Tuple
10. Enlist arithmetic and relational operators in Python.
1.Arithmetic Operators
Operator Description Example (a = 10, b = Output
5)
+ Addition a+b 15
- Subtraction a-b 5
* Multiplication a*b 50
/ Division a/b 2.0
% Modulus a%b 0
** Exponentiation a ** b 100000
// Floor Division a // b 2
2. Relational (Comparison) Operators
Operator Description Example (a = 10, b = 5) Output
== Equal to a == b False
!= Not equal to a != b True
> Greater than a>b True
< Less than a<b False
>= Greater than or equal a >= b True
to
<= Less than or equal to a <= b False
11. Explain the while loop with the else keyword and write its syntax.
while Loop with else in Python
• The else block in a while loop executes only when the loop condition becomes False
naturally (not when exited using break).
• If the loop is terminated by break, the else block does not execute.
Page No. 4
Programming with Python Solution Sessional 1
Syntax: Example:
while condition: x=1
# Loop body while x < 5:
else: print(x)
# Executes if loop completes normally x += 1
else:
print("Loop ended naturally")
1. Write a Python program that takes a number as input and checks whether it is even or odd.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
2. Write a Python program to create a list, add five numbers, and sort them.
numbers = [] # Creating an empty list
# Adding five numbers
numbers.append(10)
numbers.append(5)
numbers.append(30)
numbers.append(20)
numbers.append(15)
numbers.sort() # Sorting the list
print("Sorted List:", numbers)
3. Write a program to print numbers from 1 to 20, skipping numbers divisible by 3.
for num in range(1, 21):
if num % 3 == 0:
continue # Skip numbers divisible by 3
print(num, end=",")
Page No. 5
Programming with Python Solution Sessional 1
4. Write a Python program to copy elements from index 1 to 3 from an existing tuple into a new
tuple.
# Original tuple
tupleA = (10, 20, 30, 40, 50)
# Slicing from index 1 to 3 (excluding index 4)
new_tuple = tupleA [1:4]
print("New Tuple:", new_tuple)
Page No. 6