Home / Journal

Python (Part 2): Variables and Data Types

Python (Part 2): Variables and Data Types

In Part 1 we covered Python's basic syntax — running a script, the print() function, indentation, and comments. As promised, this time let's talk about variables and data types: how Python stores information so you can use it later.

If you followed the PHP series, this covers the same ground — but Python does a few things noticeably differently, and those differences are worth paying attention to.

What is a Variable?

Think of a variable as a labelled box where you keep a value — some text, a number, anything — and grab it again later by its name.

In Python, you create a variable just by assigning to it. There's no $ sign (like PHP) and no keyword like var or let — you write the name, an equals sign, and the value:

name = "Acronix"
age = 30

Here we made two variables: name holds the text "Acronix", and age holds the number 30. The equals sign (=) is the assignment operator — it puts the value on the right into the variable on the left.

Rules for Naming Variables

  • A name must start with a letter or an underscore (_) — never a number.
  • After that, it can contain letters, numbers, and underscores.
  • Names are case-sensitivename and Name are two different variables.
  • You can't use Python's reserved keywords (like if, for, class, True) as names.
user_name = "ok"   # valid
_token    = "ok"   # valid
2cool     = "no"   # invalid — starts with a number

By convention, Python uses snake_case for variable names — all lowercase, words joined by underscores (first_name, total_price). That's the style recommended by PEP 8, Python's official style guide, and it's what you'll see everywhere.

Displaying Variables

Use print() from Part 1 to show a variable's value:

name = "Acronix"
print(name)        # outputs: Acronix

The cleanest way to mix text and variables is an f-string — put an f right before the opening quote, then drop variables inside { }:

name = "Acronix"
print(f"Hello, {name}!")   # outputs: Hello, Acronix!

f-strings have been available since Python 3.6 and are the modern, preferred way to format text. You can even put expressions inside the braces:

age = 30
print(f"Next year you'll be {age + 1}.")   # outputs: Next year you'll be 31.

Dynamic Typing

Here's a key idea: Python is dynamically typed. You never declare what kind of value a variable holds — Python works it out from whatever you assign. Even better, the same variable can hold a different type later:

x = 10        # x is a number now
x = "hello"   # ...and now it's text — perfectly legal

This is flexible, but it's also a responsibility: it's on you to keep track of what's in a variable, because Python won't stop you from changing it.

The Core Data Types

The data types you'll meet first are:

  • str (string) — text wrapped in quotes: name = "Acronix"
  • int (integer) — whole numbers: age = 30
  • float — decimal numbers: price = 9.99
  • bool (boolean) — True or False

Watch the booleans closely if you're coming from another language: in Python they are capitalized — True and False. Lowercase true/false will throw an error. (PHP, by contrast, uses lowercase.)

is_logged_in = True
is_admin = False

A couple more you'll run into soon: None, Python's way of saying "no value at all" (its own type, NoneType), and complex numbers for math work. There are also the container types — lists, tuples, dictionaries, and sets — but those get their own lesson later in the series.

Checking a Type with type()

If you're ever unsure what type a value is, ask Python directly with the built-in type() function:

age = 30
print(type(age))      # outputs: <class 'int'>

price = 9.99
print(type(price))    # outputs: <class 'float'>

name = "Acronix"
print(type(name))     # outputs: <class 'str'>

The <class '...'> wording is just Python telling you which type the value belongs to.

A Note on Numbers

Two small things that surprise newcomers:

  • Python integers have no size limit — you can do math on enormous whole numbers and they just work, no overflow.
  • The moment you write a decimal point, you get a float: 10 is an int, but 10.0 is a float. That distinction matters once you start dividing and comparing numbers, which we'll get into next.

To Be Continued…

You can now store text, numbers, and booleans, and check what type you're holding with type(). In Part 3 we'll cover Python Operators — doing math, comparing values, and combining conditions — including a couple Python has that PHP doesn't, like floor division (//) and exponentiation (**). Stay tuned!

← All articles