This lesson is being piloted (Beta version)

Python Fundamentals

Overview

Teaching: 20 min
Exercises: 10 min
Questions
  • What basic data types can I work with in Python?

  • How can I create a new variable in Python?

  • Can I change the value associated with a variable after I create it?

Objectives
  • Assign values to variables.

Variables

Any Python interpreter can be used as a calculator:

3 + 5 * 4
23

This is great but not very interesting. To do anything useful with data, we need to assign its value to a variable. In Python, we can assign a value to a variable, using the equals sign =. For example, to assign value 60 to a variable weight_kg, we would execute:

weight_kg = 60

From now on, whenever we use weight_kg, Python will substitute the value we assigned to it. In layman’s terms, a variable is a name for a value.

In Python, variable names:

This means that, for example:

Introducing types of data

Python knows various types of data. Three common ones are:

In the example above, variable weight_kg has an integer value of 60. To create a variable with a floating point value, we can execute:

weight_kg = 60.0

And to create a string, we add single or double quotes around some text, for example:

weight_kg_text = 'weight in kilograms:'

Using Variables in Python

To display the value of a variable to the screen in Python, we can use the print function:

print(weight_kg)
60.0

We can display multiple things at once using only one print command:

print(weight_kg_text, weight_kg)
weight in kilograms: 60.0

Moreover, we can do arithmetic with variables right inside the print function:

print('weight in pounds:', 2.2 * weight_kg)
weight in pounds: 132.0

The above command, however, did not change the value of weight_kg:

print(weight_kg)
60.0

To change the value of the weight_kg variable, we have to assign weight_kg a new value using the equals = sign:

weight_kg = 65.0
print('weight in kilograms is now:', weight_kg)
weight in kilograms is now: 65.0

Variables as Sticky Notes

A variable is analogous to a sticky note with a name written on it: assigning a value to a variable is like putting that sticky note on a particular value.

Value of 65.0 with weight_kg label stuck on it

This means that assigning a value to one variable does not change values of other variables. For example, let’s store the subject’s weight in pounds in its own variable:

# There are 2.2 pounds per kilogram
weight_lb = 2.2 * weight_kg
print(weight_kg_text, weight_kg, 'and in pounds:', weight_lb)
weight in kilograms: 65.0 and in pounds: 143.0

Value of 65.0 with weight_kg label stuck on it, and value of 143.0 with weight_lb label stuck on it

Let’s now change weight_kg:

weight_kg = 100.0
print('weight in kilograms is now:', weight_kg, 'and weight in pounds is still:', weight_lb)
weight in kilograms is now: 100.0 and weight in pounds is still: 143.0

Value of 100.0 with label weight_kg stuck on it, and value of 143.0 with label weight_lb stuck on it

Since weight_lb doesn’t “remember” where its value comes from, it is not updated when we change weight_kg.

Use meaningful variable names.

var1 = 42
ewr_422_yY = 'Ahmed'
print(ewr_422_yY, 'is', var1, 'years old')

Check Your Understanding

What values do the variables mass and age have after each statement in the following program? Test your answers by executing the commands.

mass = 47.5
age = 122
mass = mass * 2.0
age = age - 20
print(mass, age)

Solution

95.0 102

Sorting Out References

What does the following program print out?

first, second = 'Grace', 'Hopper'
third, fourth = second, first
print(third, fourth)

Solution

Hopper Grace

Key Points

  • Basic data types in Python include integers, strings, and floating-point numbers.

  • Use variable = value to assign a value to a variable in order to record it in memory.

  • Variables are created on demand whenever a value is assigned to them.

  • Use print(something) to display the value of something.