LEARN Python - 1.1 Integers, Floats & Strings Python's Favorite Snack Flavors

Learn Python – 1.1: Integers, Floats & Strings: Python’s Favorite Snack Flavors

Python doesn’t live on coffee and snacks like you do, it eats data.
And not just any data, it has a few favorite flavors: integers, floats, and strings.

These are Python’s building blocks, the basic ingredients behind every program you’ll ever write.
Whenever you make a variable, you’re handing Python a little snack, a number, a decimal, or a piece of text, and it treats each one differently depending on its flavor.

Here’s a simple way to think about it:

  • Integers are solid; whole numbers like 1, 42, or -7.

  • Floats are smooth; numbers with decimals, like 3.14 or 19.99.

  • Strings are sweet; pieces of text, like "Hello" or "Python is awesome!".

Together, they cover nearly everything you’ll need as a beginner.
Once you know how to use these three types (and when to mix them), you’ll have the foundation to do real math, write programs that talk, and build things that actually feel alive.

So grab your digital fork. Today, we’re tasting Python’s favorite snacks.

zerotopyhero logo superpyduck

SuperPyDuck
Fun Fact

Python doesn’t care if your number is 10 or 10.0,
but it does care if you forget the quotes around your string.

zerotopyhero logo superpyduck

SuperPyDuck
Fun Fact

Python doesn’t care if your number is 10 or 10.0, but it does care if you forget the quotes around your string.

What Is a Data Type, Really?

Before we start snacking, let’s clear up what “data type” actually means, because it sounds scarier than it is.

A data type is simply a label that tells Python what kind of information it’s dealing with.
When you store something in a variable, Python looks at it and quietly says:

“Oh, that’s a number,” or “That’s text,” or “That’s a decimal.”

Different data types behave differently.
You can add numbers together, but you can’t add text and numbers the same way.
Python needs to know how to treat the data you give it, that’s why these labels exist.

Think of It Like a Kitchen

Imagine you’re cooking with three containers on your counter:

  • One holds whole apples (integers).

  • Another has apple slices (floats).

  • The third has labels that say “apple” (strings).

They’re all apples in some sense, but you’d treat each one differently, right?
You can’t bake a pie with the labels. You can’t eat the text.
That’s how Python works too; it needs to know what kind of thing it’s working with.

Here’s a quick peek at the three main data types you’ll use all the time:

TypeWhat It IsExampleDescription
intInteger5, -10, 2025Whole numbers — no decimals.
floatFloating point3.14, 0.5, -2.75Numbers with decimals.
strString"Hello", 'Python'Text inside quotes.

Every value you create in Python fits into one of these types, and once you get comfortable with them, everything else will start to click.

Integers: The Whole Snack

Let’s start with the simplest and crunchiest data type: Integers.
Integers (or int for short) are whole numbers. No decimals, no fractions, no drama. Just solid digits; the kind you learned to count with as a kid.

Examples:

				
					apples = 5
temperature = -2
year = 2025
				
			

Python automatically understands these are integers. You don’t need to label them or declare a type, it just knows.

Doing Math with Integers

Integers are Python’s favorite for math problems. You can add, subtract, multiply, or divide them like this:

				
					x = 10
y = 3

print(x + y)
print(x - y)
print(x * y)
print(x / y)
				
			

Output:

				
					13
7
30
3.3333333333333335
				
			
zerotopyhero logo superpyduck

SuperPyDuck
Fun Fact

That sneaky 5 at the end of 3.3333333333333335
shows up because computers store decimals in binary,
and some fractions (like 10 ÷ 3) can’t be represented exactly!

zerotopyhero logo superpyduck

SuperPyDuck
Fun Fact

That sneaky 5 at the end of 3.3333333333333335
shows up because computers store decimals in binary,
and some fractions (like 10 ÷ 3) can’t be represented exactly!

Notice something interesting? Even though x and y were integers, Python turned the division result into a float. Because division in Python with only one / always leads to floats. 

Keeping It Whole

