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

python updated questions

The document contains a collection of Python programs demonstrating various programming concepts such as swapping numbers, checking even or odd, summing digits, and performing operations with lists, tuples, dictionaries, and sets. Each program includes sample input and output to illustrate its functionality. Additionally, it covers topics like finding factorials, generating Fibonacci series, reversing numbers, counting vowels, converting decimal to binary, and checking leap years.

Uploaded by

Thiran Dami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python updated questions

The document contains a collection of Python programs demonstrating various programming concepts such as swapping numbers, checking even or odd, summing digits, and performing operations with lists, tuples, dictionaries, and sets. Each program includes sample input and output to illustrate its functionality. Additionally, it covers topics like finding factorials, generating Fibonacci series, reversing numbers, counting vowels, converting decimal to binary, and checking leap years.

Uploaded by

Thiran Dami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python Programs

1. Swap Two Numbers Without Using a Temporary Variable

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

2. Check Whether a Number is Even or Odd

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

while num > 0:


sum_of_digits += num % 10
num //= 10

print("Sum of digits:", sum_of_digits)

Sample Input:
Enter a number: 123

Sample Output:
Sum of digits: 6

4. Demonstrate Python Operators with Examples

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: "))

if a >= b and a >= c:


print("Largest number:", a)
elif b >= a and b >= c:
print("Largest number:", b)
else:
print("Largest number:", c)

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]

7. Find the Factorial of a Number

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

8. Generate Fibonacci Series up to N Terms

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

# Tuples are immutable, so we cannot add or delete elements directly.


print("Accessed Element:", element)

Sample Input:
Tuple: (5, 15, 25, 35)

Sample Output:
Accessed Element: 25

10. Dictionary Operations

Program:
# Dictionary Creation
my_dict = {"name": "Alice", "age": 25, "city": "New York"}

# Accessing a Value
age = my_dict["age"]

# Adding a New Key-Value Pair


my_dict["country"] = "USA"

# Deleting a Key-Value Pair


del my_dict["city"]
print("Updated Dictionary:", my_dict)

Sample Input:
Initial Dictionary: {"name": "Alice", "age": 25, "city": "New York"}

Sample Output:
Updated Dictionary: {'name': 'Alice', 'age': 25, 'country': 'USA'}

11. Set Operations

Program:
# Set Creation
my_set = {1, 2, 3, 4}

# Accessing Elements (Sets are unordered, so we use loops)


for item in my_set:
print("Element:", item)

# Adding an Element
my_set.add(5)

# Deleting an Element
my_set.remove(3) # Removes 3 from the set

print("Updated Set:", my_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

while num > 0:


reversed_num = reversed_num * 10 + num % 10
num //= 10

print("Reversed Number:", reversed_num)

Sample Input:
Enter a number: 1234

Sample Output:
Reversed Number: 4321

13. Count the Number of Vowels in a String

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

15. Check if a Year is a Leap Year

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

You might also like