Understand the different types of data Python can handle, including strings, integers, and floats.
Have you ever wondered why your calculator can add to get , but a computer might sometimes see '5' + '5' and give you '55' instead? It all depends on the 'type' of data you are using!
In Python, every piece of information has a data type. Think of these like different containers. An Integer (or int) is a whole number with no decimals, like or . A Floating-Point Number (or float) is a number that contains a decimal point, like or . Finally, a String (or str) is a sequence of characters wrapped in quotes, like "Hello" or even "123". Python treats the number very differently than the string "5" because they live in different containers!
Let's look at three different values and identify them: 1. is an Integer because it is a whole number. 2. is a Float because it has a decimal point. 3. "100" is a String because it is surrounded by quotation marks.
Quick Check
If you have the value , which data type is it?
Answer
It is a float (floating-point number) because it contains a decimal point.
Sometimes, especially when working with complex code, you might not know what type of data a variable is holding. Python provides a built-in tool called the `type()` function. When you pass a value into this function, Python tells you exactly what it is. For example, `type(10)` will return `<class 'int'>`. This is incredibly useful for debugging when your math isn't adding up quite right!
Imagine you are building a game score system: 1. Create a variable: `score = 15` 2. Check it: `type(score)` results in `int`. 3. Update it: `score = 15.5` (maybe a bonus point!) 4. Check again: `type(score)` now results in `float`.
Quick Check
What would `type("Python")` return?
Answer
<class 'str'> (or simply a String)
What if you have the string "20" but you need to add to it? You can't add a string and an integer directly. You must use Type Casting to convert the data. Functions like `int()`, `float()`, and `str()` act like magic wands. `int("20")` turns the string into the number . This is vital because when users type into a computer, Python usually sees that input as a string by default, even if they typed a number!
Let's solve a common problem where a user enters their age: 1. User input is received as a string: `user_age = "13"` 2. You want to see how old they will be next year: `next_year = int(user_age) + 1` 3. The `int()` function converts "13" to the number , allowing the math to work. 4. To print it back, you might cast it back: `print("Next year you are " + str(next_year))`.
Which of the following is a 'float' in Python?
What function would you use to turn the number into the string "10"?
In Python, the expression will result in a float.
Review Tomorrow
In 24 hours, try to explain to a friend the difference between and "7" in Python.
Practice Activity
Try this on your own: Create a variable for your height in centimeters (like ) and use the `type()` function to confirm it is a float.