0% found this document useful (0 votes)
2 views

doc_collage_python

The document contains a series of Python practical exercises ranging from variable swapping to calculating the sum of digits in a number. Each exercise includes a code snippet, its output, and brief descriptions of the tasks, such as finding common elements in lists, displaying memory locations, and generating Fibonacci and factorial series. The exercises are designed for BCA SEM-4 students to practice fundamental programming concepts in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

doc_collage_python

The document contains a series of Python practical exercises ranging from variable swapping to calculating the sum of digits in a number. Each exercise includes a code snippet, its output, and brief descriptions of the tasks, such as finding common elements in lists, displaying memory locations, and generating Fibonacci and factorial series. The exercises are designed for BCA SEM-4 students to practice fundamental programming concepts in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PYTHON PRECTICAL- 1 to 20

2. Write a Python program to swap two variables using a


temporary variable.

def swap_variables (a, b):

temp = a
a=b
b = temp
return a, b
x=5
y = 10

print("Before swap:")
print("x =", x)
print("y =", y)

x, y = swap_variables(x, y)

print("After swap:")
print("x =", x)
print("y =", y)

ouput: python p2.py

Before swap:

x=5

y = 10

After swap:

| Hiral M. Patel (BCA SEM-4)


PYTHON PRECTICAL- 1 to 20

x = 10

y=5

3. Write a program to swap two numbers without taking a


temporary variable.

def swap_without_temp(a, b):

a=a+b
b=a-b
a=a-b
return a, b

x=5
y = 10

print("Before swap:")
print("x =", x)
print("y =", y)

x, y = swap_without_temp(x, y)

print("After swap:")
print("x =", x)
print("y =", y)

output: python p3.py

Before swap:

| Hiral M. Patel (BCA SEM-4)


PYTHON PRECTICAL- 1 to 20

x=5

y = 10

After swap:

x = 10

y=5

4. Write a program to find out and display the common and the
non-common elements in the list using membership operators.

def find_common_and_non_common(list1, list2):


common_elements = []
non_common_elements = []

for element in list1:


if element in list2:
common_elements.append(element)
else:
non_common_elements.append(element)

for element in list2:


if element not in list1 and element not in non_common_elements:
non_common_elements.append(element)

return common_elements, non_common_elements

| Hiral M. Patel (BCA SEM-4)


PYTHON PRECTICAL- 1 to 20

list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

common, non_common = find_common_and_non_common(list1, list2)

print("Common elements:", common)


print("Non-common elements:", non_common)

output: python p4.py

Common elements: [4, 5]

Non-common elements: [1, 2, 3, 6, 7, 8]

5. Create a program to display memory locations of two


variables using id() function, and then use identity operators
two compare whether two objects are same or not.

a = 10
b = 10

print("Memory location of a:", id(a))


print("Memory location of b:", id(b))

if a is b:
print("a and b refer to the same object.")
else:
print("a and b refer to different objects.")

| Hiral M. Patel (BCA SEM-4)


PYTHON PRECTICAL- 1 to 20

b = 20

print("Memory location of a after modification:", id(a))


print("Memory location of b after modification:", id(b))

if a is b:
print("a and b refer to the same object.")
else:
print("a and b refer to different objects.")

output: python p5.py

Memory location of a: 140723543700552

Memory location of b: 140723543700552

a and b refer to the same object.

Memory location of a after modification: 140723543700552

Memory location of b after modification: 140723543700872

a and b refer to different objects.

6. Write a Python Program to find area of circle.

radius = float(input("Enter the radius of the circle: "))


area = 3.14 * radius * radius
print("The area of the circle is:", area)

Output: python p6.py

| Hiral M. Patel (BCA SEM-4)


PYTHON PRECTICAL- 1 to 20

Enter the radius of the circle: 10

The area of the circle is: 314.0

7. Write a Python Program to create a sequence of numbers


using range data type to display 1 to 30, with an increment of
2.

numbers = range(1, 31, 2)

for num in numbers:


print(num)

output: python p7.py

1 3 5 7 9 11 13 15 17 19 21 23 25
27 29

8. Write a Python program to implement Factorial series up to


user entered number.

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

factorial = 1

print(f"Factorial series up to {number}:")


for i in range(1, number + 1):
factorial *= i
print(f"Factorial of {i} is {factorial}")

| Hiral M. Patel (BCA SEM-4)


PYTHON PRECTICAL- 1 to 20

output: python p8.py

Enter a number: 5

Factorial series up to 5:

Factorial of 1 is 1

Factorial of 2 is 2

Factorial of 3 is 6

Factorial of 4 is 24

Factorial of 5 is 120

9. Write a Python program to display the Fibonacci series.

n = int(input("Enter the number of terms for the Fibonacci series: "))


a, b = 0, 1

print("Fibonacci series up to", n, "terms:")

for i in range(n):
print(a, end=" ")
a, b = b, a + b

output: python p9.py

Enter the number of terms for the Fibonacci series: 5

Fibonacci series up to 5 terms:

01123

| Hiral M. Patel (BCA SEM-4)


PYTHON PRECTICAL- 1 to 20

10. Write a Python program to calculate sum of digit of given


number.

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


sum_of_digits = 0

while number > 0:


digit = number % 10
sum_of_digits += digit
number = number // 10

print("Sum of digits:", sum_of_digits)

output: python p10.py

Enter a number: 345

Sum of digits: 12

| Hiral M. Patel (BCA SEM-4)

You might also like