Learn Python in 30 Days — Day 2: Variables & Data Types
Learn Python in 30 Days — Day 2: Variables & Data Types
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.
Here:
-
namestores a string ("Matty") -
agestores 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:
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:
You can do maths directly and get Python to store the result in a new variable:
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.
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:
This will output:
You can also combine text with variables, but you can’t directly mix text and numbers Python will complain if you try:
To fix this, convert the number into a string first:
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 {}.
Anything inside {} is replaced with the variable’s value.
You can even include calculations:
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.
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:
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 (
TrueorFalse)
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
Post a Comment