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"

x = 6
myName = "bob"
x = x + 1

The = sign is known as the assignment operator

Rules for Naming Variable

  1. Variable names in Python should begin with a letter (a-z)

  2. The rest of the name can include letters, numbers or the underscore symbol (_)

  3. 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.

today = "Wednesday"

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.

# ThankYou.py
my_name = "Bryson"
my_age = 37
your_name = input("What is your name? ")
your_age = input("How old are you? ")
print("My name is", my_name, ", and I am", my_age, "years old.")
print("Your name is", your_name, ", and you are", your_age, ".")

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.

print("MathHomework.py")

# Ask the user to enter a math problem
problem = input("Enter a math problem, or 'q' to quit: ")

# Keep going until the user enters 'q' to quit
while (problem != "q"):
    
    # Show the problem, and the answer using eval()
    print("The answer to ", problem, "is:", eval(problem) )
    
    # Ask for another math problem
    problem = input("Enter another math problem, or 'q' to quit: ")
    
    # This while loop will keep going until you enter 'q' to quit 

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

# ThankYou.py
my_name = "Bryson"
my_age = 37
your_name = input("What is your name? ")
your_age = input("How old are you? ")
print("My name is", my_name, ", and I am", my_age, "years old.")
print("Your name is", your_name, ", and you are", your_age, ".")

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:

your_age = eval(input("How old are you? "))

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

age_difference = my_age - your_age
print("I am", age_difference, "years older than you")

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