Understand Variables in Python and the Main Types of Variables

What will we cover?

You will learn what a Python variable is. How you should name the variables, and finally, the main types of variables in Python.

Step 1: What is a Python variable?

A variable can be defined as follows.

  • A variable is a reserved memory location.
  • A variable in a python program gives data to the computer for processing.

When you work with data, you need somewhere to keep it. That is what variables are used for.

A simple example of a variable can be demonstrated as follows.

s = input('What is your name')
print('Hi', s)

Here, s, is a variable. It takes input from the user and stores it there. Then it prints the value of the variable on the next line.

Notice that we used similar variables in our first lesson.

In Python, we have the following basic types.

  • Integers
  • Floats
  • Strings
  • Boolean

We will work more with them later – so don’t worry that you do not understand them yet.

Step 2: Naming convention of Variables in Python

A variable name in Python should fulfill the following

  • Use lowercase
  • Separate words with an underscore.

A variable name in Python can be

  • A single lowercase letter
  • A lowercase word
  • Or lowercase words separated by an underscore.

Examples of variable names

  • x
  • i
  • cars
  • my_cars

Note that other programming languages can have other naming conventions.

You can declare variables as follows.

s = "This is a string"
a = 2
b = 2.4

And get the type of a variable as follows.


Step 3: Python type Integer

An integer is a number that is not a fraction; a whole number.

Examples of integers: 

  • -23
  • -5
  • 0
  • 1
  • 78
  • 1,980,350

Examples of non-integers: 

  • 3.14
  • 0.001
  • 9,999.999

Try the following.

a = 2
b = 3

Then you can make arithmetic operations with them.

c = a + b

Now c is an integer with the value of 5.

Not convinced? Try to get the type as follows.

type(a + b)

Step 4: Python type Float

A float is a number with digits on both sides of a decimal point. This is in contrast to the integer data type, which houses an integer or whole number.

Examples of floats:

  • 3.13
  • 9.99
  • 0.0001

Note that a float data type in Python can represent a whole number.

  • 1.0
  • 3.0

An example could be.

a = 1.1
b = 2.3

You can also make arithmetic operations.

c = a + b

Then c will be a float.

Funny enough, if you have a float and add an integer, then you get a float. Let’s try.

d = 1
e = 1.1
f = d + e

Try to take the type of all of them to make sure they are the expected types.

type(d), type(e), type(f)

Notice you can comma-separate statements like that, and it will output it in a comma-separated line.

Step 5: A few useful Math functions

When working with integers and floats it can be handy with a few helpful mathematical functions.

Here we will look at.

  • abs() Returns absolute value of a number
  • pow() Raises a number to a power
  • round() Rounds a floating-point value

The abs() function returns the absolute value of an integer or float.

a = -2

The pow() returns the number to a power.

pow(2, 3)

Notice that this can also be accomplished as follows.


Finally, round() is a very useful function used all the time. It rounds the value of a float.

f = 1.234456778
round(f, 4)

Where the second argument of round is the precision after the decimal point.

Step 6: String in Python

A string in Python is a sequence of characters.

Examples of strings in Python include the following.

  • “I am a string!”
  • ‘I am another string’

That is, you can both use single and double-quoted strings. The standard is to use single-quoted strings but double-quoted are used in special cases.

  • “I’m a double-quoted string”

As you see it can have a quote inside the string.

That said, many, myself included, use single and double-quoted strings inconsistently.

s1 = 'I am a string'
s2 = "I am another string"

Given the two strings above you can use addition to concatenate them.

s1 + s2

Yes, that is right. You can add strings together. But you cannot add a string and an integer or float.

Step 7: Print Strings, Integers, and Floats variables with Python

The easiest way to print variables of different types in Python is to comma separate them.

a = 1
b = 1.1
c = "I am a string"
print(a, b, c)

This will work fine, while something like print(a + b + c) will fail.

A great way to print formatted output is as follows.

a = 1
print(f'I am a string with an int here {a + 2} - and more text')

Hence, if you put an f in front of the quoted string, then you can add {} with integer or float statements – see it can be a variable or an arithmetic expression.

Step 8: Boolean variables in Python

If you are new to programming, then the boolean expressions can be quite difficult to understand. Or actually not. Most people think they get them, but do not understand the full content of the power of boolean expression.

Actually, boolean expressions are what make programming possible. Boolean expressions take simple scripts that do the exact same every time they are executed to full programs, which behave differently depending on input and context.

So what are boolean values?

  • True or False (case sensitive)

And a boolean expression is something that evaluates to either True or False.

That said, everything in your life is the same.

Say, every morning you look out the window to see if it rains. If it rains you prepare yourself for that. If not, well, you enjoy it not raining.

Some object to this. They say it is not always easy to say if it rains or not. Well, you can then break it down to, whether you bring your umbrella or not.

  • Either it rains or not -> Either you take your umbrella or not

Somehow all your decisions can be translated into a boolean expression.

But let’s get started simply.

b = 10 > 5

What? Well, you assign the boolean expression to 10 > 5. If 10 is larger than 5, then it will be True.

Check the value in b, it is True.

How can this be powerful, you might ask?

Well, when we look at conditional flows, you will be able to make powerful programs that can do whatever you want them to.

Step 9: What next?

I am happy you asked. You can get another lesson here.

But if this is something you like and you want to get started with Python, then this is part of an 8 hours FREE video course with full explanations, projects on each level, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • 2 FREE eBooks to support your Python learning.

See the full FREE course page here.

Learn Python


  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

Leave a Comment