Working with Basic Data Types
In this lesson, you will learn how to identify and use Python’s three fundamental data types to represent different kinds of information in your programs.
When we store data in variables, Python needs to know what kind of data it is so it knows how to handle it. Think of data types as labels that tell Python whether it should do math with a value or treat it as plain text.
Integers (int)
Integers are whole numbers without any decimal points. You use these for counting or representing values that don't need fractions.
age = 25
count = 100
Floating-point numbers (float)
"Floats" are numbers that contain a decimal point. Even if the decimal is .0, Python treats the number as a float. You use these whenever precision is required, such as for measurements or currency.
price = 19.99
pi = 3.14159
Strings (str)
Strings are used to store text. In Python, you define a string by wrapping your characters in either single quotes (') or double quotes ("). It doesn’t matter which you use, as long as you are consistent within the same string.
name = "Alice"
greeting = 'Hello, World!'
Why does this matter?
The data type matters because Python treats these values differently. For example, if you use the + operator on two integers, Python performs addition:
result = 5 + 5 # result is 10
However, if you use the + operator on two strings, Python performs "concatenation," which joins the text together:
sentence = "Hello" + "World" # sentence is "HelloWorld"
If you try to combine types that don't match (like adding a string to an integer), Python will stop and tell you there is an error. By choosing the right data type from the start, you ensure your program logic behaves exactly how you expect it to.
Key Takeaway: Python uses int for whole numbers, float for decimals, and str for text, and the type of data determines how your code interacts with those values.