Python Variables and Data Types
Create variables and use the four basic data types: str, int, float and bool. You'll also see why "5" and 5 are not the same thing. A lot of beginner bugs start there.
Course content
Sign in to save your progress.
-
Before You Write Code
-
Introduction to Python Programming: What It Is and Why Start Here
-
Installing Python and Running Your First Script
-
-
-
The Basics You Cannot Skip
-
Python Variables and Data Types
-
Python Operators and Expressions
-
Getting Input and Printing Output in Python
-
-
-
Making Decisions and Repeating Work
-
Python If Statements: Making Your Code Decide
-
Python Loops: For, While, and When to Stop
-
-
-
Organizing Your Code
-
Python Functions: Stop Copying and Pasting
-
Python Lists and Dictionaries
-
-
-
When Things Go Wrong
-
Reading Python Error Messages Instead of Panicking
-
Python Try and Except: Handling Errors Properly
-
-
- Final Check: Python for Beginners
Python Variables and Data Types
A variable is a name attached to a value. That is all. You give the value a name so you can use it later without retyping it, and so the code explains itself. price = 250 tells the reader much more than 250 sitting alone in the middle of a calculation.
Skip ahead: If you already know the difference between "5" and 5 and why "5" + 5 fails, continue to "Python Operators and Expressions."
Why this matters
A misspelled variable name and a value of the wrong type can both stop an otherwise sensible program. A value that looks like a number may still be text. Check the name, the value and its type when an expression behaves differently from what you expected.
Creating variables
customer = "Ana Reyes"
quantity = 3
unit_price = 149.75
is_member = True
print(customer, quantity, unit_price, is_member)
The = sign here does not mean "equals" like in math. It means "store the value on the right under the name on the left." You can change it later:
quantity = 3
quantity = quantity + 2
print(quantity) # 5
Rules for names: letters, numbers, and underscores only, and they cannot start with a number. They are case-sensitive, so Price and price are two different variables. Python style is lowercase with underscores: unit_price, not UnitPrice or up.
The four basic data types
Every value has a type. These four cover most beginner work:
- str (string): text, in quotes.
"Ana",'hello',"42". - int (integer): whole numbers.
3,-10,2026. - float: numbers with a decimal point.
149.75,0.5. - bool (boolean):
TrueorFalse, with capital letters.
You can always ask Python what type something is:
print(type("42")) # <class 'str'>
print(type(42)) # <class 'int'>
print(type(42.0)) # <class 'float'>
print(type(True)) # <class 'bool'>
Text that looks like a number is still text
This is the one that catches everybody. "42" with quotes is a string. It looks like a number, but Python treats it as text. Watch:
print("5" + "5") # 55 (text joined together)
print(5 + 5) # 10 (numbers added)
And mixing them fails:
age = "30"
try:
print(age + 1)
except TypeError as error:
print("Error:", error)
Python refuses to guess whether you meant text or math. Honestly, I think that is the right call. Some languages guess for you, and the guess is wrong exactly when it matters. You convert it yourself instead:
age = "30"
age_number = int(age)
print(age_number + 1) # 31
price = float("149.75")
label = str(3) + " items"
print(price, label)
⚠ Warning: int("12.5") and int("abc") both raise a ValueError. Converting only works when the text actually looks like the target type. When the text comes from a user or a file, assume it can be wrong. The lesson on try and except shows how to handle that safely.
A real-world example
Say you read an order quantity from a spreadsheet export. It arrives as text: "3". Multiply it by another whole number and you get "333", the text repeated three times. Multiply it by a price like 149.75 and you get a TypeError. Either way, wrong invoice or no invoice. One int() at the right place fixes it. The bug is not complicated. It is just invisible if you do not know to look for it.
Common mistakes
- Typos in names.
quanttyinstead ofquantitygives aNameError. Python does not autocorrect. - Lowercase booleans.
trueis notTrue. Only the capitalized one works. - Vague names.
x,data,temp2. They save three seconds of typing and cost thirty minutes of reading later. - Using a built-in name. Do not call a variable
list,str,type, orinput. It hides the built-in, and the error shows up somewhere else entirely.
💡 Tip: When something behaves strangely, print the value and its type together: print(repr(value), type(value)). repr shows the quotes, so you can see immediately that '30' is text.
What happens if you skip this step
You write code that works with the examples you tried and fails with real data. You will not know why, because the values look right when you print them. Types are boring. I agree. But skipping them means debugging with your eyes closed.
Quick check: what does "2" * 3 print, and why is it not 6?
About
Create variables and use the four basic data types: str, int, float and bool. You'll also see why "5" and 5 are not the same thing. A lot of beginner bugs start there.
- Duration
- 5 min
- Last update
- 09/15/2026
Comments (0)
Sign in to commentNo comments yet.