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_1 is valid, but 1_server causes a SyntaxError.
  • Case-sensitive: ticket_count and Ticket_Count are 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_CASE for 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" + 10 stops immediately with a TypeError rather than guessing whether you wanted "5010" or 60.

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" or list = 10 overwrites Python’s built-in str() and list() functions. Use descriptive names like message_str or ticket_list.
  • Writing lowercase true or false: Python requires capitalised first letters: True, False and None.
  • Expecting int("45.6") to work directly: Passing a decimal string directly into int() raises a ValueError. Use int(float("45.6")).
  • Forgetting that input() always returns a string: Always wrap numeric user input in int() or float().
Key takeaways
  • Variables in Python are labels attached to objects in memory; follow PEP 8 by using snake_case for variables and UPPER_SNAKE_CASE for constants.
  • The five foundational scalar types are int, float, str, bool and NoneType.
  • Python is both dynamically typed and strongly typed.
  • Always convert external text inputs with int() or float() before performing calculations.

Related Lessons