python updated questions
python updated questions
Program:
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print("Before swapping: num1 =", num1, "num2 =", num2)
num1, num2 = num2, num1
print("After swapping: num1 =", num1, "num2 =", num2)
Sample Input:
Enter the first number: 10
Enter the second number: 20
Sample Output:
Before swapping: num1 = 10 num2 = 20
After swapping: num1 = 20 num2 = 10
Program:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
Sample Input:
Enter a number: 7
Sample Output:
7 is Odd
3. Find the Sum of Digits of a Number
Program:
num = int(input("Enter a number: "))
sum_of_digits = 0
Sample Input:
Enter a number: 123
Sample Output:
Sum of digits: 6
Program:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
Sample Input:
Enter first number: 10
Enter second number: 5
Sample Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
5. Find the Largest of Three Numbers
Program:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
Sample Input:
Enter first number: 10
Enter second number: 25
Enter third number: 15
Sample Output:
Largest number: 25
6. List Operations
Program:
# List Creation
my_list = [10, 20, 30, 40]
# Accessing an Element
element = my_list[1] # Accessing second element
# Adding an Element
my_list.append(50)
# Deleting an Element
del my_list[2] # Removing the third element
print("Updated List:", my_list)
Sample Input:
Initial List: [10, 20, 30, 40]
Sample Output:
Updated List: [10, 20, 40, 50]
Program:
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial of", num, "is", fact)
Sample Input:
Enter a number: 5
Sample Output:
Factorial of 5 is 120
Program:
n = int(input("Enter the number of terms: "))
a, b = 0, 1
print("Fibonacci Series:", a, b, end=" ")
for _ in range(n - 2):
a, b = b, a + b
print(b, end=" ")
print()
Sample Input:
Enter the number of terms: 6
Sample Output:
Fibonacci Series: 0 1 1 2 3 5
9. Tuple Operations
Program:
# Tuple Creation
my_tuple = (5, 15, 25, 35)
# Accessing an Element
element = my_tuple[2] # Accessing third element
Sample Input:
Tuple: (5, 15, 25, 35)
Sample Output:
Accessed Element: 25
Program:
# Dictionary Creation
my_dict = {"name": "Alice", "age": 25, "city": "New York"}
# Accessing a Value
age = my_dict["age"]
Sample Input:
Initial Dictionary: {"name": "Alice", "age": 25, "city": "New York"}
Sample Output:
Updated Dictionary: {'name': 'Alice', 'age': 25, 'country': 'USA'}
Program:
# Set Creation
my_set = {1, 2, 3, 4}
# Adding an Element
my_set.add(5)
# Deleting an Element
my_set.remove(3) # Removes 3 from the set
Sample Input:
Initial Set: {1, 2, 3, 4}
Sample Output:
Element: 1
Element: 2
Element: 3
Element: 4
Updated Set: {1, 2, 4, 5}
12. Reverse a Number
Program:
num = int(input("Enter a number: "))
reversed_num = 0
Sample Input:
Enter a number: 1234
Sample Output:
Reversed Number: 4321
Program:
string = input("Enter a string: ").lower()
vowels = "aeiou"
count = sum(1 for char in string if char in vowels)
print("Number of vowels:", count)
Sample Input:
Enter a string: Hello World
Sample Output:
Number of vowels: 3
14. Convert Decimal to Binary
Program:
num = int(input("Enter a decimal number: "))
binary = bin(num)[2:]
print("Binary representation:", binary)
Sample Input:
Enter a decimal number: 10
Sample Output:
Binary representation: 1010
Program:
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Sample Input:
Enter a year: 2024
Sample Output:
2024 is a leap year