Learn.Dedaandsons
Learn.Dedaandsons Python for Beginners
đŸ’ģ Variables and Data Types

Variables and their Data Types. | what is a Variable in Python? | Commonly used Datatypes

Course progress Lesson Variables and Data Types
Lesson 2 / 5

Variables and Data Types (commonly used) in Python.

Python Beginner Lesson

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.

Python
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.
Valid examples
name = "Ali"
user_name = "Ali"
age2 = 20
_score = 100
Case-sensitive
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.

01

Integer — int

Whole numbers without a decimal point.

Python
age = 20
number = 5 + 10

print(number)
15

Integers can be positive, negative or zero.

02

Float — float

Numbers that contain a decimal point.

Python
price = 20.5
temperature = 25.0

print(price)
20.5

Examples of floating-point numbers include 20.5, 1.5 and 3.14.

03

String — str

Text made up of characters.

Python
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.

04

Boolean — bool

A Boolean represents one of two values: True or False.

Python
is_student = True
is_logged_in = False

print(is_student)
True

Boolean values are commonly used with conditions such as if statements.

05

List — list

A collection of values stored in a specific order.

Python
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.

06

Tuple — tuple

An ordered collection of values that cannot be changed after creation.

Python
colors = ("red", "green", "blue")

print(colors)
('red', 'green', 'blue')

Tuples are commonly written using parentheses ().

07

Dictionary — dict

A collection of key-value pairs.

Python
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

int Whole numbers 20
float Decimal numbers 20.5
str Text "Hello"
bool True or False True
list Ordered collection [1, 2, 3]
tuple Unchangeable collection (1, 2, 3)
dict Key-value collection {"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