Variables and Data Types

Variables hold information so your code can remember names, numbers, flags, and more. Mastering core types (text, numbers, booleans) is the fastest way to make small, repeatable automations that save real time at work.

Core Types at a Glance

  • str → text (names, IDs, file paths)
  • int → whole numbers (counts, versions)
  • float → decimals (prices, rates)
  • boolTrue/False (flags, toggles)
  • None → "no value yet" placeholder

Tip: Use f-strings to combine variables into readable messages.

Common Pitfalls

  • Numbers in strings → convert with int() / float()
  • True/False are capitalized in Python
  • Division / returns float; use // for whole number division
  • Don't compare to None with ==; use is/is not

Rule: Name variables clearly; avoid single letters for anything important.

Creating Variables

Variables are created by assignment. Python infers the type from the value.

python
# Creating variables
name = "Alice"     # str
age = 25           # int
height_m = 1.68    # float
is_student = True  # bool
nothing_yet = None # special "no value"

Basic Data Types

Strings (str)

Use for any text. Triple quotes allow multi-line text.

python
first_name = "John"
last_name = 'Doe'
message = """This is a
multi-line string"""

Numbers

int for counts/IDs, float for prices/rates.

python
# Integers
count = 42
negative = -10

# Floats
price = 19.99
temperature = -5.5

# Division differences
print(7 / 2)   # 3.5 (float)
print(7 // 2)  # 3   (floor division, int)

Booleans (bool)

Use for yes/no flags and feature toggles.

python
is_active = True
is_complete = False

if is_active and not is_complete:
    print("In progress!")

Type Checking

Use type() to inspect what you have.

python
name = "Python"
print(type(name))  # <class 'str'>

age = 25
print(type(age))   # <class 'int'>

Type Conversion

Convert strings to numbers (and back) when parsing inputs or filenames.

python
# String to integer
age_str = "25"
age_int = int(age_str)

# Integer to string
number = 42
number_str = str(number)

# String to float
price_str = "19.99"
price_float = float(price_str)

Variable Naming Rules

  • Start with a letter or underscore; then letters, numbers, underscores
  • Case-sensitive (ageAge)
  • Don't use Python keywords (class, for, etc.)
python
# Good names
user_name = "Alice"
total_count = 100
_private_var = "hidden"

# Bad names (avoid)
# 2name = "Invalid"   # starts with number
# class = "Invalid"   # Python keyword

Formatting with f-strings

f-strings make readable labels, filenames, and messages.

python
first = "Ana"
sales = 12345.678
print(f"Rep: {first} | Sales: ${sales:,.2f}")  # Rep: Ana | Sales: $12,345.68

Cornerstone Project — Daily Task Calculator

Build a simple calculator that helps you track your daily work progress. This practical tool uses variables to store your goals and calculate completion percentages—perfect for staying motivated!

Your Daily Goals

Set up variables for things you want to track each day:

python
# Your daily targets
target_emails = 20
target_calls = 5
target_tasks = 8

# What you've completed so far
completed_emails = 15
completed_calls = 3
completed_tasks = 6

Calculate Your Progress

Use simple math to see how you're doing:

python
# Calculate percentages
email_progress = (completed_emails / target_emails) * 100
call_progress = (completed_calls / target_calls) * 100
task_progress = (completed_tasks / target_tasks) * 100

# Overall progress
total_completed = completed_emails + completed_calls + completed_tasks
total_target = target_emails + target_calls + target_tasks
overall_progress = (total_completed / total_target) * 100

Create a Nice Progress Report

Use f-strings to make a readable summary:

python
print("📊 Daily Progress Report")
print(f"Emails: {completed_emails}/{target_emails} ({email_progress:.1f}%)")
print(f"Calls: {completed_calls}/{target_calls} ({call_progress:.1f}%)")
print(f"Tasks: {completed_tasks}/{target_tasks} ({task_progress:.1f}%)")
print(f"
Overall: {overall_progress:.1f}% complete")

# Motivational message
if overall_progress >= 80:
    print("🎉 Great job! You're almost done!")
elif overall_progress >= 50:
    print("👍 Good progress! Keep it up!")
else:
    print("💪 You've got this! Let's keep going!")

Practice Exercise

Create your own progress tracker for something you do regularly:

python
# Try tracking your own daily activity
# Examples: books read, workouts, study hours, etc.

activity_name = "Pages Read"
target_amount = 50
completed_amount = 32

# Calculate and display
progress = (completed_amount / target_amount) * 100
remaining = target_amount - completed_amount

print(f"📖 {activity_name} Progress")
print(f"Completed: {completed_amount}/{target_amount}")
print(f"Progress: {progress:.1f}%")
print(f"Remaining: {remaining}")