Variables and Data Types in Python
Introduction to Variables in python
Variables in Python are names (mostly referred to as containers) that are used to store data values. You can think of them as labeled boxes that hold information so you can use or change it later.
Python is dynamically typed: (so a datatype declaration is not enforced beforehand)
A variable points to a value in memory
Variables and Data Types in Python
A full article is dedicated to explaining data types in detail, but here’s a brief overview of the basic data types you can assign to variables in Python:
String – A sequence of characters, enclosed in quotes.
Example:"Hello world"
Integer – Whole numbers without decimal points.
Example:1
,2
,3
,4
Float – Numbers with decimal points (floating-point numbers).
Example:3.2
,0.4
,4.23
Boolean – Represents either
True
orFalse
.
Example:True
,False
List – An ordered, changeable collection of items.
Example:[2, 4, 5]
Tuple – An ordered, unchangeable collection of items.
Example:(2, 4, 5)
Dictionary – A collection of key-value pairs.
Example:{"name": "Bright", "age": 26}
Set – An unordered collection of unique items.
Example:{1, 2, 3, 3}
→ becomes{1, 2, 3}
NoneType – Represents the absence of a value.
Example:None
Output:
Variable Naming Rules
There are a few important rules to follow when choosing a variable name in Python:
Variable names can include letters, numbers, and underscores.
A variable name cannot start with a number.
Variable names are case-sensitive (e.g.,
name
,Name
, andNAME
are all different).You cannot use reserved keywords (like
if
,class
, ordef
) as variable names.
Case Sensitivity
Python is a case-sensitive language, which means that “name” is not the same as “Name” if you pay close attention, there is a slight difference between both because the first is all lowercase while the second is capitalized
Output:
Reassigning Variables
In Python, variables are dynamically typed, meaning you don't need to declare their type ahead of time. More specifically, you can change the type of a variable at any point in your code.
Output: