Learn Python in 30 Days — Day 2: Variables & Data Types

Learn Python in 30 Days — Day 2: Variables & Data Types

Welcome to Day 2 of the Learn Python in 30 Days series!
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
Yesterday, you wrote your very first Python program. Today, you’ll learn how to make your code remember information using variables and how to work with different kinds of data.

By the end of this lesson, you’ll understand:

  • What variables are and how to create them

  • Python’s three most common data types — numbers, strings, and booleans

  • How to combine text (concatenation)

  • How to format output neatly with f-strings

  • How to check what type of data a variable holds


Step 1 – What’s a Variable?

A variable is like a labelled box that stores a piece of information in your computer’s memory.
You can give it a name, put something inside it, and use that name to get the value later.

name = "Matty" age = 30

Here:

  • name stores a string ("Matty")

  • age stores an integer (30)

Python figures out what type of data each variable holds automatically — you don’t have to declare it.

You can print them like this:

print(name) print(age)

Try typing this into the IDLE IDE and running it and you should see the following output.

You can also download this example from my GitHub here and run it yourself.

Step 2 – Numbers (Integers & Floats)

Python handles numbers very easily — but not all numbers are the same.

  • Integers (int) – whole numbers without a decimal point.
    Examples: 5, 0, -27.
    You’ll use these for counting, looping, or indexing items in a list.

  • Floats (float) – numbers with decimals.
    Examples: 3.14, 0.5, -2.7.
    These are used whenever you need more precision, like measurements, averages, or calculations involving fractions.

Example:

a = 10 # integer b = 3.5 # float (decimal)

You can do maths directly and get Python to store the result in a new variable:

result = a + b print(result)

Python automatically promotes results to a float when you mix integers and floats.

Try typing in this code for yourself then run and you should get the following results: -

You can also download this example from my GitHub here and run it yourself.

Experiment for yourself, try using -, *, /, and % .

Step 3 – Strings (Text)

A string is just a sequence of characters — letters, numbers, or symbols — enclosed in quotes.

greeting = "Hello"

Strings are used for any kind of text. You can use either single (' ') or double (" ") quotes, just be consistent.

You can join strings together using the + operator and this is called concatenation:

name = "Matty" print(greeting + " " + name)

This will output:

Hello Matty
You can also download this example from my GitHub here and run it yourself.

You can also combine text with variables, but you can’t directly mix text and numbers Python will complain if you try:

age = 30 print("I am " + age + " years old") # This causes an error
You can also download this example from my GitHub here and run it yourself.

To fix this, convert the number into a string first:

print("I am " + str(age) + " years old") # Works fine

You can also download this example from my GitHub here and run it yourself.

Or, even better, use f-strings (see next step).

Step 4 – F-Strings (Formatted Strings)

F-strings make combining text and variables simple and clean.
They’re written by adding an f before the quotes, and placing variable names inside curly braces {}.

name = "Matty" age = 30 print(f"My name is {name} and I am {age} years old.")

Anything inside {} is replaced with the variable’s value.
You can even include calculations:

print(f"Next year, I’ll be {age + 1} years old!")

F-strings are the modern, preferred way to format strings in Python.

Try writing these for yourself, this should be the following output

You can also download this example from my GitHub here and run it yourself.

Step 5 – Booleans (True or False)

A boolean represents a truth value, it can only be True or False.

is_python_fun = True print(is_python_fun)

Try this for yourself and get the following result
You can also download this example from my GitHub here and run it yourself.

Booleans are extremely useful for logic and decision-making, which you’ll explore later using if statements.

Step 6 – Checking Variable Types

Sometimes you’ll want to see what kind of data a variable holds.
Use Python’s built-in type() function:

print(type(name))       # <class 'str'> print(type(age))        # <class 'int'> print(type(is_python_fun)) # <class 'bool'>


Try this for yourself and get the following result
You can also download this example from my GitHub here and run it yourself.

This is especially useful when you start working with user input or reading data from files later in the series.

Mini-Challenge

Create a short program that stores your:

  • Name

  • Age

  • Whether you like Python (True or False)

Then print a sentence like this using an f-string:

“Hi, I’m Matty, I’m 30 years old and it’s True that I like Python!”

You can also download the code for this challenge from my GitHub here and run it yourself.

💡 Key Takeaways

  • Variables store information you can reuse.

  • Integers are whole numbers; floats include decimals.

  • Strings hold text and can be combined with + or f-strings.

  • Booleans represent truth values (True/False).

  • type() shows the data type of a variable.


Next up: Day 3 – User Input & Basic Math

You’ll learn how to take input from the user and build your first interactive program a simple calculator!

All example files for this series are available on my GitHub: Learn-Python-in-30-Days

Comments

Popular posts from this blog

Math Behind Logic Gates

6502 - Part 2 Reset and Clock Circuit

Building a 6502 NOP Test