Variables & Data Types in Python


6 min read

Variables & Data Types in Python

Disclaimer: If you are a programmer then you can just skim through this whole section. We will be exploring this section in-depth later. I guess ๐Ÿ˜‰

Variables and Naming conventions

In every programming language, at some point, we need to store some data. This data is stored in our system's memory. To access, use and manipulate this data, we assign them to a variable.

In simple terms, variables are the names you give to computer memory locations that are used for storing the actual data. In python, we can declare the variable anywhere. It is not necessary that all variables should be at the beginning of a code file. Also, to assign a value to the variable we can do the following:

age = 5
name = "John Doe"
height = 5.9
can_speak_french = False

To write better python code, we can follow naming convention as well. Basic rules are:

  1. Every variable should have a proper meaning

  2. Try to use short and meaningful names for variables

  3. If the variable contains more than one meaningful word use underscore ( _ ) to separate them.

In programming, we sometimes store values that won't be changing throughout the execution or lifetime of the program. This kind of data is called constants.


Constants follow the same naming conventions as any normal variable. But, with minor changes. All constants are written in capital cases only.

Data Types

Data types refer to what kind of data will be stored into the variable or constant. Python can store and use many types of data. Python's built-in data types are categorized as follows:

  1. Numeric: int, float, complex

  2. Sequence Type: str, list, tuple

  3. Boolean

  4. Set

  5. Dictionary


Python supports three kinds of numeric data. We can store integer (int), float and complex data as numeric data types.


Integer values can be of any length in python. Example of integers are 10, 2, 29, -20, -150 etc.

a = 5
print("The type of a", type(a))

# OUTPUT: The type of a <class 'int'>


Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It is accurate up to 15 decimal points.

b = 40.5
print("The type of b", type(b))

# OUTPUT: The type of b <class 'float'>


A complex number contains an ordered pair, i.e., x + iy where x and y denote the real and imaginary parts, respectively.

c = 1+3j
print("The type of c", type(c))

# OUTPUT: The type of c <class 'complex'>

Sequence Type

As the name suggests, this data type stores sequence of values/data. This data type contains the following sub-categories:

  1. String

  2. List

  3. Tuple


The string is just a sequence of characters. A single character is also considered as a string in python. To store string data, we can use single, double, or triple quotes.

str1 = 'Use of double quotes'

# OUTPUT: Use of double quotes

str2 = """A multiline

# OUTPUT: A multiline
#          string

str3 = "a"

# OUTPUT: <class 'str'>


The list data type is similar to an array but not exactly. In an array, we can only store one type of data. Whereas, in python list, we can store multiple types of data into a single list.

list1  = [0, "Python", "Program", 1]
# OUTPUT: <class 'list'>
# OUTPUT: [0, "Python", "Program", 1]

list1[0] = 2
# OUTPUT: [2, "Python", "Program", 1]

list2  = list([0, "Python", "Program", 1])
# OUTPUT: <class 'list'>
# OUTPUT: [0, "Python", "Program", 1]

list3  = [0, "Python", "Program", 1, 2, 3]
# OUTPUT: Program


The tuple data type is similar to lists in python. The main difference between list and tuple is, once we created a tuple we can not change its value.

tuple1  = (0, "Python", "Program", 1)

# OUTPUT: <class 'tuple'>
# OUTPUT: (0, "Python", "Program", 1, 2, 3)

tuple1[1] = 5

# Traceback (most recent call last):
#     File "", line 14, in <module>
#        tuple1[2] = "Python";
# TypeError: 'tuple' object does not support item assignment


Boolean is considered as truthfulness. It has only two values.

  1. True

  2. False

Note: T and F in True and False must be capital.

a1 = 5
b1 = 5

c1 = a1==b1
# OUTPUT: True

a2 = 5
b2 = "5"

c2 = a2==b2
# OUTPUT: False

a3 = True
# OUTPUT: <class 'bool'>


Set can be used the same way we use lists. One difference between set and list is, the list can have duplicate values whereas the set can not. It will be more clear with the example shown below.

To create a Set we have to use the set() function or add items surrounded by curly braces {}. In day-to-day programming, I have not seen much use of the set. Set has very small and specific use-cases.

To create empty set we have to set() function, empty curly braces{} will not work.

list1  = [1, "Python", "Program", 2, 1, 2]
# OUTPUT: [1, "Python", "Program", 2, 1, 2]

set1  = set([1, "Python", "Program", 2, 1, 2])
# OUTPUT: {1, 2, 'Program', 'Python'}

set2  = set("Hello There!")
# OUTPUT: {'h', 'r', 'o', 'H', 'T', 'e', 'l', '!', ' '}

set3  = {1, "Python", "Program", 2, 1, 2}
# OUTPUT: {1, 2, 'Program', 'Python'}

set4  = {}
# OUTPUT: <class 'dict'>

set5  = set()
# OUTPUT: <class 'set'>


Dictionary is a collection of key-value pairs stored in unorder fashion. If you are from a Java background you can see it as one kind of Map or Hashtable. The items in the dictionary are separated with the comma (,) and enclosed in the curly braces{}.

To create a dictionary using two methods shown in the example:

dict1 = {'Name': 'Python', 1: [1, 2, 3, 4], 2:"DICT"}
# OUTPUT: {'Name': 'Python', 1: [1, 2, 3, 4], 2: 'DICT'}

dict2 = dict({'Name': 'Python', 1: [1, 2, 3, 4], 2:"DICT"})
# OUTPUT: {'Name': 'Python', 1: [1, 2, 3, 4], 2: 'DICT'}

dict3 = dict([('Name', 'Python'), (1, [1, 2, 3, 4]), (2,"DICT")])
# OUTPUT: {'Name': 'Python', 1: [1, 2, 3, 4], 2: 'DICT'}

dict3 = {'Name': 'Python', 'Last':'Programming', 1: [1, 2, 3, 4], 2:"DICT"}
# OUTPUT: Python
# OUTPUT: [1, 2, 3, 4]

In the dictionary, we get or extract data by using the key. So, from the last code when we do dict[1], it extracts data with the key 1 that is [1, 2, 3, 4].


This section is introduced to just showcase different data types available in python. There are many in-built methods associated with these data types. We will see them in another section where I can show you some practical use of them.

That was it. Thank you for reading.

I know it is not fun to learn about data types but I hope you got some knowledge of data types in Python. If you have any questions reach out to me on Twitter or LinkedIn.