Variables and Data Types

Learn about Python variables and the different data types available in Python.

Ali Berro

By Ali Berro

5 min read Section 2
From: Python Fundamentals: From Zero to Hero

Variables and Data Types

Variables

Variables are used to store information, to be referenced later and used in our programs. Variables label data. They can also be thought of as a container for information. A variable consists of a name, a place inside the memory, and a value.

Note

Every variable is stored somewhere inside the RAM. The name is for the programmers to use so that we could reference that variable, instead of writing @1002. A variable holds a value, and each value belongs to some type.

Syntax

A variable is created as follows:

variableName = value

Where variableName is the name of our variable, and the value is the thing that we want the variable to bind to.

Info

This creates a box in memory. variableName will be used as a reference to that spot. variableName will bind to value i.e. gets assigned to that value.

Naming Rules

Each programming language puts some rules for how the a variable could be named. In Python we have the following rules:

  1. Start with a letter [a-zA-Z] or an underscore _, but never a digit.
  2. Followed by any letter, digit or underscore.
  3. A name should not be a reserved keyword if, for, while,…

Note

Python variable names are case-sensitive, meaning that myVar is not the same as myvar.

Python 3 allows Unicode variable names, however we need to take into consideration the following:

  1. Allowed characters are the ones that are classified as “like” alphanumeric and underscore.
  2. Certain Unicode letters are not allowed, such as mathematical operations (+, -,..), emoji, and special characters like @, #, or spaces
valid-variable-names.py
x = 3
age = 21
num_1 = 30
first = -5
_second = 13
π = 3.1415
علي = "Me"

It feels weird to have your name in Arabic as a variable name, but it is what it is.

Data Type

In programming a data type is the type of data that can be stored inside a variable. You can think of it as the domain, a set of allowed values that, if a variable possess any, then we say it belongs to that type. We will explore the following types:

  • int
  • str
  • float
  • None
  • bool
  • complex

These are called the primitive data types.

Integer

Integer is a non-decimal number. It can be positive, or negative.

integer-example.py
x = 10
y = -200
z = 12345678912345678123456789123456789
t = 0

Note

The integer implementation in Python has unlimited precision.

String

A string is a sequence of characters enclosed with single quotations ' or double quotations ".

string-example.py
var = "Hey!"
courseName = "Python Fundamentals"
welcome = 'Good evening'

Note

We will have a dedicated section for strings in future lessons.

Float

A floating point number is a number that has a decimal point.

float-example.py
pi = 3.14
e = 2.718281
sqrt2 = 1.4142135623730951
y = 0.0
z = -1.23456789123456789

Note

In Python, floats are usually (but not necessarily) implemented using C double.

None

None represents void, no value, or null.

none-example.py
x = None
y = None

Note

This is mostly used by functions when they don’t return a value.

Boolean

A boolean is a value that can be either True or False. Some values are tested for truthiness or falsiness, using the if statement, while loop, or other control flow statements. By default everything is considered truthy, except for:

  • Constants defined to be false: None, False,
  • Zero numeric values: 0, 0.0, 0j
  • Empty sequences and collections: '', (), [], {}, set(), range(0)
boolean-example.py
flag = False
isOn = True

Note

Booleans are subtype of integers. Objects are considered falsy if they define the method __bool__ that returns False or __len__ that returns 0. This will be explored in the OOP chapter.

Complex

Complex numbers are numbers that have a real and an imaginary part. Both parts are floating point numbers.

complex-example.py
x = 1.23 + 4.56j
y = 1.23 - 4.56j
z = 2j
t = 1 + 0j
u = 0j

Note

The letter j is the imaginary unit, it is defined as the square root of -1. It should exist in a complex number, otherwise it will be treated as a floating point number.

Numbers are created by numeric literals or as a result of operations and built-in functions (including their constructors). Numeric literals that contain a decimal point or an exponent are floating point numbers. Adding the j or J suffix yields a complex number.

Numbers

We can group digits in a number using underscores. This is called number literals.

w = 1_000_000_000
x = 1_000_000_000.0
y = 1_000_000_000.0_123_456_789
z = 22_333 + 5_123.6_789j

Exercises

Exercise 1: Invalid Variable Name

Remove illegal characters from the variable’s name

2m-first_name =42
Answer:
mfirst_name = 42

Exercise 2: Variable type

Give the type of each variable

var = 3
hello = "123"
x = 123
y = 3.14
z = '2.718281'
test = "Something"
k = "True"
p = False
10 = Ready
Answer:
int
str
int
float
str
str
str
bool
Error

Exercise 3: Spot the mistake

Spot the mistake

V_ar = "Hello World'
Answer:

We are using a double quotation from the left side, but a single quotation from the right side.

Course Progress

Section 2 of 17

Back to Course