0% found this document useful (0 votes)
13 views2 pages

Python Basic Exercises for Beginners

The document outlines a series of basic Python exercises aimed at beginners. Each exercise involves simple tasks such as adding numbers, calculating averages, converting units, and performing basic input/output operations. The exercises are designed to help users practice fundamental programming concepts in Python.

Uploaded by

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

Python Basic Exercises for Beginners

The document outlines a series of basic Python exercises aimed at beginners. Each exercise involves simple tasks such as adding numbers, calculating averages, converting units, and performing basic input/output operations. The exercises are designed to help users practice fundamental programming concepts in Python.

Uploaded by

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

Python Basic Exercises

1. Add Two Numbers: Ask the user to enter two numbers and
print their sum.
2. Find Average: Input three numbers, convert them to float,
and print the average.
3. Square of a Number: Take an integer input and print its
square.
4. Convert Age to String: Input your age, convert it to str, and
print 'You are X years old'.
5. Convert Temperature: Ask for Celsius temperature and
convert it to Fahrenheit (°F = °C × 9/5 + 32).
6. Circle Area Calculator: Input the radius (float) and print the
area using 3.1416 * radius * radius.
7. Simple Interest Calculator: Input principal, rate, and time (as
float), and compute interest = (P × R × T) / 100.
8. Type Check: Input any value and print its data type using
type().
9. Sum of Digits: Input a two-digit number and print the sum of
its digits.
10. Name and Age Greeting: Input your name and age, and
print 'Hello <name>! You are <age> years old.'
11. Convert Meters to Kilometers: Input distance in meters,
convert to kilometers (divide by 1000), and print both.
12. Convert Seconds to Minutes: Input total seconds, convert to
minutes (seconds / 60), and print.
13. Type Conversion Practice: Input a number as string, then
convert it to int and float. Print both results.
14. Combine Strings and Numbers: Ask for name and age; print
using concatenation:
'My name is ' + name + ' and I am ' + str(age) + ' years old.'
15. Perimeter of Rectangle: Input length and width as float,
calculate and print perimeter = 2 × (L + W).
16. BMI Calculator: Input weight (kg) and height (m), convert
to float, and print BMI = weight / (height²).
17. Convert MMK to USD: Input amount in Kyats, convert to
USD using 1 USD = 2100 MMK.
18. Combine int + str (Error Fix): Try printing
num = 5
print('Number is ' + num)
Then fix the error by converting num properly.
19. Read Integer and Float Together: Input one integer and one
float, print their sum and product.
20. Reverse Number Order: Input two numbers and print them
in reverse order on one line.

Common questions

Powered by AI

To compute the area of a circle from its radius, input the radius as a float using input() and convert it to a float with float(). Use the formula `area = π * radius²`, where π is approximately 3.1416. Finally, print the calculated area: ```python radius = float(input('Enter the circle radius: ')) pi = 3.1416 area = pi * radius * radius print('The area of the circle is:', area) ```

To create a program in Python that adds two numbers, you can use the input() function to prompt the user for inputs. The inputs should be converted to integers or floats using int() or float(), then added together. Finally, print the result using the print() function: ```python num1 = int(input('Enter first number: ')) num2 = int(input('Enter second number: ')) sum = num1 + num2 print('The sum is:', sum) ```

To calculate the average of three numbers in Python, prompt the user for three inputs using input(), convert these inputs to floats using float(), and compute the average by summing these numbers and dividing by three. Print the result as follows: ```python num1 = float(input('Enter first number: ')) num2 = float(input('Enter second number: ')) num3 = float(input('Enter third number: ')) average = (num1 + num2 + num3) / 3 print('The average is:', average) ```

To calculate the perimeter of a rectangle in Python, ask the user for the rectangle's length and width using input(), convert these to floats, and compute the perimeter using the formula `perimeter = 2 * (length + width)`. Display the result with: ```python length = float(input('Enter length of the rectangle: ')) width = float(input('Enter width of the rectangle: ')) perimeter = 2 * (length + width) print('Perimeter of the rectangle is:', perimeter) ```

To determine and print the data type of user input in Python, use input() to receive a value from the user. Utilize the type() function to get the data type, and print this result: ```python value = input('Enter any value: ') print('The type of the entered value is:', type(value)) ``` If you convert the input to an int or float before using type(), it will show the respective type.

To convert meters to kilometers in Python, use input() to prompt the user for a distance in meters. Convert this input to float and divide by 1000 to convert to kilometers. Print both the meters and kilometers: ```python distance_meters = float(input('Enter distance in meters: ')) distance_kilometers = distance_meters / 1000 print('Distance in meters:', distance_meters) print('Distance in kilometers:', distance_kilometers) ```

To fix type errors in Python when attempting to concatenate strings and integers, convert the integer to a string using str(). This prevents a TypeError: ```python num = 5 print('Number is ' + str(num)) ``` In this example, str(num) correctly converts the integer 5 to a string, allowing it to be concatenated with another string.

To convert Celsius to Fahrenheit in Python, prompt for the Celsius temperature using input() and convert it to a float. Apply the conversion formula `F = C × (9/5) + 32` and print the Fahrenheit temperature: ```python celsius = float(input('Enter temperature in Celsius: ')) fahrenheit = celsius * 9/5 + 32 print('Temperature in Fahrenheit:', fahrenheit) ```

To convert seconds to minutes in a Python script, first get the total seconds from the user using input(), then convert the input to an integer or float. Use integer division or float division to find minutes: `minutes = seconds / 60`. Finally, print the result: ```python seconds = int(input('Enter time in seconds: ')) minutes = seconds / 60 print('Time in minutes:', minutes) ```

To build a BMI calculator in Python, prompt the user to input weight and height using input(), convert these values to float. Calculate BMI using the formula `BMI = weight / (height ** 2)` and print the result: ```python weight = float(input('Enter weight in kg: ')) height = float(input('Enter height in meters: ')) bmi = weight / (height ** 2) print('Your BMI is:', bmi) ```

You might also like