Py 1204
Py 1204
float, string).
integer_var = 10
float_var = 5.5
string_var = "Hello"
print(f"Integer: {integer_var}, Float: {float_var}, String: {string_var}")
OUTPUT-
OUTPUT-
Addition: 15, Subtraction: 5, Multiplication: 50, Division: 2.0
OUTPUT-
my_variable: 10, another_var: 20
OUTPUT-
Expression result: 8
This is a statement
OUTPUT-
This will execute
This will also execute
OUTPUT-
a: 1 , b: 2.5 , c: Python
OUTPUT-
+: 13 , - : 7 , * : 30 , / : 3.3333333333333335 , % : 1 , ** : 1000
OUTPUT-
Enter a number: -9
-9.0 is negative!
-9.0 is a rational integer!
Enter a number: 89
89.0 is positive!
89.0 is a rational integer!
11. Write a program to find the largest of two numbers using if-else.
a, b = map(int, input("Enter two numbers: ").split())
if a > b:
print(f"Largest: {a}")
else:
print(f"Largest: {b}")
OUTPUT-
Enter two numbers: 5 3
Largest: 5
12. Write a program to check voting eligibility (age >= 18) using if-else.
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote")
else: print("You are not eligible to vote")
OUTPUT-
Enter your age: 76
OUTPUT-
Enter your score: 34
Grade: F
OUTPUT-
Enter a number: 71
71 has remainder 2 when divided by 3
OUTPUT-
Enter three sides: 6 6 7
Isosceles triangle
OUTPUT-
Enter a character: G
G is uppercase
Enter a character: F
F is uppercase
OUTPUT-
Enter a year: 2009
2009 is not a leap year
Enter a year: 2004
2004 is a leap year
OUTPUT-
Enter two strings: 5 5
Strings are equal
OUTPUT-
Enter a number: 3542
3542 is above range
Enter a number: 5
5 is below ra
20.Print numbers 1 to 10 using a while loop:
i=1
while i <= 10:
print(i)
i += 1
OUTPUT-
1,2,3,4,5,6,7,8,9,10
OUTPUT-
Enter a number: 3
OUTPUT-
Enter a number: 8
2,4,6,8
OUTPUT-
OUTPUT-
Enter a number: 57
Reversed number: 75
OUTPUT-
Enter a number: 5
Factorial of 5 is: 120
OUTPUT-
**
***
****
*****
OUTPUT-
Enter a number: 54
Number of digits: 2
OUTPUT-
Enter a number: 5
2
1
OUTPUT-
Enter a number: 7
Sum of odd numbers up to 7 is: 16
OUTPUT-
1
2
3
4
5
31. Print numbers 1 to 10 and skip 5 using continue:
for i in range(1, 11):
if i == 5:
continue
print(i)
OUTPUT-
1
2
3
4
6
7
8
OUTPUT-
OUTPUT-
Enter a number: 6
2 is prime
3 is prime
5 is prime
OUTPUT-
Enter a number: 5
OUTPUT-
i=1, j=1
i=2, j=1
i=3, j=1
i=4, j=1
i=5, j=1
OUTPUT-
i=1, j=1
i=1, j=3
i=2, j=1
i=2, j=3
i=3, j=1
i=3, j=3
i=4, j=1
OUTPUT-
rows = 5
for i in range(rows):
for j in range(i + 1):
if j % 2 == 0:
continue
print("*", end=" ")
print()
OUTPUT-
**
**
OUTPUT-
OUTPUT-
OUTPUT-
Is 6 even? True
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
Result of multiplication: 24
OUTPUT-
Raj singh
Raj veer singh
47. Function with arbitrary arguments to sum all inputs:
def sum_all(*args):
return sum(args)
# Test the function
print(sum_all(1, 2, 3, 4))
print(sum_all(5, 10, 15))
OUTPUT-
10 30
OUTPUT-
Before function, x = 10
Inside function, x = 5
After function, x = 10
exponent = 3
result = power(base, exponent)
print(f"{base} raised to {exponent} is: {result}")
OUTPUT-
2 raised to 3 is: 8
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
'12345' contains only digits: True '123abc' contains only digits: False
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
69. Extract all dates from a text using regex (format: DD/MM/YYYY):
import re
def extract_dates(text):
pattern = r'\b\d{2}/\d{2}/\d{4}\b'
return re.findall(pattern, text)
# Test the function
string = "Meetings on 12/05/2023 and 25/12/2024"
dates = extract_dates(string)
print(f"Dates found: {dates}")
OUTPUT-
Dates found: ['12/05/2023', '25/12/2024']
OUTPUT-
OUTPUT-
First element: 10
OUTPUT-
OUTPUT-
Last element: 50
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
OUTPUT-
Empty tuple: ()
OUTPUT-
Second element: 20
OUTPUT-
First three elements: (0, 1, 2)
OUTPUT-
Last element: 50
OUTPUT-
OUTPUT-
OUTPUT-
Length of tuple: 5
OUTPUT-
OUTPUT-
OUTPUT-
def add_to_dict():
my_dict = {}
my_dict["name"] = "Alice"
return my_dict
# Test the function
result = add_to_dict()
print(f"Dictionary after adding pair: {result}")
Output-
Dictionary after adding pair: {'name': 'Alice'}
Output-
Value for key 'name': Bob
Output-:
Dictionary after replacing value: {'name': 'Charlie', 'age': 35}
Output-
Dictionary after adding new pair: {'name': 'David', 'city': 'New York'}
Output-
Dictionary after removal: {'name': 'Eve'} # Removed value: 28
Output-
: All keys: ['name', 'age', 'city']
Output-
All values: ['Grace', 22, 'Paris']
Output-
Is 'age' in dictionary? True
Output-
Merged dictionary: {'name': 'Ivy', 'age': 27, 'city': 'Tokyo'}’’
Output-
Dictionary after clearing: {}