Python Assignment Solutions
1. Define the following terms with examples
a) Variable:
A variable is a name given to a memory location to store data.
Example:
x = 10
name = "Alice"
b) Data type:
It specifies the kind of value a variable holds.
Example:
age = 20 (Integer)
pi = 3.14 (Float)
c) Identifier:
It is the name used to identify variables, functions, classes, etc.
Example: student_name = "John"
d) Tuple:
Tuple is an immutable, ordered collection of items.
Example:
student = ("Asha", 20, "Biology")
2. Difference between List and Tuple (7 differences)
List:
- Mutable
- Uses []
- Slower than tuple
- Can add/remove elements
Python Assignment Solutions
- Uses more memory
- Suitable for dynamic data
- Example: [1, 2, 3]
Tuple:
- Immutable
- Uses ()
- Faster than list
- Cannot add/remove
- Uses less memory
- Suitable for fixed data
- Example: (1, 2, 3)
3. Five built-in data types in Python with examples
int -> num = 100
float -> pi = 3.14
str -> name = "Alice"
list -> marks = [90, 80, 70]
tuple -> student = ("John", 20)
4. Python program with list operations
marks = [85, 78, 92, 88, 76]
# a) 3rd subject
print("3rd subject marks:", marks[2])
# b) Replace 2nd
marks[1] = 80
Python Assignment Solutions
# c) Add two
[Link](90)
[Link](95)
# d) Sort
[Link]()
print("Sorted marks:", marks)
5. Tuple operations
info = ("Asha", 20, "Biology")
# a) Name & subject
print(info[0], info[2])
# b) Modify age
info[1] = 21
Error: TypeError: 'tuple' object does not support item assignment
6. Python program to take 3 inputs
data = []
for i in range(3):
value = input("Enter value: ")
[Link](value)
print("The list is:", data)