0% found this document useful (0 votes)
5 views4 pages

PWP QP

The document provides a comparison between lists and tuples in Python, highlighting their mutability and syntax differences. It includes examples of comments, identity operators, a factorial function, and dictionary operations for managing student information. Additionally, it covers decision-making statements, code output examples, and various Python data types with corresponding examples.

Uploaded by

Shantanu Kaute
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

PWP QP

The document provides a comparison between lists and tuples in Python, highlighting their mutability and syntax differences. It includes examples of comments, identity operators, a factorial function, and dictionary operations for managing student information. Additionally, it covers decision-making statements, code output examples, and various Python data types with corresponding examples.

Uploaded by

Shantanu Kaute
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Q.

1) (Pick any FOUR of these)

a) Two differences between list and tuple:


1. Lists are mutable (can be modified) while tuples are immutable (cannot be modified
after creation)
2. Lists use square brackets [] while tuples use parentheses ()

b) Comments in Python:
- Single line comment: Uses # symbol
```python
# This is a single line comment
```
- Multi-line comment: Uses triple quotes
```python
"""
This is a
multi-line comment
"""
```

c) Identity operators in Python:


- is: Returns True if both variables point to same object
- is not: Returns True if both variables point to different objects

d) Program to print factorial using for loop:


```python
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result

# Example usage
num = 5
print(f"Factorial of {num} is {factorial(num)}")
```

e) Four built-in tuple functions with examples:


1. len(): Returns length of tuple
2. max(): Returns maximum value
3. min(): Returns minimum value
4. sum(): Returns sum of numeric tuple

```python
my_tuple = (1, 2, 3, 4, 5)
print(len(my_tuple)) # 5
print(max(my_tuple)) # 5
print(min(my_tuple)) # 1
print(sum(my_tuple)) # 15
```

Q.2) (Pick any THREE)


# Create an empty dictionary to store student information
students = {}

# i) Add three students to the dictionary


def add_students():
students[1] = "John"
students[2] = "Emma"
students[3] = "Shreyas"
print("After adding students:", students)

# ii) Update name of student with ROLL NO = 2


def update_student():
students[2] = "Shreyas"
print("After updating student:", students)

# iii) Delete information of ROLL NO = 1


def delete_student():
del students[1]
print("After deleting student:", students)

# Execute all operations


add_students()
update_student()
delete_student()
b) Decision making statements example:
```python
# if-else
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")

# if-elif-else
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
else:
print("C")
```

c) The output of the given code:

For `>>> a = [2, 5, 1, 3, 6, 9, 7]`:


- `>>> print(a)` will output: [2, 5, 1, 3, 6, 9, 7]
- `>>> a[2:6] = [2, 4, 9, 0]` will modify the slice, making a = [2, 5, 2, 4, 9, 0, 7]

For `>>> b = ["Hello", "Good"]`:


- `>>> b.append("python")` will add "python" to b
- `>>> print(b)` will output: ['Hello', 'Good', 'python']

For `>>> t1 = [3, 5, 6, 7]`:


- `>>> print(t1[2])` will output: 6
- `>>> print(t1[-1])` will output: 7

d) Python data types with examples:


1. Integer (int):
```python
x=5
```
2. String (str):
```python
name = "Python"
```
3. Float:
```python
price = 10.99
```
4. List:
```python
numbers = [1, 2, 3]
```
5. Dictionary:
```python
person = {"name": "John", "age": 30}
```
6. Tuple:
```python
coordinates = (10, 20)
```

You might also like