Curriculum
Python supports several data types to store different kinds of information. Understanding these data types is essential because they determine how data is stored, manipulated, and used in your programs. In this sub-section, you’ll learn about four of the most common data types in Python:
In Python, different types of data are used to store and manipulate values. The four most commonly used data types are:
str
) – Used for textint
) – Used for whole numbersfloat
) – Used for decimal numbersbool
) – Used for True/False valuesUnderstanding these data types is essential for performing operations, making comparisons, and writing efficient Python programs.
str
)A string in Python is a sequence of characters enclosed in single quotes ('
), double quotes ("
), or triple quotes ('''
or """
). Strings are used to store textual data, such as names, messages, or any sequence of characters.
text1 = "Hello, Python!" # Using double quotes
text2 = 'Python is fun!' # Using single quotes
text3 = """This is a
multi-line string.""" # Using triple quotes
You can access individual characters in a string using indexing. Python uses zero-based indexing, meaning the first character is at index 0
.
message = "Python"
print(message[0]) # Output: P
print(message[3]) # Output: h
# Concatenation (joining strings)
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name) # Output: John Doe
# Repeating a string
repeat_text = "Hello " * 3
print(repeat_text) # Output: Hello Hello Hello
# String length
print(len(full_name)) # Output: 8
int
)An integer is a whole number, meaning it has no decimal point. It can be positive, negative, or zero.
num1 = 10 # Positive integer
num2 = -5 # Negative integer
num3 = 0 # Zero
a = 15
b = 4
print(a + b) # Addition: Output: 19
print(a - b) # Subtraction: Output: 11
print(a * b) # Multiplication: Output: 60
print(a // b) # Floor division: Output: 3
print(a % b) # Modulus (remainder): Output: 3
print(a ** b) # Exponentiation: Output: 50625
x = 10
y = 20
print(x < y) # Output: True
print(x == y) # Output: False
float
)A float (floating-point number) is a number that contains a decimal point. Floats are used for representing real numbers, such as measurements and calculations requiring precision.
pi = 3.14159 # A floating-point number
temperature = -12.5 # A negative float
Floats can be used in mathematical operations just like integers.
num1 = 7.5
num2 = 2.5
print(num1 + num2) # Output: 10.0
print(num1 - num2) # Output: 5.0
print(num1 * num2) # Output: 18.75
print(num1 / num2) # Output: 3.0
a = 10.5
b = 10
print(a > b) # Output: True
print(a == b) # Output: False
You can convert an integer to a float and vice versa using float()
and int()
functions.
x = 10
y = 3.7
x_float = float(x) # Convert int to float
y_int = int(y) # Convert float to int
print(x_float) # Output: 10.0
print(y_int) # Output: 3
bool
)A boolean (bool
) represents one of two values: True
or False
. Booleans are often used in conditional statements and logical operations.
is_sunny = True
is_raining = False
Boolean values result from comparison operations.
a = 10
b = 20
print(a > b) # Output: False
print(a < b) # Output: True
print(a == 10) # Output: True
Python considers some values truthy (evaluates to True
) and some values falsy (evaluates to False
).
print(bool(0)) # Output: False
print(bool(1)) # Output: True
print(bool("")) # Output: False (empty string)
print(bool("Hello")) # Output: True (non-empty string)
print(bool([])) # Output: False (empty list)
print(bool([1, 2, 3])) # Output: True (non-empty list)
The following concepts were discussed in this section:
str
) are used to store text and are enclosed in quotes. They support operations like concatenation and repetition.int
) represent whole numbers and support basic arithmetic operations.float
) represent decimal numbers and are used in calculations requiring precision.bool
) store True
or False
values and are used in logical conditions.Understanding these data types is essential for writing effective Python programs.
In the next lesson, 2.4 Type Conversion (e.g., from String to Integer), we will explore how to convert data between different types to perform calculations and handle user input correctly.
Not a member yet? Register now
Are you a member? Login now