If you want to do division but keep the result a whole number, you can use floor division written as //:

				
					print(10 // 3)
				
			

Output:

				
					3
				
			

Python simply chops off the decimal and rounds down.

Large Numbers Made Easy

Python can handle enormous integers, and lets you make them easier to read with underscores:

				
					world_population = 7_900_000_000
print(world_population)
				
			

Output:

				
					7900000000
				
			

The underscores don’t change the value. They just make big numbers readable, kind of like commas for your eyes. If you’re confident enough, you can just not put them there. 

Floats: When Numbers Go Swimming

If integers are solid snacks, floats are their smooth, creamy cousins.
Floats (short for floating-point numbers) are numbers that include a decimal point,  even if that decimal ends in .0.

They’re what you use when you need precision: money, measurements, or math that doesn’t land neatly on whole numbers.

Examples:

				
					pi = 3.14
price = 19.99
temperature = -12.5

				
			

Python automatically knows these are floats, no extra setup required.

Doing Math with Floats

Floats behave just like integers in math operations, except they float (pun intended).

				
					x = 3.5
y = 2.5

print(x + y)
print(x - y)
print(x * y)
print(x / y)

				
			

Output:

				
					6.0
1.0
8.75
1.4

				
			

Even if the math result could be a whole number (like 3.5 + 2.5 = 6), Python still adds .0 to remind you it’s a float.

Float Quirks (Why 0.1 + 0.2 Isn’t 0.3)

Now, brace yourself for the first moment every coder meets Python’s little decimal glitch:

				
					print(0.1 + 0.2)

				
			

Output:

				
					0.30000000000000004

				
			

Wait, what?!

No, your computer’s not broken. It’s just how binary math works under the hood.
Python (like almost every language) represents decimals in a way that isn’t perfectly exact.

It’s weird, but harmless, just something you need to know exists.
If you ever need precise decimals (like for money), Python has special tools for that later on.

Mixing Floats and Integers

When you mix an integer and a float in a math operation, Python automatically upgrades the result to a float:

				
					print(5 + 2.0)

				
			

Output:

				
					7.0

				
			

Python thinks,

“Well, one of these is a float, so let’s play it safe and float everything.”

It’s the safest option for Python, so it doesn’t risk anything and outputs a float. You can have a thousand integers, but if you have one float in your calculation, the output will always be a, you guessed it, a float.

Strings: Python’s Sweet Tooth

After all that number crunching, Python deserves dessert and its favorite treat is strings.

A string (type: str) is simply text, anything wrapped in quotes.
Letters, words, sentences, even emojis, if it’s inside " " or ' ', Python treats it as a string.

Examples:

				
					name = "Alex"
greeting = 'Hello, world!'
mood = "excited"

				
			

Strings are how Python stores human-readable information; names, messages, jokes, and sometimes error messages that hurt your feelings.

Quotation Marks 101

You can use either single or double quotes, Python doesn’t mind, as long as you’re consistent.

				
					message = "Python is fun!"
reply = 'I agree!'

				
			

Both work perfectly.
But what happens if your text also contains quotes?

				
					quote = 'Python's awesome!'

				
			

Boom! Error.
Python gets confused because it thinks the string ends early.

Fix it by using the other kind of quote:

				
					quote = "Python's awesome!"

				
			

Or use the escape character (\) to tell Python, “Hey, this quote isn’t the end, it’s part of the text”:

				
					quote = 'Python\'s awesome!'

				
			

The backslash is Python’s way of saying, ‘Don’t freak out, I meant that literally.’

Multi-Line Strings

Need a longer message that spans several lines? Use triple quotes.

				
					poem = """Roses are red,
Violets are blue,
Python is fun,
And so are you."""

				
			

When you print it, Python keeps the formatting:

				
					Roses are red,
Violets are blue,
Python is fun,
And so are you.

				
			

Perfect for longer messages, docstrings, or your future poetry career.

String Tricks and Fun

Strings aren’t just static text; you can combine or repeat them, too.

Add strings together (concatenation):

				
					first = "Hello"
second = "there"
print(first + " " + second)

				
			

The two quotation marks in the middle with the space in between, you ask? Great question! It’s there to add the space between the two words. 

Output:

				
					Hello there

				
			

Repeat them:

				
					laugh = "ha"
print(laugh * 3)

				
			

Output:

				
					hahaha

				
			

Python doesn’t care about your sense of humor, it just multiplies the laughter.

Strings Aren’t Numbers

This one trips up beginners:

				
					print("5" + "5")

				
			

Output:

				
					55
				
			

That’s not ten, that’s “five” glued to “five.”
Because Python treats them as text, not numbers.

Strings talk, numbers count. They don’t mix well without translation.

Converting Between Types

By now you’ve met Python’s three favorite flavors: Integers, floats, and strings.
But what if you accidentally mix them? What if your number is inside quotes, or your text is supposed to act like a number?

That’s when you need type conversion, teaching Python how to switch one data flavor into another.

Why Conversion Matters

Let’s look at a classic beginner mistake:

				
					age = "25"
print(age + 5)

				
			

You probably expect it to print 30, right?
But Python throws this little tantrum instead:

				
					TypeError: can only concatenate str (not "int") to str

				
			

Translation:

“You’re trying to mix text and numbers. I’m not doing that.”

To fix it, you have to tell Python, “Hey, that thing inside the quotes? Treat it like a number.”

Converting Text to Numbers

Use int() or float() to turn strings into numbers:

				
					age = "25"
age = int(age)
print(age + 5)

				
			

Output:

				
					30

				
			

Or if it’s a decimal:

				
					price = "19.99"
price = float(price)
print(price * 2)

				
			

Output:

				
					39.98

				
			

Think of int() and float() as translators between the land of words and the land of math.

Converting Numbers to Text

The reverse works, too. If you want to combine numbers and strings, you can use str() to turn the number into text:

				
					age = 25
print("I am " + str(age) + " years old.")

				
			

Output:

				
					I am 25 years old.

				
			

Without str(), that line would have crashed, but now Python knows to treat 25 as text.

Not Everything Can Be Converted

You can’t just turn any word into a number. Try this:

				
					num = int("hello")

				
			

Python will reply:

				
					ValueError: invalid literal for int() with base 10: 'hello'

				
			

That’s Python’s polite way of saying,

“I have no idea what kind of number ‘hello’ is.”

Quick Conversion Reference

FunctionConvertsExampleResult
int()text or float → integerint("7")7
float()text or integer → floatfloat(7)7.0
str()anything → textstr(7.0)"7.0"

Mixing Data Types Carefully

Now that you know how to switch between data types, let’s talk about what happens when you mix them, and how to do it without chaos.

Python is smart, but also a little strict.
It likes clear instructions, especially when it comes to combining numbers and text.

When Things Go Wrong

If you try this:

				
					name = "Alex"
age = 25
print("My name is " + name + " and I’m " + age + " years old.")

				
			

Python frowns and says:

				
					TypeError: can only concatenate str (not "int") to str

				
			

Because "Alex" and "I’m " are strings (text), but 25 is an integer (a number).
Python refuses to mash them together until you tell it how.

It’s like trying to glue numbers and words together without tape, Python just won’t do it.

Option 1: Convert the Number

You can fix it by turning the number into text with str():

				
					name = "Alex"
age = 25
print("My name is " + name + " and I’m " + str(age) + " years old.")

				
			

Output:

				
					My name is Alex and I’m 25 years old.

				
			

Simple and effective, just a bit long-winded.

Option 2: Use f-Strings (The Easy Way)

Python’s f-strings make combining text and variables a breeze.
They let you drop variables directly into a string.  No +, no str(), no confusion.

				
					name = "Alex"
age = 25
print(f"My name is {name} and I’m {age} years old.")

				
			

Output:

				
					My name is Alex and I’m 25 years old.

				
			

See how clean that looks?
The f in front of the string tells Python to format it, every { } is replaced by the value of a variable.

f-strings are like Python’s version of fill-in-the-blank sentences; fast, simple, and almost impossible to mess up.

Option 3: Using Commas in print()

There’s also a lazy shortcut. The print() function can handle mixed types automatically if you separate them with commas:

				
					name = "Alex"
age = 25
print("My name is", name, "and I’m", age, "years old.")

				
			

Output:

				
					My name is Alex and I’m 25 years old.

				
			

It automatically adds spaces and handles conversions, though it’s less flexible for complex formatting later on.

Mixing Numbers: Float + Int

When mixing numbers, Python’s less dramatic.
If you combine an integer and a float, it just converts everything to float automatically:

				
					result = 3 + 2.5
print(result)

				
			

Output:

				
					5.5

				
			

Python thinks,

“Better safe than sorry, let’s float everything.”

Checking Data Types

(Hey, we’ve already had this? It’s what you think, right? We will iterate some stuff a lot. That’s how we learn.) 

Sometimes you’ll look at a variable and think, “Wait, what even are you?”
Is it text? A number? A float that snuck in while you weren’t looking?

Python has a built-in way to check: the type() function.
It tells you exactly what kind of data lives inside a variable, no guessing required.

Meet type()

				
					x = 42
y = 3.14
z = "Hello, Python!"

print(type(x))
print(type(y))
print(type(z))

				
			

Output:

				
					<class 'int'>
<class 'float'>
<class 'str'>

				
			

The part between the angle brackets (< >) tells you the data type.

  • int = integer (whole number)

  • float = number with a decimal

  • str = string (text)

type() is like a label maker for Python, it tells you exactly what kind of data you’re dealing with.

Why It’s Useful

Knowing the type helps you predict what Python will allow.

If something’s a string, you can glue it to another string ("Hello" + "World").
If something’s a number, you can add it or multiply it.
If you mix them, Python wants you to either convert one, or apologize with str().

Let’s test a few tricky cases:

				
					print(type(10 / 5))    # Division makes floats
print(type(10 // 5))   # Floor division stays int
print(type("10"))      # Still a string
print(type(int("10"))) # Now an integer

				
			

Output:

				
					<class 'float'>
<class 'int'>
<class 'str'>
<class 'int'>

				
			

See? Python changes the type depending on what you do.

Common Mistakes

Working with different data types can feel a bit like juggling flaming torches; fun, but sometimes painful.
Let’s go through the most common beginner slip-ups so you’ll recognize them instantly when they happen (because they will happen).

1. Mixing Text and Numbers Without Converting

				
					age = 25
print("I am " + age + " years old.")

				
			

Python immediately says:

				
					TypeError: can only concatenate str (not "int") to str

				
			

You can’t add numbers to strings using +, even if it looks like a harmless sentence.
Fix it with str() or use an f-string:

				
					print("I am " + str(age) + " years old.")
# or
print(f"I am {age} years old.")

				
			

2. Forgetting the Quotes Around Text

				
					name = Alex
print(name)

				
			

Python thinks Alex is a variable that doesn’t exist.

Result:

				
					NameError: name 'Alex' is not defined

				
			

Always wrap text in quotes:

				
					name = "Alex"
print(name)

				
			

Python isn’t psychic, if you don’t use quotes, it assumes you’re talking about another variable.

3. Forgetting to Convert Input

You’ll see this one a lot once you start using input().
Python’s input() function always gives you a string, even if the user types a number.

				
					age = input("How old are you? ")
print(age + 5)

				
			

This gives a TypeError, because "25" + 5 is nonsense to Python.

Convert it:

				
					age = int(input("How old are you? "))
print(age + 5)

				
			

Now it works perfectly.

4. Confusing = and ==

				
					age = 25
if age = 30:
    print("You're 30!")

				
			

Python throws:

				
					SyntaxError: invalid syntax

				
			

Because = is for assigning, not comparing.
Use == when checking if two things are equal:

				
					if age == 30:
    print("You're 30!")

				
			

= stores values. == compares them. Don’t mix their personalities.

Tired of making mistakes? Read this: Staying Motivated When Nothing in Python Works

Mini Challenge: The Snack Sampler

You’ve tasted Python’s favorite snack flavors. Now it’s time to throw them all into one little recipe and see how they work together.

Your Mission, if you choose to accept it

Create a new Python file called snack_types.py.
Your job: use one variable of each data type to tell a short story, with math involved.

Step 1: Create the Snacks

				
					fruit = "apple"     # String
apples = 5          # Integer
price = 3.5         # Float

				
			

So far, you’ve given Python three different data types:

  • A string ("apple")

  • An integer (5)

  • A float (3.5)

Step 1: Create the Snacks

Let’s calculate the total cost:

				
					total = apples * price

				
			

That works because Python knows integers and floats can play nicely together, the result will be a float (17.5).

Step 1: Create the Snacks

Now, turn that data into a readable sentence using an f-string:

				
					print(f"I bought {apples} {fruit}s for {price} dollars each.")
print(f"The total cost is {total} dollars.")

				
			

Output:

				
					I bought 5 apples for 3.5 dollars each.
The total cost is 17.5 dollars.

				
			

Boom! Your program just calculated, stored, and communicated information.

Step 4: Personalize It

Want to make it fun? Add your name, a store name, or your favorite fruit.
Try playing around with different data types, see what happens when you:

  • Change price to an integer.

  • Turn apples into a float.

  • Add a string that shows the currency ("USD").

Let's Wrap Up

You’ve officially met Python’s three favorite snacks — and now you can serve them up like a pro.

Here’s what you’ve learned today:

  • Integers (int); whole numbers for counting, adding, and scoring points.
  • Floats (float); numbers with decimals for anything involving precision (prices, measurements, math that isn’t neat).
  • Strings (str); text in quotes for names, messages, and storytelling.

You’ve also learned how to:

  • Mix and convert data types without chaos.

  • Check a variable’s type using type().

  • Avoid the classic “string + number” meltdown.

  • Write a simple program that combines words and numbers in one smooth line.

That’s a big leap, you’ve gone from simply storing data to understanding how Python interprets it.

It’s the difference between saying “I know words exist” and actually knowing grammar.

“Variables gave Python memory, but data types gave it personality.”

ZeroToPyHero