T.Y.B.Sc.
(Computer Science)
SECC - I
CS-3510 PYTHON PROGRAMMING
Semester V
(From Academic Year 2021)
Name Ashwin Clement Pinto Roll No.12
College Christ College Division ----
Academic Year 2024-25
1|Page TYBSc CS Python WorkBook
Lab Assignments
SET A
1. Python Program to Calculate the Area of a Triangle
2. Python Program to Swap Two Variables
3. Python Program to Generate a Random Number
SET B
1. Write a Python Program to Check if a Number is Positive, Negative or Zero
2. Write a Python Program to Check if a Number is Odd or Even
3. Write a Python Program to Check Prime Number
4. Write a Python Program to Check Armstrong Number
5. Write a Python Program to Find the Factorial of a Number
PROGRAMS FOR PRACTICE:
1. Python Program to Convert Kilometers to Miles
2. Python Program to Convert Celsius To Fahrenheit
3. Write a Python Program to Check Leap Year
4. Write a Python Program to Print all Prime Numbers in an Interval
5. Write a Python Program to Print the Fibonacci sequence
6. Write a Python Program to Find Armstrong Number in an Interval
7. Write a Python Program to Find the Sum of Natural Numbers
Signature of the instructor Date 14/08/2024
Assignment Evaluation
0:Not done 2:Late Complete 4:Complete
1:Incomplete 3:Needs improvement 5:Well Done
32
Ashwin_Practical_Assignment_1_[Roll_No_12]
August 14, 2024
[1]: #Set A Q1) Python Program to Calculate the Area of a Triangle
base = float(input("Enter base of triangle: "))
height = float(input("Enter height of triangle: "))
area = 0.5 * base * height
print("Area of triangle: ", area)
Enter base of triangle: 12
Enter height of triangle: 14
Area of triangle: 84.0
[3]: #Set A Q2) Python Program to Swap Two Variables
a = 5
b = 10
print("Before swap: a =", a, "b =", b)
a, b = b, a
print("After swap: a =", a, "b =", b)
Before swap: a = 5 b = 10
After swap: a = 10 b = 5
[5]: #Set A Q3) Python Program to Generate a Random Number
import random
random_number = random.randint(1, 100)
print("Random number: ", random_number)
Random number: 48
[7]: #Set B Q1) Python Program to Check if a Number is Positive, Negative or Zero
num = int(input("Enter a number: "))
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
Enter a number: 12
1
The number is positive.
[9]: #Set B Q2) Python Program to Check if a Number is Odd or Even
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Enter a number: 12
The number is even.
[11]: #Set B Q3) Python Program to Check Prime Number
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print("The number is not prime.")
break
else:
print("The number is prime.")
else:
print("The number is not prime.")
Enter a number: 1
The number is not prime.
[13]: #Set B Q4) Python Program to Check Armstrong Number
num = int(input("Enter a number: "))
sum = 0
for digit in str(num):
sum += int(digit) ** len(str(num))
if num == sum:
print("The number is an Armstrong number.")
else:
print("The number is not an Armstrong number.")
Enter a number: 12
The number is not an Armstrong number.
[15]: #Set B Q5) Python Program to Find the Factorial of a Number
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("The factorial of", num, "is", factorial)
Enter a number: 12
2
The factorial of 12 is 479001600
[17]: #PFP Q1) Python Program to Convert Kilometers to Miles
conversion_factor = 0.621371
kilometers = float(input("Enter kilometers: "))
miles = kilometers * conversion_factor
print(f"{kilometers} kilometers is equal to {miles:.2f} miles")
Enter kilometers: 12
12.0 kilometers is equal to 7.46 miles
[19]: #PFP Q2) Python Program to Convert Celsius to Fahrenheit
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit:.2f}°F")
Enter temperature in Celsius: 12
12.0°C is equal to 53.60°F
[21]: #PFP Q3) Python Program to Check Leap Year
year = int(input("Enter a year: "))
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Enter a year: 2004
2004 is a leap year.
[23]: #PFP Q4) Python Program to Print all Prime Numbers in an Interval
start = int(input("Enter the start of the interval: "))
end = int(input("Enter the end of the interval: "))
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
for num in range(start, end + 1):
if is_prime(num):
print(num)
Enter the start of the interval: 0
Enter the end of the interval: 12
2
3
5
3
7
11
[25]: #PFP Q5) Python Program to Print the Fibonacci sequence
n = int(input("Enter the number of terms: "))
a, b = 0, 1
print("Fibonacci sequence:")
for i in range(n):
print(a, end=" ")
a, b = b, a + b
Enter the number of terms: 12
Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34 55 89
[27]: #PFP Q6) Write a Python Program to Find Armstrong Number in an Interval
def is_armstrong(n):
original = n
power = len(str(n))
sum = 0
while n > 0:
number = n % 10
sum += number ** power
n = n // 10
return original == sum
start = int(input("Enter the start of the interval: "))
end = int(input("Enter the end of the interval: "))
print("Armstrong numbers in the interval:")
for num in range(start, end + 1):
if is_armstrong(num):
print(num)
Enter the start of the interval: 0
Enter the end of the interval: 12
Armstrong numbers in the interval:
0
1
2
3
4
5
6
7
8
9
4
[29]: #PFP Q7) Function to calculate the sum of natural numbers
def sum_of_natural_numbers(n):
return n * (n + 1) // 2
n = int(input("Enter a natural number: "))
print("Sum of natural numbers up to", n, "is:", sum_of_natural_numbers(n))
Enter a natural number: 12
Sum of natural numbers up to 12 is: 78
[ ]: