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
x = 10

# Floats
y = 3.14

# Complex Numbers
z = 3 + 4j

You can perform basic arithmetic using operators like +, -, *, /, and more:

python
Copy code
a = 5 + 3  # Addition
b = 7 - 2  # Subtraction
c = 3 * 4  # Multiplication
d = 10 / 2  # Division
e = 10 // 3  # Floor Division
f = 10 % 3  # Modulus

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
greeting = "Hello, Python!"
name = 'Alice'

# Multi-line strings
message = """This is a
multi-line string."""

# String concatenation
full_message = greeting + " " + name

Strings come with powerful built-in methods for manipulation:

python
Copy code
word = "Python"
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
is_python_fun = True
is_snowing = False

# 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
x = 10
name = "Alice"
is_active = True

You can reassign variables to hold different types of data:

python
Copy code
x = 10  # Integer
x = "Now a string!"  # String

Multiple Assignments

python
Copy code
a, b, c = 1, 2, 3  # Assign multiple variables at once
x = y = z = 0  # Assign the same value to multiple variables

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
age = 25  # Integer
age = "Twenty-five"  # Now a string

4. Best Practices for Variables

  • Use descriptive names:

    python
    Copy code
    temperature = 23.5
    username = "john_doe"
  • Follow Python naming conventions:

    • Use snake_case for variable names.
    • Avoid starting with numbers or using reserved keywords.

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.