Variables
In this section, you will learn:
Create your own variables to store numbers, string and lists
Discuss the differences between different number types in Python
Use basic mathematical operations in Python to perform calculations
Explain the differences between strings, numbers and lists
Write out shorts programs in steps as comments to help you build your own code
Take user input in a variety of situations and use that input in your programs
Variables
Variables are where we keep our stuff in a program. A variable is something you want the computer to remember.
Python can remember several different types of values, including:
Numbers: 7, 42, 98.6
Strings: "letters", "words", "sentences"
The = sign is known as the assignment operator
Rules for Naming Variable
Variable names in Python should begin with a letter (a-z)
The rest of the name can include letters, numbers or the underscore symbol (_)
Variable name are case sensitive: my_name, My_Name, and MY_NAME are all different variable names in Python
Strings
Strings are words and sentences in a program. To use strings in Python we surround our text with quotation marks.
Thankyou.py
Here we will store some information in variable and collect some information from the user before displaying it back on sentences.
Open a new file in the Python Idle and save it as Thankyou.py. Write out the following code:
Create a file called thanksyou.py and copy the following code.
Notice a few things:
At the end of the input text, there is a space. This prevents the answer from appears right up against the ?
We combine strings in the bring statement by separating out our strings and variables with a comma
Numbers
Integers - whole numbers including negatives like 7, -9, 9
Floating-point numbers - numbers with decimals like 1.0, 2.5, 3.14159265
Two do basic mathematical operations in Python, we use the Python arithmetic operators
In the python shell, we can do some basic mathematics using these operators. Have a play:
We can also store values in variable in the Python shell.
Notice that the final line x = x - 7 doesn't make any sense from a mathematical point of view. However, in a program, the right hand side gets evaluated first and then assigned to x. So x = x - 7 means reduced the current value of x by 7 and save the new number back into x
Example: Python Does Your Maths Prep
We can use the eval() function to calculate basic maths problems in our program.
Create a new files calls MathsHomework.py and include the following code.
We have something new here. The While Loop. This will keep going until so long as the condition in the bracket is met. In this case, as long as the user input is not equal to q.
Lets go back to thankyou.py
We're going to add an extra print command that tells the user the difference between your age and Brysons. The problem is that Python doesn't know whether or not the input into your_age
is a string or a number. We can tell Python that it is a number by using the eval()
function. This is short for evaluate.
Change line 5 to:
Now Python knows that your_age
is a number
Now create a new variable at the bottom of the file that works out the age difference and print it out
Run and test your program. Can you think of any issues / improvements. What will make the program crash?
Project
To convert temperature from degrees Fahrenheit to Celsius we we can use the formula:
celsius = (fahrenheit - 32) * 5 / 9
Write a program that asks the user where they live and the temperature in degrees Fahrenheit today. Store these in variables and make the computer display the temperature in celsius to the user. Eg
In Marlborough it is 45 degrees Celsius.
Last updated