Understanding Variables and Data Types in Python

by Kal Bartal


1. What are Variables and Data Types?
2. Assigning Values to Variables
3. Naming Conventions for Variables
4. Integer Data Type
5. Float Data Type
6. String Data Type
7. Boolean Data Type
8. List Data Type
9. Dictionary Data Type
10. Tuples Data Type

Python is a powerful programming language that can be used to create a variety of applications. One of the most important concepts of Python is working with variables and data types. We’ll be taking a look at what variables and data types are, how to assign values to them, and how to name them. We’ll also be discussing the different data types like integers, floats, strings, booleans, lists, dictionaries, and tuples and how to use them. By the end of this guide, you’ll have a better understanding of how to use variables and data types in Python. So let’s begin!

1. What are Variables and Data Types?

Variables are components used to store information in a program, while data types are the kinds of information that can be stored within those variables. Variables and data types are fundamental programming concepts, and in Python they act as the building blocks for writing code.

When you create a new variable, you assign it a name and a value. The name is a label that makes it easy to reference the data that is stored within the variable. The value is the information itself, which can be of different data types. Python recognizes the following data types:

  • Strings: text data to store words, sentences, or paragraphs; e.g. ‘This is a string.’
  • Integers: whole numbers, both positive and negative; e.g. -3, 5, 9
  • Floats: real numbers with decimal points; e.g. 2.7, 8.1024
  • Booleans: data that can only be True or False; e.g. True, False
  • Lists: ordered data collections of any type; e.g. [‘apple’, ‘banana’, ‘cherry’]
  • Tuples: ordered data collections that can’t be changed; e.g. (‘apple’, ‘banana’, ‘cherry’)
  • Dictionaries: collections of data pairs with unique keys; e.g. { ‘name’: ‘John’, ‘age’: 33 }

By understanding variables, data types, and how to use them in Python, you will be able to create data structures and write code to manipulate and interact with data. This can open up a whole new world for your programming skills!

2. Assigning Values to Variables

Python is a powerful, general-purpose programming language widely used in many types of programming and data science. One of the most basic concepts of Python is assigning variables to data types.

A variable is a named piece of memory space where we can store data. In Python, we assign a value to a variable in the following way:

In the example below, we create a variable "x" and assign it an integer value of 10

x = 10

We can also assign a string to a variable, such as:

name = "John"

Or, a float value:

price = 52.50

We can also assign a Boolean value to a variable:

is_valid = True

These are just a few of the ways we can assign values to variables. Remember, it’s important to assign values to variables properly. It will keep your code organized and readable, and help you debug problems faster.

3. Naming Conventions for Variables

The use of good naming conventions for variables when programming in Python can be incredibly helpful for increasing the readability of your code. By following a few basic guidelines and using descriptive words, it’s possible to make it much easier for other developers to understand what your code is doing.

  1. Use Meaningful Names – Try to give variables a meaningful name that relates to the data it is storing. This makes it much easier to understand what the code is doing, instead of relying on trying to guess what the variable might be used for. For example, instead of using ‘a’, ‘b’ or ’c’, use a name like ‘student_name’ or ‘class_name’ when dealing with student and course information respectively.
  2. Use Clear and Readable Names – When choosing the names for your variables, select something that is easy to read and understand. Make sure to use appropriate word breaks, instead of using run together words or using underscores to separate words. For example, a good name for camera related variables would be ‘camera_make’ or ‘camera_model’ instead of ‘cameramake’ or ‘cameramodel’.
  3. Avoid Abbreviations – When naming variables, avoid using abbreviations and acronyms. This can create confusion for other developers and make code harder to read. For example, instead of ‘tot_cost’, use ‘total_cost’, or instead of ‘user_type’ use ‘user_type_description’.

By following these basic tips and avoiding common variable-naming pitfalls, you can ensure that your code is easy to read and understand by other developers. This makes it much easier to debug and maintain, saving you time and energy in the long run.

4. Integer Data Type

