Variables

Variables

A variable is a value that can change. They can be used to store information that can be referenced and used by programs. Instead of using the stored value directly, we can use the variable instead. Variables could be any number of things, such as, a string of text, a list or an object. Using variables makes our code flexible and reusable. Without variables we would have to hard code all our values into the code.

Variable names are defined when you declare them. The convention for variable names is set out in pep8, the style guide for python. They should be lowercase, with words separated by underscores as necessary to improve readability.

Variables in python are declared in the format name = value. In python we can store different types of data without having to be explicit about the type when we declare it.

Below are some examples:

a_str = "This is an example of a string in quotes" # In python the word string is abbreviated to str
my_float = 5.5
an_integer = 5 # integer is abbreviated to int
shopping_list = ["apples", "oranges", "pears"]
a_dict = {"userId": "JBloggs"} # dictionary is abbreviated to dict
my_var = another_variable # variable is abbreviated to var
test_function = my_function() #function is abbreviated to func
example_tuple = ("apple", "orange", "pear")
boolean_values = True # boolean is abbreviated to bool