Variables
Variables are used to store data values. Use the equals sign to assign a value to a variable. Once a variable has been created it can be used in calculations. You can display the value of a variable using print()
.
coins = 10
coins = coins + 5
print(coins)
Output:
15
Exercises
Experiment with variables:
-
Create variables
length
andwidth
and assign them numerical values. Calculate and print the area of a rectangle using the formula: area equals length times width. -
Create two variables,
x
andy
, and assign them integer values. Calculate and print their sum, difference, product, and quotient. -
Create a variable
fahrenheit
and assign it a temperature in Fahrenheit. Convert the temperature to Celsius using the formula: C=(F-32) times (5/9)Hint: use tab completion when typing out
fahrenheit