Integers in Python refer to a set of whole numbers that can be positive, negative or zero. Integers are commonly used in computer programming especially when processing numeric data. In Python, integers are stored as a fixed-width binary number that ranges from -2147483648 to 2147483647.


1. Positive integers such as 10, 23, 1101 and 6784. 
2. Negative integers such as -3, -7, -14 and -2234. 
3. Zero is also an integer. 

Integers are used to perform numerical operations such as addition, subtraction, multiplication and division. Python allows us to perform these operations in a variety of ways. For example, we can use the “+” operator for adding two integers, the “-” operator for subtracting two integers, the “*” operator for multiplying two integers and the “/” operator for dividing two integers.

We can also use the built-in functions or methods such as the “abs()” function or method to get the absolute value of an integer, the “round()” function or method to round a value to the nearest integer and the “bin()” function or method to convert an integer to its binary representation.

Overall, integers provide Python (and other languages) with a way to represent and process numeric data quickly and accurately.

5. Float Data Type

Float data type is an essential part of Python when it comes to dealing with numeric values. A float data type is a numeric data type that can hold fractional numbers as well as integers. It is stored as a number with a decimal point and can take any length or precision.

For example, the number 1.1 is a float or 3.91 or 9.8765. All these are floating point numbers. Even numbers without a decimal point like 5, 6 are also floating point numbers.

Using the float data type in Python is a simple process. All you have to do is assign the value to a variable using the ‘float’ keyword. Here’re some examples:

a = float(5)

The above line of code assigns the value 5 to the variable ‘a’ as a float. Similarly, we can assign any other numeric value as a float:

b = float(7.89)

The above line assigns the value 7.89 to the variable ‘b’ as a float. The same works for fractional numbers as well:

c = float(13.4567)

In this example, the float value 13.4567 is assigned to the variable ‘c’.

It is important to note that if you want to perform mathematical operations on float data types, use ‘/’ or ‘//’ instead of ‘*’. This is because Python automatically converts integers to float types when performing division. For example:

x = 5
y = 2

z = x/y # This will give 2.5 as the result

So, the float data type is essential for dealing with fractional numbers in Python. It’s a simple process to assign a float value to any variable which can be easily used in mathematical operations.

6. String Data Type

Python strings are data types that let you store and manipulate little pieces of text information. They are very useful in many programming situations and can provide flexibility and power to many applications. In this post, we’ll look at some of the basic properties of strings in Python, as well as some examples of how to use them.

A Python string is a sequence of characters, all within quote marks (single or double).


my_string = 'This is a string'
another_string = "This is also a string"

You can also create a string with Python’s triple-quotes, for multi-line strings.


 long_string = """This is a
 longer string that
 spans multiple lines"""

Strings are immutable, meaning you can’t make changes to them after they’ve been declared. You can, however, combine strings together with methods like concatenation.


string1 = "Hello"
string2 = "World!"

concatenated_string = string1 + string2

concatenated_string will hold the value: "HelloWorld!"

Python strings have built-in methods that you can use to work with and manipulate them.


string1 = "HelloWorld"

Returns the uppercase version of the string

string1.upper() # Returns "HELLOWORLD"

Returns the length of the string

string1.length() # Returns 10

Returns whether or not the string contains the substring "World"

string1.contains("World") # Returns true

These are just a few simple examples – Pyhton strings have plenty of additional in-built methods for a variety of scenarios.

Strings are a powerful tool in a Python programmer’s toolbox and are used for a variety of tasks. Here, we’ve looked at some of the basic properties of Python strings, and some examples of how to use them.

7. Boolean Data Type

A Boolean data type is used to store a single value: either True or False. In Python, the Boolean data type is represented by the keywords True and False. This type of data is used when evaluating a condition and is usually associated with operations like logic and comparison.

For example, if you have a variable called a that contains the value 5 and a variable called b that contains the value 10, you could use a Boolean expression to determine if a is greater than b. You can do this by writing a Boolean expression that would evaluate as True if a is greater than b.

a > b

The result of this Boolean expression would be False, since 5 is not greater than 10.

