6  Python Data Types: Strings and Numeric Types

Today, we’re investigating the magical realm of Python data types. We’ll explore the difference between strings and numeric types, and learn about some powerful spells (functions) that can transform these types. By the end of this lesson, you’ll be able to identify and convert between different data types like a true Python sorcerer!

6.1 The Two Realms: Strings and Numbers

In the world of Python, data exists in different forms, much like how in a fantasy world, you might encounter humans, elves, and dwarves. Two of the most common types of data are strings and numeric types.

6.2 Strings: The Realm of Text

Strings are sequences of characters, like words or sentences. They’re always enclosed in quotes (single or double).

Examples of strings:

"Hello, World!"
'Python is awesome'
"42"  # Yes, this is a string, not a number!

6.3 Numeric Types: The Realm of Numbers

Python has two main types of numbers:

  1. Integers (int): Whole numbers, positive or negative.
  2. Floating-point numbers (float): Numbers with decimal points.

Examples of numeric types:

42  # This is an integer
-10  # This is also an integer
3.14  # This is a float

6.4 The type() Function: Identifying the Species

In our magical Python world, type() is like a spell that reveals the true nature of any data. Let’s use it to identify some different types:

print(type("Hello"))  # Output: <class 'str'>
print(type(42))       # Output: <class 'int'>
print(type(3.14))     # Output: <class 'float'>

6.5 Transformation Spells: Converting Between Types

Sometimes, we need to convert data from one type to another. Python provides magical functions for these transformations:

6.6 str(): Turning Anything into a String

The str() function can turn numbers (and many other things) into strings:

number = 42
string_number = str(number)
print(type(string_number))  # Output: <class 'str'>
print("The answer is " + string_number)  # Now we can concatenate!

6.7 int(): Converting to Integers

The int() function converts strings or floats to integers:

string_number = "42"
integer = int(string_number)
print(type(integer))  # Output: <class 'int'>
print(integer + 8)    # Now we can do math! Output: 50

Be careful! int() will remove any decimal part from a float:

print(int(3.99))  # Output: 3

6.8 float(): Converting to Floating-Point Numbers

The float() function converts strings or integers to floating-point numbers:

integer = 42
float_number = float(integer)
print(type(float_number))  # Output: <class 'float'>
print(float_number)        # Output: 42.0

string_float = "3.14"
pi = float(string_float)
print(type(pi))  # Output: <class 'float'>
print(pi)        # Output: 3.14

6.9 Practical Magic: Using These Powers

Let’s see how we can use these transformation spells in a real scenario. Imagine we’re creating a spell points calculator for a game:

# The player's current spell points (as a string)
spell_points_str = "100"

# The cost of casting a fireball (as an integer)
fireball_cost = 30

# Convert spell points to an integer
spell_points = int(spell_points_str)

# Cast the spell!
remaining_points = spell_points - fireball_cost

# Convert the result back to a string for display
result = "Remaining spell points: " + str(remaining_points)

print(result)  # Output: Remaining spell points: 70

6.10 Practice Your Magic

Now it’s your turn to practice these transformation spells:

  1. Create a string containing a number (like “3.14”) and convert it to a float.
  2. Take an integer (like 42) and convert it to a string.
  3. Use type() to check the type of each result.
  4. Try to add a number to a string (like “Age: " + 25). What happens? How can you fix it?

6.11 Common Bugs to Watch Out For

As you experiment with these magical type conversions, be wary of these common pitfalls:

  1. TypeError: This occurs when you try to combine incompatible types, like adding a string to an integer.

    print("Age: " + 25)  # TypeError: can only concatenate str (not "int") to str
  2. ValueError: This happens when you try to convert a string to a number, but the string doesn’t represent a valid number.

    int("Hello")  # ValueError: invalid literal for int() with base 10: 'Hello'
  3. Losing Precision: When converting from float to int, you lose the decimal part.

    print(int(3.99))  # Output: 3
  4. Forgetting to Convert: Remember to convert strings to numbers before doing math operations.

    "5" * 3  # This repeats the string, doesn't multiply! Output: '555'

6.12 Conclusion

You’ve learned to distinguish between strings and numeric types, and you’ve mastered the arts of type identification and conversion. These skills will serve you well on your coding quests.

Remember, the power to convert between types is great, but with great power comes great responsibility. Always be mindful of what type of data you’re working with, and use your conversion spells wisely!

6.13 Further Resources

To deepen your understanding of Python data types, check out these magical tomes:

  1. Python’s Official Documentation on Built-in Types
  2. Real Python’s Guide to Python Data Types
  3. Codecademy’s Learn Python 3 course (Data Types section)

Keep practicing, and soon you’ll be casting these Python spells in your sleep!