0% found this document useful (0 votes)
2 views3 pages

Python 5 Mark Programs

The document contains Python programs demonstrating the use of tuples, dictionaries, and sets. It shows how to create, access, modify, and perform operations on these data structures. Each section includes code examples and their corresponding outputs.

Uploaded by

prajwalkhot39
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)
2 views3 pages

Python 5 Mark Programs

The document contains Python programs demonstrating the use of tuples, dictionaries, and sets. It shows how to create, access, modify, and perform operations on these data structures. Each section includes code examples and their corresponding outputs.

Uploaded by

prajwalkhot39
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/ 3

16.

Write a program to demonstrate Tuple

Python Program:

```python

# Creating a tuple

student_info = ("Alice", 21, "BCA")

# Accessing elements

print("Name:", student_info[0])

print("Age:", student_info[1])

print("Course:", student_info[2])

# Tuple is immutable - this will raise an error

# student_info[1] = 22

# Using count() and index()

marks = (85, 90, 85, 95)

print("Count of 85:", marks.count(85))

print("Index of 95:", marks.index(95))

```

Output:

Name: Alice

Age: 21

Course: BCA

Count of 85: 2

Index of 95: 3

17. Write a program to demonstrate Dictionary

Python Program:

```python

# Creating a dictionary

student = {

"name": "Bob",

"age": 20,

"course": "Python"
}

# Accessing values

print("Name:", student["name"])

# Adding a new key-value pair

student["grade"] = "A"

# Modifying a value

student["age"] = 21

# Iterating over dictionary

for key, value in student.items():

print(key, ":", value)

```

Output:

Name: Bob

name : Bob

age : 21

course : Python

grade : A

18. Write a program to demonstrate Set

Python Program:

```python

# Creating a set

colors = {"red", "green", "blue"}

# Adding elements

colors.add("yellow")

# Removing elements

colors.discard("green")
# Set operations

a = {1, 2, 3}

b = {3, 4, 5}

print("Union:", a | b)

print("Intersection:", a & b)

# Traversing a set

for color in colors:

print("Color:", color)

```

Output:

Union: {1, 2, 3, 4, 5}

Intersection: {3}

Color: red

Color: blue

Color: yellow

You might also like