You can also use Boolean values in the context of control flow. For example, the if statement only executes the code inside its block if the condition evaluates to True.


A = 5
B = 10

if A > B:
print("A is greater than B")

This code will not be executed since the Boolean expression evaluates to False.

8. List Data Type

A list is one of the most commonly used data types in Python. It’s a collection of values that are ordered and changeable. You can put any type of data into a list – such as numbers, strings, and even other lists.

To create a list, you assign it a variable name and then assign it a set of values between square brackets. The values are separated with commas. Here’s a quick example:

my_list = [1,2,3,4,5]

Each item in the list can also be accessed using an index. For example, if you wanted to access the second value in the list, you could use: my_list[1].

Lists are very versatile in that you can perform various operations on them. For instance, you can add new elements to the list ( my_list.append(6) ), remove elements ( my_list.remove(3) ), remove the last element ( my_list.pop() ), or sort list items ( my_list.sort() ).

Lists are an integral part of any Python code, and understanding how to create and manipulate them is a key skill. So when you’re coding with lists, remember to be creative and explore all the possibilities.

9. Dictionary Data Type

Dictionary is one of the most popular data types used in Python, and it allows you to store data in key-value pairs. A key-value pair is a set of two elements that are related to each other and stored in the dictionary. The first element is referred to as the key and the second element is referred to as the value.

A dictionary in Python is an unordered collection of data with key-value pairs, which are also known as hashes or associative arrays. To create a dictionary, we use curly brackets and place the key-value pairs inside of them. The following is an example of creating a simple dictionary in Python:


my_dict = { 'name': 'John Doe',
'age': 45,
'address': '123 Main Street'}

In this example, ‘name’, ‘age’, and ‘address’ are the keys, while ‘John Doe’, 45 and ‘123 Main Street’ are their respective values.

To retrieve a value associated with a key in the dictionary, we can use the following syntax:



This will return the value associated with the ‘name’ key (in this case, ‘John Doe’). We can also update the values of keys by assigning a new value to them, or we can delete them completely:


my_dict['name'] = 'Bob Smith'
del my_dict['address']

Dictionaries are powerful tools for storing and retrieving data in Python. They allow you to store and access key-value pairs in an organized way, making them perfect for creating complex data structures.

10. Tuples Data Type

In Python, tuples are a data type that can be used to store collections of different objects. Unlike a list, elements in a tuple are placed inside parentheses and cannot be changed (immutable). Tuples provide an efficient way of organizing data that may need to be referenced multiple times.

Here is an example of a tuple in Python:

my_tuple = (1, 2, 3, 4)

As you can see, the elements in this tuple are integers, but tuples can contain any type of objects. For example, here is another tuple that contains a string and a float:

my_tuple_2 = ('hello', 1.5)

Accessing Values in a Tuple

You can access specific values within a tuple using indexes. For example, to access the first value in our my_tuple tuple, you can do the following:

first_value = my_tuple[0]

Output: 1

You can also use negative indexes to get values from the end of a tuple. For example, this will return the last value in the tuple:

last_value = my_tuple[-1]

Output: 4

Slicing Tuples

You can also use the slicing notation to get values from the middle of a tuple. For example:

middle_values = my_tuple[1:3]

Output: (2, 3)

This returns a new tuple with the values 2 and 3 from our my_tuple tuple.

Tuple Concatenation

You can also join two tuples together using the + operator. For example:

my_tuple + my_tuple_2

Output: (1, 2, 3, 4, 'hello', 1.5)

This creates a new tuple that contains all the elements from my_tuple and my_tuple_2.

Tuples vs. Lists

Tuples are very similar to lists, but they have one key difference: lists are mutable (can be changed) and tuples are immutable (cannot be changed). This means that tuples are more efficient than lists when you have data that should not be modified.


Comprehension of variables and data types in Python is essential for understanding how to use the language effectively. Knowing how to assign values, name variables, and work with different data types will help you create complex programs and find solutions to interesting problems. You can become proficient at using Python and become a successful programmer with practice and dedication.

Related Posts

Leave a Comment