We can use math to change numbers around and edit our data. You can use these operators on an integer and another integer, on a float and another float, or on a combination of the two.
Usage (Code Example)
- Addition (
x = 5 y = 3 print(x + y) # prints 8
- Subtraction (
x = 5 y = 3 print(x - y) # prints 2
- Multiplication (
x = 5 y = 3 print(x * y) # prints 15
- (Floating Point) Division (
x = 15 y = 3 print(x / y) # prints 5.0 x = 5 y = 3 print(x / y) # prints 1.6666666666666667
- (Floor) Division (
x = 5 y = 2 print(x // y) # prints 2
- Modulus (
x = 14 y = 3 print(x % y) # prints 2
- Exponent (
x = 2 y = 3 print(x ** y) # prints 8
Ask the user for a number, and store it in a variable called x. Divide the number x by 5, then add 2 to it, then store it in a variable called y. Print the result (y).
Write a program that asks for the radius and height of a cylinder and returns the volume of the cylinder. The formula for the volume of a cylinder is
volume = pi * (radius ** 2) * height
Write a program that asks the user for the radius of a circle. Then, print the area and circumference of the circle. Use
3.14as the value for pi. Don't forget to add comments!
area = pi * (radius ** 2)
circumference = pi * 2 * radius
Write a program that accepts two numbers, x and y. Compute and print the quantity
(x + y) ** 5. Display the entire equation. The program should accept decimals as inputs.
Write a program that converts Celsius to Fahrenheit. It should ask the user for the temperature in degrees Celsius and then print the temperature in degrees Fahrenheit. The formula is:
F = C * 9/5 + 32
Write a program that asks the user for a floating-point number as input and outputs the decimal part (the part to the right of the decimal point). Don't worry about floating point errors! Note: To display a more exact output, you can use
rounded_decimal = round(decimal_variable, 10)to set rounded_decimal to your decimal rounded to a precision of 10 decimal places.
Write a program that prompts the user with the following questions:
- How many cookies did you make?
- How many cookies did your friend make?
- How many cookies are burnt?
- How many friends do you have?
Then, output the number of cookies that will be leftover if you throw out all the burnt cookies and all the friends get the same amount of cookies.
Ask the user to input how many people are in the lunchroom and how many people can sit at each table. Output the number of people that will be left without a table.
Write code that takes, as input, the number of dollars a person has (a floating number) and outputs how much they have in dollars, quarters, dimes, nickels, and pennies.
For example, if someone has $4.62, the program would print the following:
The starting code is given.
This is a challenging problem! Don't feel bad or disheartened if you can't solve it. Your teacher can go over it next class