11. Variables

A variable is a name used to refer to a memory location where a value is stored.
It can be thought of as a box that stores data.
In Python, the same variable can be reused to store values of any type.
e.g In rectangle_length = 2.3, the variable names is rectangle_length, the type is float (decimal) and the value is 2.3.
e.g In user_name = Annie, the variable names is user_name, the type is str (string) and the value is Annie.
Rules for Python variable names:
  • A variable name must start with a letter or the underscore character

  • A variable name cannot start with a number

  • A variable name can only contain alpha-numeric characters (A-z, 0-9) and underscores ( _ )

  • Variable names are case-sensitive (age, Age and AGE are three different variables)


11.1. Python Reserved Words

Reserved Words are keywords that have a set meaning and can’t be used for other purposes such as for variable names. Reserved words are case-sensitive and must be used exactly as shown. They are all entirely lowercase, except for False, None, and True.

The list of reserved words is:
False, None, True, and, as, assert, async, await, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield
A list of keywords can be shown by using print(help("keywords")).
A second method for listing keywords uses the keyword library. (see: https://docs.python.org/3/library/keyword.html)
import keyword

print(keyword.kwlist)

11.2. Python conventions

It is helpful to use meaningful variable names that indicate what the variable is. e.g. height instead of x for the height of an object.

11.3. Snake case

Snake case is used for variables and functions.
Snake case uses underscores between words. e.g. player_count, player_1_score, player_2_score

11.4. ALL_CAPS

ALL_CAPS are used for constants, such as PI = 3.14, GRAVITY = 9.8, MILES_TO_METRES = 1609.

11.5. CapWords

CapWords, such as AnimalFeatures, are used for Class names in python.

11.6. camelCase

camelCase variables, such as playerScore, are not recommended in python.

11.7. KebabCase

KebabCase variables, such as player-score, are not recommended in python.

11.8. Sample code

The print statement below can be generalised using a variable for the team name and a variable for the number of premierships.
This is good practice since it separates the data from the output.
print('Federer has won 20 Grand Slams.')

Generalised to:

player_name = 'Federer'
wins = '20'
print(player_name + ' has won ' + wins + ' Grand Slams.')

11.9. Tasks

Questions

  1. Write AGE in snake case.

  2. Write MyName in snake case.

  3. Write MyFirstNameLastName in snake case.

  4. Write rectangleArea in snake case.

  5. Write cm_in_an_inch = 2.14 as ALL_CAPS.

  6. Write lbs_in_a_kg = 2.2 as ALL_CAPS.

  7. A program asks for a person’s age and stores it. What would be a good variable to use: x, variable1, AGE, age, Years_Old?

  8. A program uses a person’s first name and last name. What would be a good variable to use for their last name: x, variable1, SURNAME, last_name, Name?

  9. A program calculates the area of a rectangle. What would be two good variables to use for the length and width of the rectangle: x, y, LENGTH, length, Width, width?

Write AGE in snake case.

age

Write MyName in snake case.

my_name

Write MyFirstNameLastName in snake case.

my_first_name_last_name

Write rectangleArea in snake case.

rectangle_area

Write cm_in_an_inch = 2.14 as ALL_CAPS.

CM_IN_AN_INCH = 2.14

Write lbs_in_a_kg = 2.2 as ALL_CAPS.

LBS_IN_A_KG = 2.2

A program asks for a person’s age and stores it. What would be a good variable to use: x, variable1, AGE, age, Years_Old?

age

A program uses a person’s first name and last name. What would be a good variable to use for their last name: x, variable1, SURNAME, last_name, Name?

last_name

A program calculates the area of a rectangle. What would be two good variables to use for the length and width of the rectangle: x, y, LENGTH, length, Width, width?

length, width