0% found this document useful (0 votes)
4 views6 pages

Program To Demonstrate List in Python

The document provides examples of various data structures in Python, including lists, tuples, sets, and dictionaries, along with their characteristics and operations. It also includes programs demonstrating loops, Armstrong number checks, palindrome checks, and prime number checks. Key differences between lists and tuples are highlighted, emphasizing mutability and usage scenarios.
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)
4 views6 pages

Program To Demonstrate List in Python

The document provides examples of various data structures in Python, including lists, tuples, sets, and dictionaries, along with their characteristics and operations. It also includes programs demonstrating loops, Armstrong number checks, palindrome checks, and prime number checks. Key differences between lists and tuples are highlighted, emphasizing mutability and usage scenarios.
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

# Program to demonstrate List in Python

# Creating a list

marks = [78, 85, 90, 66]

print("List of marks:", marks)

# Accessing elements

print("First mark:", marks[0])

# Modifying elements (Lists are mutable)

marks[2] = 95

print("Updated list:", marks)

# Adding an element

[Link](88)

print("List after adding a new mark:", marks)

# Length of list

print("Number of elements in list:", len(marks))


# Program to demonstrate Tuple in Python

# Creating a tuple

student = ("Barkha", 20, "[Link]")

print("Student details:", student)

# Accessing elements

print("Student name:", student[0])

# Length of tuple

print("Number of elements in tuple:", len(student))

# Trying to modify tuple (will cause error if uncommented)

# student[1] = 21

Difference

Feature List Tuple

Brackets [] ()

Mutable Yes No

Used when Data changes Data is fixed


# Program to demonstrate for loop over a sequence

subjects = ["Python", "Java", "C", "Cyber Security"]

for sub in subjects:

print(sub)

Output:

Python

Java

Cyber Security

# Program to print countdown using while loop

num = int(input("Enter a number: "))

while num >= 0:

print(num)

num = num – 1

Output:

Enter a number: 5

3
2

# Program to demonstrate Set in Python

# Creating a set

numbers = {10, 20, 30, 40, 20}

print("Set elements:", numbers)

# Adding an element

[Link](50)

print("After adding an element:", numbers)

# Removing an element

[Link](10)

print("After removing an element:", numbers)

# Program to demonstrate Dictionary in Python

# Creating a dictionary

student = {

"Name": "Barkha",

"Age": 20,

"Course": "[Link]"

}
print("Student details:", student)

# Accessing values

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

# Updating value

student["Age"] = 21

print("Updated dictionary:", student)

# Program to check Armstrong number

num = int(input("Enter a number: "))

temp = num

sum = 0

while temp > 0:

digit = temp % 10

sum = sum + digit**3

temp = temp // 10

if sum == num:

print(num, "is an Armstrong number")

else:

print(num, "is not an Armstrong number")

# Program to check Palindrome number

num = int(input("Enter a number: "))


temp = num

rev = 0

while temp > 0:

digit = temp % 10

rev = rev * 10 + digit

temp = temp // 10

if num == rev:

print(num, "is a Palindrome number")

else:

print(num, "is not a Palindrome number")

# Program to check Prime number

num = int(input("Enter a number: "))

count = 0

for i in range(1, num + 1):

if num % i == 0:

count = count + 1

if count == 2:

print(num, "is a Prime number")

else:

print(num, "is not a Prime number")

You might also like