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

Python Lab

Uploaded by

vinitsaini991151
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 views9 pages

Python Lab

Uploaded by

vinitsaini991151
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

Experiment-6

Aim: Write a Python program to create a set and a frozen set and display its
elements. Add elements to the set using add() function, remove elements
using remove() function, and add multiple elements using update() function.

Source code
my_set = {1, 9, 3, 8}
print("Original set:", my_set)
frozen = frozenset([100, 200, 300, 400])
print("Frozen set:", frozen)

my_set.add(5)
print("After adding 5:", my_set)
my_set.remove(9)
print("After removing 9:", my_set)
my_set.update([6, 7, 8])
print("After updating multiple elements:", my_set)

Output
Experiment-7
Aim: Write a Python program to create a dictionary with student names and
marks. Access values using keys, add a new key-value pair, and delete an
existing key.

Source code
student = {
"Vinit": 85,
"Rahul": 90,
"Amit": 78
}
print("Original dictionary:", student)

print("Marks of Rahul:", student["Rahul"])


student["Neha"] = 88
print("After adding Neha:", student)
del student["Amit"]
print("After deleting Amit:", student)

Output
Experiment-8
Aim: Write a Python program to check whether a substring exists in a main
string or not.

Source code
main_string = input("Enter main string: ")
substring = input("Enter substring: ")

if substring in main_string:
print("Exist")
else:
print("Doesn't exist")

Output
Experiment-9
Aim: Write a Python program to demonstrate command line arguments
by obtaining two numbers from the command line and displaying their
sum.

Source code
import sys
num1 = int([Link][1])
num2 = int([Link][2])
result = num1 + num2
print("First number:", num1)
print("Second number:", num2)
print("Sum =", result)

Output
Experiment-10
Aim: Write a Python program using argument parser to find the square of a
given number.

Source code
import argparse

parser = [Link]()

parser.add_argument("number", type=int, help="Enter a number") args =


parser.parse_args()

square = [Link] ** 2

print("Square =", square)

Output
Experiment-11
Aim: Write a Python program to find the power of a number when it is raised
to a particular power using command line arguments.

Source code
import sys base = int([Link][1])

power = int([Link][2])

result = base ** power

print("Base =", base)

print("Power =", power)

print("Result =", result)

Output
Experiment-12
Aim: Write a Python program to display the output using different
formatting options:

(a) Right alignment

Source code
text = "Python"

print("Right aligned:")

print(f"{text:>20}")

Output
(b)Left allignment

Source code
text = "Python"
print("Left aligned:")
print(f"{text:<20}")

Output

(c) Up to 5 decimal places

Source code
num = 12.3456789
print("5 decimal places:")
print(f"{num:.5f}")

Output
(d) Using f-strings

Source code

name = "Vinit"
age = 19
print(f"My name is {name} and I am {age} years old.")

Output

(e) Display name and salary using replacement fields

Source code

name = "Vinit"
salary = 25000
print("Name: {}, Salary: {}".format(name, salary))

Output

You might also like