Take two inputs from the user. One will be an integer. The other will be a float number. Then multiply them to display the output.
Hint: Use input() to take user input. By default, input returns a string. Use int() and float() to convert those inputs to numeric types, and then multiply them.
int_num = int(input("Give me an integer number: "))
float_num = float(input("Give me a float number: "))
result = int_num * float_num
print("Your result is: ", result)
Take two numbers from the users. Calculate the result of second number power of the first number.
Hint: To raise a number to a power, use two asterisks (**). For example, 4**3 gives you 64.
base_num = int(input('Give me the base number: '))
power_num = int(input('Give me the power number: '))
result = base_num ** power_num
print('Your result is: ', result)
Find the largest of the three numbers entered by the user.
Hint: Ask the user to enter three numbers. Then compare them. You can assume the first number is the largest and then check if the second or third number is larger. Alternatively, use the built-in max() function to simplify.
num1 = int(input("First number: "))
num2 = int(input("Second number: "))
num3 = int(input("Third number: "))
largest = max(num1, num2, num3)
print("Largest number you entered is: ", largest)
Take multiple numbers as input from the user in a single line and calculate the average.
Hint: Ask the user to input numbers separated by spaces. Use split() to convert the input string into a list. Then convert each element to a number using map() or a loop. Use len() to get the number of items and calculate the average by dividing the total by the count.
numbers = input("Enter numbers separated by spaces: ").split()
numbers = [float(num) for num in numbers]
total = sum(numbers)
count = len(numbers)
average = total / count
print("The average is:", average)
Take the lengths of the three sides of a triangle from the user and calculate the area of the triangle.
Hint:
To calculate the area of a triangle when all three sides are known, use Heron's formula:
First calculate the semi-perimeter:
s = (a + b + c) / 2 Then use the formula:
area = √(s * (s - a) * (s - b) * (s - c)) You can use math.sqrt() to calculate the square root.
import math
a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))
s = (a + b + c) / 2 # semi-perimeter
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print("The area of the triangle is:", area)
Take the principal amount (money borrowed), annual interest rate, and duration in years as input. Then, compute the compound interest.
Hint:
Use the compound interest formula:
Amount = Principal * (1 + Rate/100) ^ Time Then use:
Compound Interest = Amount - Principal You can use ** to calculate the power, or use pow() function.
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate (in %): "))
time = float(input("Enter the duration in years: "))
amount = principal * (1 + rate / 100) ** time
compound_interest = amount - principal
print("Compound Interest is:", compound_interest)
Take marks and their corresponding weights as input from the user, and calculate the weighted average mark.
Hint:
In a weighted average, each mark is multiplied by its weight.
Use the formula:
weighted_average = (mark₁ * weight₁ + mark₂ * weight₂ + ...) / (weight₁ + weight₂ + ...) You can ask the user to input all marks and weights as space-separated numbers.
marks = input("Enter marks separated by spaces: ").split()
weights = input("Enter corresponding weights separated by spaces: ").split()
marks = [float(m) for m in marks]
weights = [float(w) for w in weights]
total_weighted_score = sum(m * w for m, w in zip(marks, weights))
total_weight = sum(weights)
weighted_average = total_weighted_score / total_weight
print("The weighted average mark is:", weighted_average)
You have three lists of words. Create all possible combinations of sentences by taking one element from each list—one from the first list, one from the second, and one from the third.
Hint:
Use three nested loops to iterate through each list. In the innermost loop, combine one word from each list into a sentence.
You can use f-strings or join() to format the sentence.
list1 = ["I", "You"]
list2 = ["like", "love"]
list3 = ["Python", "programming"]
for word1 in list1:
for word2 in list2:
for word3 in list3:
sentence = f"{word1} {word2} {word3}"
print(sentence)
Generate a random password that contains at least one uppercase letter, one lowercase letter, one digit, and one special character. The total password length should be at least 8 characters, and the remaining characters should be randomly selected from all character types.
Hint:
Use the random and string modules.
Steps:
Pick one character from each required type: string.ascii_uppercase, string.ascii_lowercase, string.digits, and string.punctuation.
Decide on the total length (e.g., 10 or more characters).
Fill the remaining characters with random choices from all combined sets.
Use random.shuffle() to mix the order for better randomness.
import random
import string
# Required character types
upper = random.choice(string.ascii_uppercase)
lower = random.choice(string.ascii_lowercase)
digit = random.choice(string.digits)
special = random.choice(string.punctuation)
# Define total password length
length = 10
remaining_length = length - 4
# Combine all characters for the rest
all_chars = string.ascii_letters + string.digits + string.punctuation
remaining_chars = random.choices(all_chars, k=remaining_length)
# Combine and shuffle
password_list = [upper, lower, digit, special] + remaining_chars
random.shuffle(password_list)
# Join into final password
password = ''.join(password_list)
print("Generated Password:", password)
Build a simple word completion game. The program will show an incomplete word with some missing letters (represented by underscores), and the user must guess the missing letters one by one. The game ends when the word is completed.
Hint:
Choose a secret word and display it with some letters replaced by underscores (e.g., _e__o for "hello").
Use a loop to allow the user to guess one letter at a time.
If the guessed letter is in the original word, reveal its position(s).
Use a list to keep track of correctly guessed letters and update the display accordingly.
secret_word = "hello"
guessed = ["_" if c != "e" else "e" for c in secret_word] # example with some pre-filled letters
while "_" in guessed:
print("Current word:", " ".join(guessed))
guess = input("Guess a letter: ").lower()
if guess in secret_word:
for i, c in enumerate(secret_word):
if c == guess:
guessed[i] = guess
else:
print("Wrong guess. Try again!")
print("Congratulations! The word was:", secret_word)
Data Analysis Projects
Given a DataFrame of employee data, filter out rows where age is greater than 30 and salary is more than 70,000.
Hint: Use logical operators & with parentheses for combining multiple conditions in df[condition].