Variables and Data Types

Learn about Python variables and the different data types you can use in Python.

Variables

In Python, variables do not have to be declared. Rather, variables come into existence when you assign a value to them. For example:

best_value = 36 # Variable best_value created and assigned the value of 36
print(best_value)

Convention: Variable names begin with a lowercase letter. For multi-word variable names, most programmers use underscores between words (as seen in the example above), but some use “studly caps.”

An example of the studly caps convention would be:

bestValue = 36
print(bestValue)

Caution: Python’s type names and function names are not reserved words, and it is easy to accidentally override them. For example, if you use the name list or abs for a variable, you can no longer use those names to create lists or find absolute values.

You can see this in the following example where we assign the value 5 to a variable called abs. Now, when we try to make the negative variable, neg, into a positive numerical value by using the built-in absolute command (abs), we get an error:

abs = 5 # Creating the variable abs and assigning it the value of 5
print("abs:", abs) # Printing the value of the abs variable (reserved word getting overriden)
neg = -1
print(abs(neg)) # Using built-in abs command to make the variable neg positive

Important data types

The basic data types available in Python are similar to what you’re already used to in other programming languages:

  • Integers
  • Floats (reals)
  • Booleans (logicals)
  • Strings
  • Lists

Integers

Integers are one of the types that can be used to represent numbers in Python. These can be both positive and negative values and may be arbitrarily large. The following are a few examples of these:

num = 73 # Positive number
print(num)
neg_num = -15 # Negative number
print(neg_num)
largeNumber = 12345678901234567890
print(largeNumber)

Floats

Floats are another form of representing numbers in Python and exist as either positive or negative decimal values. Below are some examples of floats:

pie_value = 3.1416
print(pie_value)
std_form = 6.02200000000000000000000
print(std_form)

Boolean

Boolean is a built-in logical data type that is mainly used for checking whether the logic of an expression or comparison is true or not. Boolean variables can only have two possible values: True or False.

Note: Both True and False are capitalized.

A few examples of these might be:

print(True)
print(False)
true_var = True
false_var = False
print(true_var)
print(false_var)

Strings

The string data type is used for representing characters. In Python, there are no major restrictions for using strings. You can use either single or double quotes to represent them. You can even insert single quotes inside double-quoted strings, and vice versa.

Note: Unlike other programming languages such as C++, there is no separate “character” data type.

print("Hello World")
print('Goodbye')
print("##@@")
print("Tom said, 'it has been raining a lot these days'.") # single quotes inside doubly-quoted strings
print("") # A string containing no characters is an "empty string"

Lists

Lists are similar to collections, as in you can store multiple items of different data types inside of them. These items are stored with a fixed and defined order and are easily changeable.

Note: Lists aren’t exactly arrays, but you can treat them as such.

my_list_emp = [] # empty list
print(my_list_emp)
my_list_int = [1, 2, 3, 4, 5] # list of integers
print(my_list_int)
my_list = [1, 2, "Buckle my shoe"] # list of mixed data types
print(my_list)

Lists are also zero indexed, meaning the first item in the list is assigned an index of 0. For example, in the code above, the value 1 can be accessed from my_list by my_list[0].

Similarly, let’s say a list has ten elements inside of it. Its first element can be accessed by index 0 and the last by index 9. If we try to access the last element of this list by using 10 as its index, we will get an error.

my_list = [1, 2, "Buckle my shoe"]
print(my_list[0]) # Accessing first index
new_list = [1,2,3,4,5,6,7,8,9,10]
print(new_list[0]) # Accessing first index
print(new_list[9]) # Accessing last index
print(new_list[10]) # This will give an error

NoneType

There is also another simple but unusual data type known as, NoneType, which only has a single value, None.

This is an actual value that can be assigned to variables.

Note: None is not a default value for values that have not yet been assigned a value. If users did try to read an unassigned variable, this will result in an error.

In Python, every function returns a value, and None is most often the result of a function that does not explicitly return a value.

no_val = None
print(no_val) # prints "None" and returns None
print(x) # will return error as x is an unassigned variable

There are also other data types available in Python such as tuples, sets, and dictionaries. We will discuss these in later chapters.