Python Basic Exercises for Beginners
Python Basic Exercises for Beginners
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) ```