Python Programming: Zero to Confident
Chapter 2 / 9· 20 min read· 0 cards

Variables, Data Types and Operators

How Python stores information in numbers, text, and true/false values — and how to do math with it.

Variables: labelled boxes for your data

A variable is just a name that points to a value. You create one with an equals sign — the name on the left, the value on the right:

age = 19
city = "Ahmedabad"
price = 49.99
is_student = True

Read = as "is set to", not "equals" in the math sense. age = 19 means "make a box called age and put 19 in it". You can change what's in the box any time, and you can use the box's name anywhere you'd use the value:

age = 19
print(age)        # shows 19
age = age + 1     # take what's in age, add 1, put it back
print(age)        # shows 20

Good variable names are lowercase and describe what they hold — total_price, user_name, num_students. Avoid x and data for anything that matters; future-you will thank present-you.


The core data types

Every value in Python has a type, and the type decides what you can do with it. The four you'll use constantly:

  • int — whole numbers: 19, -4, 1000000
  • float — numbers with a decimal point: 49.99, 3.14, -0.5
  • str — text, always in quotes: "Ahmedabad", 'hello'
  • bool — a truth value, either True or False

You can ask Python the type of anything with type():

print(type(19))        # <class 'int'>
print(type(49.99))     # <class 'float'>
print(type("hello"))   # <class 'str'>
print(type(True))      # <class 'bool'>

Python figures out the type automatically from the value — you never have to declare it. This is called dynamic typing, and it's a big part of why Python feels so light.


Doing math with operators

Python is a calculator that never gets tired. The arithmetic operators:

print(7 + 3)    # 10   addition
print(7 - 3)    # 4    subtraction
print(7 * 3)    # 21   multiplication
print(7 / 3)    # 2.333...  division (always a float)
print(7 // 3)   # 2    floor division (drops the decimal)
print(7 % 3)    # 1    modulo (the remainder)
print(7 ** 3)   # 343  exponent (7 to the power 3)

Two of these surprise beginners. The single slash / always gives a decimal, even 6 / 2 gives 3.0. And the modulo % gives the remainder after division — 7 % 3 is 1 because 3 goes into 7 twice with 1 left over. Modulo is secretly one of the most useful operators: number % 2 == 0 is how you check if a number is even.


Type conversion — the classic beginner trap

Remember input() from the last chapter? It always gives you back text, even if the user typed a number. This bites everyone once:

age = input("Your age: ")   # user types 19
print(age + 1)              # 💥 ERROR — can't add a number to text

The fix is to convert the text into a number with int() (or float() for decimals):

age = int(input("Your age: "))   # convert text "19" into number 19
print(age + 1)                   # 20 — works!

You can convert the other way too — str(20) turns the number 20 into the text "20", which you need when gluing numbers into a sentence with +. Keep this rule in your head: input gives text; convert before you calculate.


f-strings: the clean way to build text

Gluing text with + gets ugly fast, especially with numbers you have to convert. Modern Python has a much nicer tool called the f-string. Put an f before the opening quote, then drop variables right inside curly braces:

name = "Mehul"
age = 19
print(f"Hi {name}, next year you'll be {age + 1}.")
# Hi Mehul, next year you'll be 20.

Notice you can even do math inside the braces, and you never have to call str() — Python handles the conversion. From here on, f-strings are the way we'll build all our output. They're cleaner, easier to read, and far less error-prone. Practise the pattern until it's automatic, because you will type it thousands of times.

Reading mode · scroll to read at your own pace

Finished "Variables, Data Types and Operators"?

Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.

Was this chapter clear?

Try it yourself — open the Code Playground15+ languages — Python, JavaScript, Java, C++, SQL & more — full IDE-style editor, instant run. Your code is auto-saved per language.