Variables and Data Types (commonly used) in Python.
Python Variables and Data Types
Learn what variables are and how Python stores different kinds of values such as numbers, text, Boolean values, lists, tuples, and dictionaries.
What is a variable?
A variable is a name that refers to a value in a Python program. Variables allow you to store information and use that information later.
name = "Ali"
age = 20
In this example, name refers to the string
"Ali", while age refers to the
integer 20.
Python variable naming rules
Python variable names can contain letters, numbers and underscores, but they cannot start with a number.
- A variable name can start with a letter or underscore.
- It can contain letters, numbers and underscores.
- It cannot contain spaces.
- Variable names are case-sensitive.
name = "Ali"
user_name = "Ali"
age2 = 20
_score = 100
name = "Ali"
NAME = "Sara"
name and NAME are different
variable names because Python is case-sensitive.
Common Python data types
A data type describes the kind of value stored in a variable. Some of the most commonly used Python data types are integers, floats, strings, Booleans, lists, tuples and dictionaries.
Integer â int
Whole numbers without a decimal point.
age = 20
number = 5 + 10
print(number)
15
Integers can be positive, negative or zero.
Float â float
Numbers that contain a decimal point.
price = 20.5
temperature = 25.0
print(price)
20.5
Examples of floating-point numbers include
20.5, 1.5 and
3.14.
String â str
Text made up of characters.
name = "Hello World"
print(name)
Hello World
Strings are written inside quotation marks. A string can contain letters, numbers, spaces and other characters.
message = "This is a string"
number_text = "12345"
Even though "12345" looks like a number, it is
a string because it is enclosed in quotation marks.
Boolean â bool
A Boolean represents one of two values:
True or False.
is_student = True
is_logged_in = False
print(is_student)
True
Boolean values are commonly used with conditions such as
if statements.
List â list
A collection of values stored in a specific order.
fruits = ["apple", "banana", "cherry"]
print(fruits)
['apple', 'banana', 'cherry']
Lists use square brackets []. A list can
contain different types of values and its contents can
be changed.
Tuple â tuple
An ordered collection of values that cannot be changed after creation.
colors = ("red", "green", "blue")
print(colors)
('red', 'green', 'blue')
Tuples are commonly written using parentheses
().
Dictionary â dict
A collection of key-value pairs.
person = {
"name": "Ali",
"age": 20
}
print(person["name"])
Ali
Dictionaries use curly braces {} and store
information as key-value pairs.
In this example, "name" is a key and
"Ali" is its value.
Quick data type reference
20
20.5
"Hello"
True
[1, 2, 3]
(1, 2, 3)
{"name": "Ali"}
Quick check
What data type is stored in the following variable?
age = 25
Show answer
Integer (int) â
because 25 is a whole number without
a decimal point.
Variables and Data Types (commonly used) in Python.
Python for beginners: Variables and their Data Types. | what is a Variable in Python? | Commonly used Datatypes
Related
About this Python course
This Python course introduces beginners to programming through simple lessons, examples and step-by-step explanations.
The lessons gradually introduce Python concepts such as printing output, variables, data types, input, conditions, loops and other programming fundamentals.