Welcome to the exciting world of Python! Before we start storing information, it's crucial to understand the different 'kinds' of information Python can handle. These are called data types. Think of them as containers designed for specific kinds of contents. Just like you wouldn't store water in a sieve, you want to use the right data type for the right kind of data to ensure your programs work efficiently and correctly.
Let's explore the fundamental data types you'll encounter most often in Python:
1. Integers (int)
These are whole numbers, positive or negative, without any decimal points. They are perfect for counting, indexing, and performing mathematical operations that don't require fractions.
age = 30
count = -5
year = 20232. Floating-Point Numbers (float)
These are numbers that have a decimal point. They are used for measurements, calculations involving fractions, and any time you need to represent a value with a fractional part.
price = 19.99
pi = 3.14159
temperature = -2.53. Strings (str)
Strings are sequences of characters. They are used to represent text, like names, messages, or any kind of textual data. You define strings by enclosing the characters in quotation marks (either single '...' or double "...").
name = "Alice"
message = 'Hello, Python!'
address = "123 Main St."4. Booleans (bool)
Booleans represent truth values. They can only be one of two things: True or False. These are fundamental for making decisions in your code, like controlling loops or conditional statements.
is_active = True
is_raining = False
completed = True