Data Types Integers, Floats, Strings, Booleans, type(), Type Casting.

Python Basics: Data Types

Python Basics: Data Types

Learn about integers, floats, strings, booleans, and how to convert between them!

What Are Data Types?

Data types are categories for different kinds of data, like numbers, text, or true/false values. Think of them as different types of lunchboxes:

  • Integer: Holds whole numbers (e.g., 5).
  • Float: Holds decimals (e.g., 3.14).
  • String: Holds text (e.g., "Hello").
  • Boolean: Holds True or False.

1. Integers & Floats: Numbers in Python

Integers

Whole numbers without decimals.

age = 12
students = 30
print(age)  # Output: 12

Floats

Numbers with decimals.

height = 5.4  # 5 feet 4 inches
pi = 3.14
print(pi)  # Output: 3.14

Fun Experiment:

print(10 + 5.5)  # Result? It becomes a float: 15.5!

2. Strings: Text Data

Enclose text in quotes (" " or ' ').

school = "Sunrise Middle School"
emoji = "🐍"
print("I love " + school + "!")  # Output: I love Sunrise Middle School!

String Tricks:

Concatenation: Glue strings with +.

print("Python" + " is " + "fun!")  # Output: Python is fun!

Multiline Strings: Use triple quotes """ for paragraphs.

story = """
Once upon a time,
a Python loved coding.
The end!
"""
print(story)

3. Booleans: True or False

Boolean values are True or False (always uppercase!).

is_sunny = True
is_raining = False
print(is_sunny)  # Output: True

Real-World Use:

score = 95
passed = (score >= 60)  # Checks if score is 60 or higher
print(passed)  # Output: True

4. type(): What’s My Data Type?

Use type() to check the data type of a variable.

print(type(10))       # Output: 
print(type(5.5))      # Output: 
print(type("Hello"))  # Output: 
print(type(True))     # Output: 

5. Type Casting: Converting Data Types

Convert one data type to another using functions like int(), float(), str().

Why Cast Types?

To perform math on strings that look like numbers.

num_str = "10"
num_int = int(num_str)  # Convert string to integer
print(num_int * 2)      # Output: 20

Common Conversions:

Code Result Explanation
int(5.9) 5 Drops decimals (no rounding!).
float(7) 7.0 Adds decimal.
str(25) "25" Converts number to text.
bool(0) False 0 = False, other numbers = True.

⚠️ Watch Out!

# This will cause an error:
print(int("ten"))  # Can’t convert text like "ten" to a number!

Fun Activity: Mad Libs Game

Create a silly story using different data types:

animal = input("Enter an animal: ")  # Ask user for input
number = int(input("Enter a number: "))
food = input("Enter a food: ")

story = f"""
One day, a {animal} ate {number} {food}s.
It said, "{number} {food}s are too many!"
"""
print(story)

Example Output:

One day, a dinosaur ate 100 pizzas.
It said, "100 pizzas are too many!"

Troubleshooting Tips

  • TypeError: Mixing data types? Fix: Convert types! E.g., print("Score: " + str(100)).
  • ValueError: Wrong conversion (e.g., int("hi")).
  • Missing Quotes: name = Alex → Fix: name = "Alex".

Key Takeaways

  • ✅ Integers: Whole numbers.
  • ✅ Floats: Decimal numbers.
  • ✅ Strings: Text in quotes.
  • ✅ Booleans: True or False.
  • ✅ type(): Checks data type.
  • ✅ Casting: Convert with int(), str(), float().

Practice Time!

Fix the errors in this code:

score = "85"
print("Your score is: " + score + 5)

Hint: score is a string! Convert it to an integer first.

Post a Comment

Previous Post Next Post