0% found this document useful (0 votes)
2 views3 pages

Python

The document contains various Python code snippets demonstrating different functionalities, including calculating when a user will turn 100 based on their age, converting kilometers to miles, checking if a number is even or odd, finding the largest of three numbers, printing a multiplication table, identifying numbers divisible by 3, reversing a string, and finding the maximum in a list. Each code snippet is self-contained and illustrates a specific programming concept or task. The overall focus is on basic Python programming techniques and operations.

Uploaded by

hamsalu bekana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Python

The document contains various Python code snippets demonstrating different functionalities, including calculating when a user will turn 100 based on their age, converting kilometers to miles, checking if a number is even or odd, finding the largest of three numbers, printing a multiplication table, identifying numbers divisible by 3, reversing a string, and finding the maximum in a list. Each code snippet is self-contained and illustrates a specific programming concept or task. The overall focus is on basic Python programming techniques and operations.

Uploaded by

hamsalu bekana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

how input name and age display year when user turns 100 in python
from datetime import datetime
def main():
try:
# Get user name
name = input("Enter your name: ").strip()
if not name:
print("Name cannot be empty.")
return

# Get user age and validate


age_input = input("Enter your age: ").strip()
if not age_input.isdigit():
print("Invalid age. Please enter a positive integer.")
return

age = int(age_input)
if age < 0 or age > 150:
print("Age must be between 0 and 150.")
return

# Calculate the year when the user will turn 100


current_year = [Link]().year
years_to_100 = 100 - age
year_turn_100 = current_year + years_to_100

if age >= 100:


print(f"Hello {name}, you turned 100 in the year
{year_turn_100}.")
else:
print(f"Hello {name}, you will turn 100 in the year
{year_turn_100}.")

except Exception as e:
print(f"An error occurred: {e}")

if __name__ == "__main__":
main()
2. how convert kilometer to miles in python
# driver code
kilometers = 5.5

# conversion factor
conv = 0.621371

# calculate miles
miles = kilometers * conv
print('%0.3f kilometers is equal to %0.3f miles' % (kilometers, miles))

# example 2
kilometers = 6.5

# calculate miles
miles = kilometers * conv
print('%0.3f kilometers is equal to %0.3f miles' % (kilometers, miles))
[Link] even or odd!
x = 24
# Check the remainder dividing x by 2 is 0
if x % 2 == 0:
print("Even")
else:
print("Odd")

# Checking another number


x=7

if x % 2 == 0:

print("Even")
else:
print("Odd")
4. how find largest of three numbers in python
a = 10
b = 14
c = 12

if a >= b and a >= c:


res = a
elif b >= a and b >= c:
res = b
else:
res = c

print(res)
5. how find largest of three numbers in python
def print_multiplication_table(number, upto=10):
"""
Prints the multiplication table for the given number up to 'upto' times.
"""
print(f"\nMultiplication Table of {number}")
print("-" * 30)
for i in range(1, upto + 1):
print(f"{number} x {i} = {number * i}")
print("-" * 30)

def main():
try:
# Get number from user
num = int(input("Enter a number to print its multiplication table:
"))

# Optional: get range limit


limit_input = input("Enter the range limit (default is 10):
").strip()
limit = int(limit_input) if limit_input else 10

if limit <= 0:
print("Range limit must be a positive integer.")
return

# Print the table


print_multiplication_table(num, limit)

except ValueError:
print("Invalid input! Please enter integers only.")

if __name__ == "__main__":
main()
6. convert numbers divisible by 3(1-100)
divisible_by_3_strings = [str(num) for num in range(1, 101) if num % 3 == 0]

print("Numbers divisible by 3 (as strings):")


print(divisible_by_3_strings)

divisible_by_3_squares = [num ** 2 for num in range(1, 101) if num % 3 == 0]

print("Squares of numbers divisible by 3:")


print(divisible_by_3_squares)
7. write reverse_strings function
s = "Very Good"
rev = s[::-1]
print(rev)
8. how find maximum in list in python
numbers = [2, 133, 12, 12]
result = max(numbers)
print(result) # Output: 133

You might also like