Data Types in Python

Week 1 — Python Basics Revision

Introduction

Every value in Python has a type. The type determines what you can do with that value — you can add numbers but not divide text. Understanding data types helps you write correct, bug-free code.

Core Data Types

Type Name Example Use Case
intInteger42Age, count, year
floatDecimal3.14Price, GPA, temperature
strString"Hello"Names, messages, addresses
boolBooleanTrueYes/no flags, status
listList[1, 2, 3]Collections of items
dictDictionary{"key": "val"}Key-value data

Integers and Floats

Integers are whole numbers without decimals. Floats are numbers with a decimal point. Python handles math between them naturally.

age = 25          # int
price = 19.99     # float
year = 2026       # int

print(type(age))    # <class 'int'>
print(type(price))  # <class 'float'>

# Math works naturally
total = price * 3
print(total)        # 59.97
Output
<class 'int'>
<class 'float'>
59.97

Strings

Strings are sequences of characters enclosed in quotes. You can use single or double quotes. They support concatenation with + and length checking with len().

first_name = "Alice"
last_name = 'Smith'
full_name = first_name + " " + last_name
print(full_name)    # Alice Smith
print(len(full_name))  # 11
Output
Alice Smith
11

Booleans

Booleans represent one of two values: True or False. They are commonly the result of comparisons.

is_active = True
is_deleted = False
print(type(is_active))  # <class 'bool'>

# Result of comparisons
print(10 > 5)    # True
print(3 == 7)    # False
Output
<class 'bool'>
True
False

Lists and Dictionaries

Lists store ordered collections of items. Dictionaries store data as key-value pairs. Both are used heavily in Python programs.

grades = [85, 92, 78, 95]
print(grades[0])   # 85
print(len(grades))  # 4

student = {"name": "Alice", "age": 22}
print(student["name"])  # Alice
Output
85
4
Alice

Checking Types

The type() function tells you what type a value is. This is useful for debugging and understanding your data.

print(type(42))       # <class 'int'>
print(type("hello"))  # <class 'str'>
print(type(True))     # <class 'bool'>
Output
<class 'int'>
<class 'str'>
<class 'bool'>

Type Conversion

You can convert between types using built-in functions. This is called type casting.

# String to integer
age_text = "25"
age = int(age_text)

# Integer to float
whole = 10
decimal = float(whole)  # 10.0

# Number to string
price = 19.99
text = str(price)  # "19.99"

How This Connects to OOP

When you create a class, its attributes have types. A BankAccount class might store balance as a float and owner as a string. Knowing types helps you design better classes.

Best Practices

  • Use type() when unsure about a value
  • Choose the right type: money = float, count = int
  • Be careful mixing types in operations

Common Mistakes

  • Adding string + number: "Age: " + 25 causes TypeError (use f-string instead)
  • Confusing int("abc") — crashes if text is not a number
  • Assuming "5" and 5 are the same (they are not)

Practice Exercises

  1. Create one variable of each type (int, float, str, bool). Print each with type().
  2. Convert the string "100" to an integer, add 50, and print the result.
  3. Create a list of 3 fruits and a dictionary with your name and age. Print both.

Summary

  • Every value in Python has a type: int, float, str, bool, list, dict.
  • Use type() to check and int(), float(), str() to convert between types.
  • Choosing the correct type prevents bugs and makes your code clearer.

Next: now that you know what types of data Python can work with, let us learn how to get input from users and display output.