close
close
python data types cheat sheet

python data types cheat sheet

2 min read 19-10-2024
python data types cheat sheet

Python Data Types: A Cheat Sheet for Beginners

Python's data types are the fundamental building blocks of any program. Understanding them is crucial for efficient and effective programming. This cheat sheet provides a concise overview of key data types, their characteristics, and practical examples to help you get started.

1. Numbers

  • Integers (int): Whole numbers without decimal points.
    • Example: age = 25
  • Floats (float): Numbers with decimal points.
    • Example: price = 19.99
  • Complex Numbers (complex): Numbers with real and imaginary parts.
    • Example: z = 3 + 4j

2. Strings (str)

  • Strings: Sequences of characters enclosed in single (' ') or double (" ") quotes.
    • Example: name = "Alice"
    • Important: Strings are immutable, meaning you cannot directly change individual characters within a string. You need to create a new string with the desired changes.

3. Booleans (bool)

  • Booleans: Represent truth values, either True or False.
    • Example: is_active = True

4. Lists (list)

  • Lists: Ordered sequences of elements enclosed in square brackets [].
    • Example: colors = ["red", "green", "blue"]
    • Key Features: Mutable, meaning you can modify elements in place. Allow duplicate elements.

5. Tuples (tuple)

  • Tuples: Ordered sequences of elements enclosed in parentheses ().
    • Example: coordinates = (10, 20)
    • Key Features: Immutable.

6. Sets (set)

  • Sets: Unordered collections of unique elements enclosed in curly braces {}.
    • Example: fruits = {"apple", "banana", "cherry"}
    • Key Features: Mutable. Do not allow duplicate elements.

7. Dictionaries (dict)

  • Dictionaries: Unordered collections of key-value pairs enclosed in curly braces {}.
    • Example: user = {"name": "Bob", "age": 30}
    • Key Features: Mutable. Keys must be unique and immutable.

Practical Examples:

Let's see how these data types come together in a simple program:

# Defining variables
name = "John Doe"  # string
age = 35  # integer
is_student = False  # boolean
favorite_colors = ["blue", "green"]  # list
coordinates = (12.5, -74.0)  # tuple
student_info = {"name": name, "age": age, "courses": ["Python", "Math"]}  # dictionary

# Using the variables
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Is student: {is_student}")
print(f"Favorite colors: {favorite_colors}")
print(f"Coordinates: {coordinates}")
print(f"Student info: {student_info}") 

Understanding Data Type Conversions

You may encounter situations where you need to convert data from one type to another. Python provides functions for this purpose:

  • int(): Convert to integer.
  • float(): Convert to floating-point number.
  • str(): Convert to string.
  • list(): Convert to list.
  • tuple(): Convert to tuple.
  • set(): Convert to set.
  • dict(): Convert to dictionary.

Example:

age_str = "25"
age_int = int(age_str)
print(f"Age as integer: {age_int}")

This cheat sheet provides a basic introduction to Python data types. As you explore Python further, you'll encounter more specialized types like bytes, bytearray, and others. Don't hesitate to experiment and practice to solidify your understanding!

Related Posts


Latest Posts