T

TechIdea

Ecosystem

Pythonbeginner6 min read

Data Types

Discover the basic data types in Python: strings for text, integers and floats for numbers, and booleans for true/false checks.

Learning Goals

1
Identify core Python data types: integers, floats, strings, and booleans.
2
Convert data from one type to another using type casting functions.
3
Use type() to inspect the data type of variables dynamically.
4
Avoid type mismatch errors when combining text and numbers.

The Core Concept

In Python, every value has a data type. The most common primitive data types are Strings (text), Integers (whole numbers), Floats (decimal numbers), and Booleans (true or false values).

Understanding data types is crucial because you cannot perform the same operations on all of them. For instance, you can add two integers, but adding a string to an integer will raise a TypeError. Python provides built-in functions like type(), str(), int(), and float() to check and convert between types when necessary.

Visual guide

Python automation process

A simple original diagram to connect the lesson idea with real project flow.

Code & Implementation

python
# Different data types
price = 19.99      # Float
quantity = 3       # Integer
product = "Book"   # String
in_stock = True    # Boolean

# Checking types
print(type(price))
print(type(quantity))

# Converting types (Type Casting)
quantity_str = str(quantity)
print("Quantity as text: " + quantity_str)

Expected Output

<class 'float'>
<class 'int'>
Quantity as text: 3

Receipt Calculator with Tax

Hands-on practice task

Required for Mastery

The Challenge

Write a program that takes a subtotal integer, converts it to float, adds 8.5% tax, and prints a message showing the final price as a string.

Helpful Hints

  • Start with subtotal = 100.
  • Calculate tax: tax = subtotal * 0.085.
  • Calculate total and convert it to string when printing next to text: print("Total: " + str(total)).

Quick Knowledge Check

Why does Python distinguish between Integers and Floats?
Floats are stored differently in computer memory to represent fractional parts. While you can mix them in math operations, knowing the difference prevents unexpected decimal rounding errors.
Can I convert any string to an integer?
Only if the string contains digits (like '123'). Converting a string like 'apple' to an integer will result in a ValueError.

Continue Learning

Next steps after this lesson

Practice task

Write a program that takes a subtotal integer, converts it to float, adds 8.5% tax, and prints a message showing the final price as a string.

Ready to take action?

Supercharge your career workflows!

Discover free online utilities to format data, build job applications, and automate your productivity routine with TechIdea.

Growth Newsletter

Get practical AI tools, SEO tips, and growth guides weekly.

Join creators, students, and businesses scaling with TechIdea.