Python Exercises

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.