0% found this document useful (0 votes)
9 views2 pages

Python Assignment Solution

The document contains Python code snippets demonstrating basic set operations, swapping two numbers without using a third variable, and calculating the factorial of a user-input number. It includes examples of creating a set, adding and removing elements, and performing arithmetic operations. Each section is accompanied by print statements to display the results.

Uploaded by

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

Python Assignment Solution

The document contains Python code snippets demonstrating basic set operations, swapping two numbers without using a third variable, and calculating the factorial of a user-input number. It includes examples of creating a set, adding and removing elements, and performing arithmetic operations. Each section is accompanied by print statements to display the results.

Uploaded by

danikhattak31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Assignment Solution

1–5: Set Operations Program


# Create a set
my_set = {1, 2, 3, 4}

# Print all elements


print("Elements in set:", my_set)

# Add an element
my_set.add(5)
print("After adding 5:", my_set)

# Find length
print("Length of set:", len(my_set))

# Remove an element
my_set.remove(3)
print("After removing 3:", my_set)

Swap Two Numbers (Without Third Variable)


a=5
b = 10

print("Before swap:", a, b)

# Swapping
a, b = b, a

print("After swap:", a, b)

Factorial Program (User Input)


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

fact = 1

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


fact *= i

print("Factorial =", fact)

You might also like