Variables

What It Is

A variable is a named container that stores a value. You can think of it like a labeled box — you put something inside, and later you can use the label to get it back.

Why We Use It

Variables let you:

  • Store data so you can use it later
  • Reuse values without typing them over and over
  • Update information in one place instead of many
  • Give meaning to your data with descriptive names

In sports analytics, variables might hold a player's name, their point total, a team's win percentage, or any other piece of data you want to work with.

How to Use It

Basic Syntax

variable_name = value

The = sign means "assign" — you're putting the value into the variable. Think of it like writing a label on a box and putting something inside.

Examples with Sports Data

# Storing text (strings) - use quotes
player_name = "Wilt Chamberlain"
team = "Philadelphia Warriors"
date = "March 2, 1962"

# Storing numbers - no quotes needed
points_scored = 100
field_goals = 36
free_throws = 28
games_played = 1

# You can use the variables later
print(player_name)  # Output: Wilt Chamberlain
print(points_scored)  # Output: 100

Updating Variables

Variables can change — that's why they're called "variables"! You can update them anytime by assigning a new value.

score = 0
print(score)  # Output: 0

score = 50
print(score)  # Output: 50

score = 100
print(score)  # Output: 100

Naming Rules

Python has rules for variable names:

RuleGood ExampleBad Example
Start with a letter or underscoreplayer_name1st_player
No spacestotal_pointstotal points
Only letters, numbers, underscoresstat_2024stat-2024
Case sensitiveScore and score are different variables

While these rules have no effect on the functionality of your code, it is good practice to follow them.

Naming Tips

Choose names that describe what the variable holds. Good names make your code easier to read and understand:

# Good - descriptive names
player_name = "LeBron James"
career_points = 40000
championship_rings = 4

# Bad - unclear names
x = "LeBron James"
n = 40000
thing = 4

Two Types of Values

Python treats text and numbers differently. It's important to know which one you're using!

Strings (Text)

Text values go inside quotes — single ' or double " both work:

player = "Tom Brady"
team = 'New England Patriots'
nickname = "The GOAT"

Numbers

Numbers are written without quotes:

# Integers (whole numbers)
touchdowns = 649
super_bowls = 7
age = 45

# Decimals
completion_rate = 64.2
yards_per_attempt = 7.5

Important: "100" (in quotes) is text, but 100 (no quotes) is a number. They're different! You can't do math with text that looks like a number.

text_number = "100"
real_number = 100

# This would cause an error:
# result = text_number + 50  # Can't add text and numbers!

# This works:
result = real_number + 50  # Result is 150

Common Pitfalls

ProblemWhat Went WrongFix
NameError: name 'player' is not definedVariable doesn't exist yetCreate the variable before using it
SyntaxError with variable nameUsed a space or started with a numberFollow naming rules
Number math not workingNumber is actually a string (in quotes)Remove the quotes
Wrong value showingYou might have updated it somewhere elseCheck all places the variable is used

Quick Reference

# Creating variables
player = "Shohei Ohtani"
home_runs = 44
batting_average = 0.304

# Using variables
print(player)
print(home_runs)

# Updating variables
home_runs = 45  # He hit another one!