Data Types
Core Data Types and Variables
Introduction
Python provides a variety of built-in data types that allow you to store and manipulate data in different ways. In this post, we’ll explore the core data types—numbers, strings, and booleans—and understand how to use variables effectively. By the end, you’ll have a solid grasp of Python’s dynamic typing and how it simplifies programming.
1. Python’s Core Data Types
Numbers
Python supports several types of numbers:
- Integers: Whole numbers, e.g.,
10
,5
- Floats: Decimal numbers, e.g.,
3.14
,0.5
- Complex Numbers: Numbers with real and imaginary parts, e.g.,
3 + 4j
Examples:
python
Copy code# Integers
= 10
x
# Floats
= 3.14
y
# Complex Numbers
= 3 + 4j z
You can perform basic arithmetic using operators like +
, -
, *
, /
, and more:
python
Copy code= 5 + 3 # Addition
a = 7 - 2 # Subtraction
b = 3 * 4 # Multiplication
c = 10 / 2 # Division
d = 10 // 3 # Floor Division
e = 10 % 3 # Modulus f
Strings
A string is a sequence of characters enclosed in quotes ('
or "
). Strings are widely used for handling text.
Examples:
python
Copy code# Single-line strings
= "Hello, Python!"
greeting = 'Alice'
name
# Multi-line strings
= """This is a
message multi-line string."""
# String concatenation
= greeting + " " + name full_message
Strings come with powerful built-in methods for manipulation:
python
Copy code= "Python"
word print(word.upper()) # Converts to uppercase
print(word.lower()) # Converts to lowercase
print(word[0]) # Accessing characters by index
print(word[-1]) # Accessing the last character
Booleans
Booleans represent truth values: True
or False
. They are essential for decision-making in your programs.
Examples:
python
Copy code= True
is_python_fun = False
is_snowing
# Boolean expressions
print(5 > 3) # True
print(10 == 5) # False
Booleans are often used with comparison operators:
==
(equal to)!=
(not equal to)<
,>
,<=
,>=
(comparison)
2. Variables: Storing Data
Variables act as containers for storing data. In Python, you don’t need to declare their type explicitly—they’re dynamically typed.
Variable Assignment
python
Copy code= 10
x = "Alice"
name = True is_active
You can reassign variables to hold different types of data:
python
Copy code= 10 # Integer
x = "Now a string!" # String x
Multiple Assignments
python
Copy code= 1, 2, 3 # Assign multiple variables at once
a, b, c = y = z = 0 # Assign the same value to multiple variables x
3. Dynamic Typing
Python determines the type of a variable at runtime based on the assigned value. This makes Python highly flexible but requires careful coding to avoid type errors.
Example:
python
Copy code= 25 # Integer
age = "Twenty-five" # Now a string age
4. Best Practices for Variables
Use descriptive names:
python Copy code= 23.5 temperature = "john_doe" username
Follow Python naming conventions:
- Use
snake_case
for variable names. - Avoid starting with numbers or using reserved keywords.
- Use
Conclusion
You’ve now mastered Python’s core data types and variables! Understanding these basics is essential for working with more advanced data structures and building complex programs.