This post will explain one of the fundamental concepts in python – Variables and Data Types. If you have not yet installed python, please follow this post to get started.

In this post, we will be going through the following topics:

  1. What are variables?
  2. Variable assignment
  3. Types of variables in python

What are variables?

A variable internally represents a memory location where we can store a value. As its name suggests, the values stored in a variable can change while the program is running. You can think of variables as containers that hold values. When a variable is defined, some memory is allocated for it. The amount of memory allocated depends on the data type of the variable.

Variable assignment

The syntax for assigning value to a variable is:

<variable_name> = <value>

For example:

Python is a little different than other popular languages like Java and C++ in that you do not need to declare the variable before assigning a value to it. Also, you do not need to declare the data type of the variable. The data type is dynamically interpreted when the program is running.

There are certain rules to be followed while naming a variable in Python:

  1. Variable names cannot start with a number. They should begin with an alphabet or an underscore ( _ ).
  2. Variable name should not contain white spaces.
  3. Reserved keywords cannot be used as variable name. For eg. for, if, and, elif, etc.

Types of variables in Python

There are various types of variables in Python that are classified according to the kind of data they can hold. Following are the built-in data types you can find in Python:

Numeric

Numeric data types are used to store numeric values. They are further divided into the following:

  1. Integers (int) : Used for storing positive or negative whole numbers including 0. Eg.: 1, 2, 0, -5, -70, etc.
  2. Float (float) : Real numbers with decimal places. For eg. 1.35, 1.007, etc.
  3. Complex Numbers : Used for representing complex numbers.

The syntax for each of the numeric types are shown below:

String

String data type is used when you want to store a sequence of characters. The characters can be alphanumeric, special characters like ‘$’, ‘%’ and almost every character that you can see in the keyboard. One example of using string data type is to store the name of a person.

Eg.:

Boolean

Variables with Boolean data type can store one of two values only – ‘True’ or ‘False’. A boolean variable is like a switch – either it is on (True) or off (False). For example:

List

‘List’ in Python is exactly what it says. It is an ordered list of items where the items can be of any data type. Every item in a list is given an index (starting from 0) which can be used to access the items.

Let’s say you want to create a list to store names of your friends. We do it this way:

Tuple

Tuples in python, like lists, are ordered collection of items that can store heterogeneous values. Like lists, the item in a tuple can be accessed with their index.

Eg.:

Lists and Tuples may look like similar to you. But they are slightly different and have different use cases. To see more details, check this out.

Dictionary

A dictionary in Python is a collection of key-value pairs. Think of a dictionary with a word and its meaning as key and value. Like in a real dictionary, you can see the meaning of a word against it, similarly, in Python dictionary, you can access the value stored against the key.

The syntax of assigning a dictionary is:

<dictionary_name> = {<key1>:<value1>, <key2>:<value2>}

For eg:

*****

If you have any questions or comments regarding the post (or something else), please feel free to reach out through the comments.