Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Module 1 - Python Fundamentals Python Python Basics Python Programming: From Zero to Real-World Applications

Python Data Types - A Complete Overview

Reviewed & accurate
AI Summary

What You Will Learn

Beginner

  • All Python data types and their categories
  • How to check and convert types
  • When to use each type
Python Data Types HierarchyPython Data TypesNumericint, float, complexSequencestr, list, tuple, rangeMappingdictSetset, frozensetBooleanTrue, FalseNoneTypeNone
Python data types organized by category

The Built-in Types

TypeExampleUse case
int42Whole numbers
float3.14Decimal numbers
str"hello"Text
boolTrue, FalseTrue/false values
list[1, 2, 3]Ordered, mutable collection
tuple(1, 2, 3)Ordered, immutable collection
dict{"k": "v"}Key-value pairs
set{1, 2, 3}Unique unordered items
NoneTypeNoneRepresents nothing/null

Checking Types

Terminalpython
x = 42
print(type(x))         # 
print(isinstance(x, int))  # True

Type Conversion

Terminalpython
# String to int
age_str = "28"
age = int(age_str)
print(age + 2)        # 30

# Int to string
num = 42
text = str(num)
print("Number: " + text)

# String to float
price = float("19.99")
print(price)           # 19.99

# Int to float (automatic)
result = 10 / 3        # 3.333 (always float in Python 3)
Type conversion errors
int("hello") raises ValueError. int("3.14") also raises ValueError (use float() first). Always wrap in try/except for user input.

Practical Exercise

Create one variable of each type: int, float, str, bool, list, dict
Print the type of each: print(type(var))
Convert: int("42"), str(100), float("3.14")
Try: int("hello") - see the error

Key Takeaways

  • 9 built-in types: int, float, str, bool, list, tuple, dict, set, None.
  • type() checks the type. isinstance() is safer for checks.
  • Convert with int(), float(), str(), bool().
  • int("hello") raises ValueError - always handle user input.
  • 10 / 3 always returns float in Python 3.
Previously: Lesson 04 covered variables.
Today: You learned: Every data type in Python explained.
Next: Lesson 06 covers numbers.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments