Understand Variables in Python and the Main Types of Variables

What will we cover?

We will cover the what a 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 variable 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 fulfil the following

  • Use lowercase
  • Separate words with underscore.

A variable name in Python can be

  • A single lowercase letter
  • A lowercase word
  • Or lowercase words separated by 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 which 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 value 5.

Not convinced? Try to get the type as follows.

type(a + b)

Step 4: Python type Float

A float is a numbers 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 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 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 includes 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 the double-quoted are used in special cases.

  • “I’m a double quoted string”

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

That said, many, me included, use single and double quoted string 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 and Integers and Floats 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 boolean expression 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 is what makes 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 the break it down to, either 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 to boolean expression.

But let’s get started simple.

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 it to.

Step 9: What next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, 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.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Python Like a Pro?

If you’re serious about learning Python, there’s nothing better than strong commits. At your request, we have created an improved version of this popular free online course.

This version has the following benefits to enhance your learning journey.

  1.  Tracking your progress in the course.
  2.  Questionaries to ensure you understand concepts between important lessons.
  3. Downloadable Cheat Sheets for fast lookup what you learned.
  4. Direct Q&A with the instructor to help you to understand the material better.
  5. Added material for better explanations and insider knowledge.
  6. Extra videos with more explanations and stories.
  7.  Certificate at completion.

Start the change in your life and commit to doing something amazing that you have always dreamed of.

Sign up and become part of the exclusive Python Like a Pro elite.

Leave a Reply

%d bloggers like this: