Variables, Data Types and Type Conversion in Python
Every useful programme needs to remember information: a user’s email address, the number of open incident tickets, the percentage of disk space remaining or whether a server is online. In Python, you store and track this information using variables and data types. Understanding how Python labels values in memory—and how it converts one type into another—prevents some of the most common bugs in scripting and data processing.
What Is a Variable in Python?
In Python, it is accurate to think of a variable as a named label attached to an object in memory. You create a variable using the assignment operator (=):
open_tickets = 14
cpu_usage_percent = 68.5
environment = "Production"
is_maintenance_window = False
Python uses dynamic typing, meaning the interpreter automatically works out the data type from the value you assign without needing explicit type declarations beforehand.
Naming Rules and PEP 8 Conventions
- Allowed characters: Letters (
a–z,A–Z), digits (0–9) and underscores (_). - No leading digits:
server_1is valid, but1_servercauses aSyntaxError. - Case-sensitive:
ticket_countandTicket_Countare two completely different variables. - Use
snake_case: Write ordinary variable names in lowercase words separated by underscores (for example,max_retry_attempts). - Use
UPPER_SNAKE_CASEfor constants: When a value should not change, write it in capitals (for example,DEFAULT_TIMEOUT_SECONDS = 30).
The Core Built-in Data Types
| Data Type | Python Keyword | Examples | Typical IT Use Case |
|---|---|---|---|
| Integer | int |
0, 42, -15, 10_000_000 |
Counting items, port numbers (8080), HTTP status codes (200, 404) |
| Floating-Point | float |
3.14, 99.95, -0.01 |
Response latencies, CPU percentages, financial metrics |
| String | str |
"srv-web-01", 'admin@company.com' |
Hostnames, file paths, log messages, user names |
| Boolean | bool |
True, False |
Feature flags, health statuses, conditional checks |
| NoneType | None |
None |
Representing a missing value, an empty field or a not-yet-set variable |
Dynamic AND Strong Typing: What Is the Difference?
- Dynamically typed: A variable label can be reassigned to a different type of object later in the script.
- Strongly typed: Python refuses to silently combine incompatible types in dangerous ways. Trying to evaluate
"50" + 10stops immediately with aTypeErrorrather than guessing whether you wanted"5010"or60.
Checking and Converting Types (Type Casting)
| Function | What It Does | Example | Result |
|---|---|---|---|
type(x) |
Returns the current data type of x |
type(42) |
<class 'int'> |
int(x) |
Converts a string or float to an integer (truncates decimals) | int("8080") / int(9.8) |
8080 / 9 |
float(x) |
Converts a string or integer to a decimal float | float("99.95") |
99.95 |
str(x) |
Converts any object into its text representation | str(404) |
"404" |
bool(x) |
Converts a value to True or False |
bool(0) / bool(5) |
False / True |
A Short Example: Calculating Server Storage Capacity
# Raw values read from a configuration source (strings)
raw_total_gb = "500"
raw_used_gb = "342.75"
# Convert strings to numbers before performing arithmetic
total_gb = int(raw_total_gb)
used_gb = float(raw_used_gb)
free_gb = total_gb - used_gb
usage_ratio = (used_gb / total_gb) * 100
print(f"Free Storage: {free_gb} GB")
print(f"Disk Utilisation: {round(usage_ratio, 1)}%")
Output:
Free Storage: 157.25 GB
Disk Utilisation: 68.5%
Common Mistakes
- Shadowing built-in function names: Naming a variable
str = "hello"orlist = 10overwrites Python’s built-instr()andlist()functions. Use descriptive names likemessage_strorticket_list. - Writing lowercase
trueorfalse: Python requires capitalised first letters:True,FalseandNone. - Expecting
int("45.6")to work directly: Passing a decimal string directly intoint()raises aValueError. Useint(float("45.6")). - Forgetting that
input()always returns a string: Always wrap numeric user input inint()orfloat().
- Variables in Python are labels attached to objects in memory; follow PEP 8 by using
snake_casefor variables andUPPER_SNAKE_CASEfor constants. - The five foundational scalar types are
int,float,str,boolandNoneType. - Python is both dynamically typed and strongly typed.
- Always convert external text inputs with
int()orfloat()before performing calculations.