Expressions¶
-
Write the following statement in legal python
y = x^2 + 2x + 10
, assuming that variablex
already refers to a value.The exponentiation operator is**
. Also, you cannot put2
andx
directly next to each other to do multiplication. You have to use*
.
-
Given two variables,
miles
andgallons
, write an assignment statement that creates the variablempg
set to the computed number of miles per gallon.Use the true division operator/
.
-
Create a variable
remainder
that refers to the remainder when dividing 17 by 5.To get the remainder when dividing two numbers, you need to use the modulo operator%
.
-
Write a small program that asks the user for two numbers and prints out the remainder when dividing the first by the second. An example run should look like this:
Enter the numerator:
22
Enter the denominator:
4
The remainder when dividing is 2
To get the remainder when dividing two numbers, you need to use the modulo operator%
.
-
You are a bean counter who counts beans and puts them into piles of 16 beans each. Your manager gives you some number of beans and asks you to tell him how many full piles that will produce. Write a program that asks for the number of beans and prints out the number of full piles.Use integer division, which divides two numbers, and then “throws out” the fractional part.
-
Remainder and integer division can often be used together. Alter the program from the last exercise to print out not only the number of full piles of beans, but also the number of beans that remain. E.g., a run of your program might look like this:
Enter # of beans: 99 99 beans is 6 piles with 3 remaining
Use integer division, which divides two numbers, and then “throws out” the fractional part, and modulo division, which divides two numbers, keeping the remainder. You might find it useful to create 3 variables:
beans
is the count of beans the user entered,piles
is the computed number of piles, andremaining
is the computed remainder.Also, remember that you cannot add together integers and strings. If you are going to concatenate, both items must be strings.
-
The discriminant of the quadratic formula is the expression:
b^2 - 4ac
Write a program to ask the user for a value for
a
, a value forb
, and a value forc
, and then prints the discriminant.Don’t forget that you cannot just put variablesa
andc
next to each other. You have to use*
.
-
Write a short program that asks the user to enter an integer, and then prints out the last digit of the number that was entered. E.g.,:
Enter an integer: 162 The last digit is 2
The last digit of a number is the remainder when dividing by 10.
-
Write a short program that computes a total restaurant bill, including a 15% tip. E.g.,:
Enter the amount on the bill: 30.00 The 15% tip amount is: $4.5 The total is: $34.5
To read in a decimal value (afloating point number
), usevariable = float(input('prompt'))
. Use multiple variables and create your program one line at a time. Remember that 15% is 0.15. Don’t worry about always having 2 digits after the decimal point.