Unit3 Python
Unit3 Python
Marks:02
Synatx:
list_name.sort()
Example
numbers.sort()
print(numbers)
2) What is dictionary?
Answer:
A dictionary is an unordered collection of key-value pairs, where each key is unique. It's
used to store data in a way that allows fast retrieval using a key.
Key Characteristics:
i) >>>indices[:4]
Output: ['zero', 'one', 'two', 'three']
ii) >>>indices[:-2]
Marks (4 to 6)
OR
i) Create set
ii) Access set Element
iii) Update set
Iv ) Delete set
Answer:
my_set = {1, 2, 3, 4, 5}
print(element)
dict1⋅update(dict2);
for key, values in dictl⋅items( ):
Answer:
Google 1
Facebook 2
Microsoft 2
GFG 1
Youtube 3
5(List data types used in python. Explain any two with example
1. List
2. Tuple
3. Set
4. Dictionary
5. String
1. List
A list is an ordered collection of items, which can be of different data types. Lists are
mutable (can be changed).
my_list.append("Python")
print(my_list)
2. Tuple
A tuple is an ordered collection of items, similar to a list, but immutable (cannot be changed
after creation).
print(my_tuple)
7) Write the output for the following if the variable course = “Python ”
OR
>>> course [ 3 : ]
>>> course [ 2 : 2 ]
>>> course [ : ]
>>> course [ -1 ]
>>> course [ 1 ]
Answer:
Output: "Pyt"
Output: "hon"
Output: "Python"
Output: "n"
Output: "y"
ANSWER:
(1, 2, 2, 3).count(2) → 2
i) >>> a=[2,5,1,3,6,9,7]
>>> a[2:6]=[2,4,9,0]
>>> print(a)
Output: [2, 5, 2, 4, 9, 0, 7]
>>> b.append(“python”)
>>>print(b)
iii) >>>t1=[3,5,6,7]
output: >>>print(t1[2])
>>>6
>>>print(t1[-1])
>>>7
>>>print(t1[2:])
>>>[6, 7]
>>>print(t1[:])
>>>[3, 5, 6, 7]
Answer:
Basic List Operations :
List Dictionary
Ordered collection of items Collection of key-value pairs
Access by index Access by key
Syntax: [1, 2, 3] Syntax: {'a': 1, 'b': 2}
Keys must be unique
Duplicate items are allowed
a = [1, 2, 3]
print(b) # [1, 2, 3, 4]
Definition: Their contents cannot be changed once created. Any “modification” produces a
new object.
Common Examples:
o Integers (int)
o Floats (float)
o Strings (str)
o Tuples (tuple)
s = "hello"
print(t) # "hello"
OR
Explain different functions or ways to remove key : value pair from Dictionary
# Keys and values can be any immutable type (strings, numbers, tuples)
student = {
"name": "Alice",
"age": 23,
"major": "Physics"
product = {
"id": 101,
"name": "Laptop",
"price": 799.99,
"in_stock": True
# 2. Access
print(product["name"]) # Laptop
print(product.get("discount")) # None
print(product.get("discount", 0)) # 0
# 3. Iterate
print(f"{key}: {val}")
14)Write a python program to input any two tuples and interchange the
tuple variables.
# Swap them
# Show results
print("After swapping:")
factorial = 1
factorial *= i
print(f"{num}! = {factorial}")
if s == s[::-1]:
print("Palindrome")
else:
print("Not a palindrome")
18) Write a Python Program to accept values from user in a list and find the
largest number and smallest number in a list.
numbers = []
for _ in range(n):
numbers.append(num)
19) Explain use of Pass and Else keyword with for loops in python
pass Keyword:
Example:
for i in range(5):
if i == 3:
pass # Do nothing when i is 3
else:
print(i)
output:
0
1
2
4
else with for loop:
The else runs only if the loop completes normally (no break).
Useful for searching logic — "not found" scenarios.
for i in range(5):
if i == 7:
print("Found!")
break
else:
print("Not found.")
4 6 8
10 12 14 16 18