3rd Sem Deepak
3rd Sem Deepak
Expected Output:
Enter a year: 2024
2024 is a leap year.
2. Swap Two Numbers
Explanation:
This program swaps the values of two numbers entered by the user. It uses
Python's multiple assignment feature to achieve the swap in a single line.
Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(f"Before swapping: a = {a}, b = {b}")
a, b = b, a
print(f"After swapping: a = {a}, b = {b}")
Expected Output:
Enter first number: 5
Enter second number: 10
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
3. Factorial Calculator
Explanation:
This program calculates the factorial of a number entered by the user.
Factorial of a number is the product of all positive integers less than or equal
to that number. The program uses a loop to compute the factorial.
Code:
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}.")
Expected Output:
Enter a number: 5
The factorial of 5 is 120.
4. User Input Program
Explanation:
This program takes multiple inputs from the user: their name and age. It then
outputs a personalized message based on the input.
Code:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello, {name}! You are {age} years old.")
Expected Output:
Enter your name: John
Enter your age: 25
Hello, John! You are 25 years old.
5. List Operations
Explanation:
This program performs basic operations on a list, such as adding, removing,
sorting, and reversing elements. It demonstrates how to manipulate a list in
Python.
Code:
my_list = [1, 2, 3, 4, 5]
print("Original list:", my_list)
my_list.append(6)
print("After appending 6:", my_list)
my_list.remove(3)
print("After removing 3:", my_list)
my_list.sort(reverse=True)
print("After sorting in descending order:", my_list)
Expected Output:
Original list: [1, 2, 3, 4, 5]
After appending 6: [1, 2, 3, 4, 5, 6]
After removing 3: [1, 2, 4, 5, 6]
After sorting in descending order: [6, 5, 4, 2, 1]
6. Prime Numbers in a Range
Explanation:
This program identifies and prints all prime numbers between 2 and a user-
defined number. Prime numbers are greater than 1 and have no divisors other
than 1 and themselves.
Code:
n = int(input("Enter the value of n: "))
print("Prime numbers from 2 to", n, ":")
for num in range(2, n + 1):
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
break
else:
print(num, end=" ")
Expected Output:
Enter the value of n: 10
Prime numbers from 2 to 10:
7. Fibonacci Series Generator
Explanation:
This program generates a Fibonacci series up to a specified number of terms.
The series starts with 0 and 1, and each subsequent number is the sum of the
two preceding ones.
Code:
n = int(input("Enter the number of terms: "))
a, b = 0, 1
print("Fibonacci Series:", end=" ")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
Expected Output:
Enter the number of terms: 5
Fibonacci Series: 0 1 1 2 3
8. Using a For Loop
Explanation:
This program demonstrates the use of a for loop to iterate over a list of
numbers, calculate their square, and print the results.
Code:
numbers = [1, 2, 3, 4, 5]
print("Number - Square")
for num in numbers:
print(f"{num} - {num**2}")
Expected Output:
Number - Square
1-1
2-4
3-9
4 - 16
5 - 25
9. Type Conversion
Explanation:
This program demonstrates type conversion in Python. It converts a number
from a string to an integer, a float, and a boolean, showing how data types can
be manipulated.
Code:
data = input("Enter a number: ")
int_data = int(data)
float_data = float(data)
bool_data = bool(int_data)
print(f"Integer: {int_data}, Float: {float_data}, Boolean: {bool_data}")
Expected Output:
Enter a number: 5
Integer: 5, Float: 5.0, Boolean: True
10. Functions in Python
Explanation:
This program defines and uses a function to calculate the square of a number.
Functions help in reusing code and structuring programs efficiently.
Code:
def square(num):
return num * num
Expected Output:
Enter a number: 4
The square of 4 is 16.