CodingNic

Python Foundations

Data Types

Python Foundations 20 min read

Data Types

Data Types

Every value in Python has a type. Data types help Python understand what kind of data it is working with and what operations can be performed on that data.

Why Data Types Matter

Imagine trying to add a number to a sentence or divide a name by 2. Python uses data types to prevent invalid operations and to process data correctly.

Common Python Data Types

String (str)

Used for text.

python
name = "Alex"
message = "Welcome to Python"

Integer (int)

Used for whole numbers.

python
age = 25
year = 2026

Float (float)

Used for decimal numbers.

python
price = 19.99
height = 5.8

Boolean (bool)

Used for True or False values.

python
is_logged_in = True
has_paid = False

Checking a Data Type

Use the type() function.

python
name = "Alex"
print(type(name))

Output:

text
<class 'str'>

Type Conversion

Sometimes you need to convert one type into another.

python
age = "25"
age = int(age)
print(age + 5)

Output:

text
30

Common Conversion Functions

  • str() → convert to string
  • int() → convert to integer
  • float() → convert to float
  • bool() → convert to boolean

Code Along

python
name = "Maya"
age = 20
height = 1.65
student = True

print(type(name))
print(type(age))
print(type(height))
print(type(student))

Mini Challenge

Create four variables:

  • Your name
  • Your age
  • Your height
  • Whether you like Python

Print each value and its type.

Real World Use Case

An online store uses strings for product names, integers for stock quantity, floats for prices, and booleans for availability.

Quiz

  1. Which type is used for text?
  2. What type is 3.14?
  3. What are the two boolean values?
  4. What function checks a value’s type?

Assignment

Create a script with at least six variables using different data types. Print each variable and its type.

Summary

You learned what data types are, why they matter, common Python types, how to check types, and how to convert between them.