Lecture 1: Variables & Types

Overview

Today we'll learn how to store and work with data in Python.

What is a Variable?

A variable is a named container that stores a value.

Think of it like a labeled box:

  • The label is the variable name
  • The contents are the value
age = 20
name = "Alex"

Creating Variables

Use = to assign a value to a variable.

# Storing a number
credits = 15

# Storing text (a "string")
major = "Biology"

# Storing a decimal
gpa = 3.75

The variable name goes on the left, the value on the right.

Naming Rules

Variable names must:

  • Start with a letter or underscore
  • Contain only letters, numbers, underscores
  • Not be a Python keyword (like if, for, print)
# Good names
student_name = "Jordan"
age2 = 21
_private = "secret"

# Bad names (these will error!)
2nd_place = "silver"    # Can't start with number
my-name = "Alex"        # No hyphens allowed
for = 5                 # 'for' is reserved

Naming Conventions

By convention, Python uses snake_case:

# Good (snake_case)
first_name = "Alex"
total_credits = 45

# Works but not preferred
firstName = "Alex"      # camelCase
FIRSTNAME = "Alex"      # SCREAMING_CASE (used for constants)

Choose descriptive names:

# Good
student_count = 25
average_score = 87.5

# Avoid
x = 25
temp = 87.5

Data Types

Python has several built-in types:

TypeDescriptionExample
intWhole numbers42, -7, 0
floatDecimal numbers3.14, -0.5
strText (strings)"Hello", 'World'
boolTrue/FalseTrue, False

Integers (int)

Whole numbers, positive or negative:

age = 20
temperature = -5
population = 1000000

You can do math with integers:

sum = 5 + 3      # 8
diff = 10 - 4    # 6
product = 6 * 7  # 42
quotient = 15 // 3  # 5 (integer division)

Floats (Decimal Numbers)

Numbers with decimal points:

price = 19.99
pi = 3.14159
temperature = 98.6

Division always gives a float:

result = 7 / 2   # 3.5
result = 6 / 2   # 3.0 (still a float!)

Strings (Text)

Text enclosed in quotes (single or double):

name = "Alice"
greeting = 'Hello, World!'
message = "It's a beautiful day"

Combine strings with +:

first = "Hello"
second = "World"
combined = first + " " + second  # "Hello World"

Booleans (True/False)

Only two possible values:

is_student = True
has_graduated = False

Result from comparisons:

5 > 3      # True
10 == 10   # True
"a" == "b" # False

We'll use these a lot in the next lecture!

Checking Types

Use type() to see a value's type:

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

Type Conversion

Convert between types:

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

# Integer to string
count = 42
count_str = str(count)  # "42"

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

Note: This is important for input()—it always returns a string.

The print() Function

Display output to the screen:

print("Hello!")
print(42)
print(3.14)

Print multiple values:

name = "Alex"
age = 20
print("Name:", name)
print(name, "is", age, "years old")

The input() Function

Get input from the user:

name = input("What is your name? ")
print("Hello,", name)

Note: input() always returns a string.

age = input("How old are you? ")
# age is "25" (string), not 25 (int)

# Convert if you need a number:
age = int(input("How old are you? "))

Putting It Together

# Get user information
name = input("Enter your name: ")
birth_year = int(input("Enter your birth year: "))

# Calculate age
current_year = 2025
age = current_year - birth_year

# Display result
print("Hello,", name)
print("You are approximately", age, "years old")

Key Takeaways

  1. Variables store values with meaningful names
  2. Types include int, float, str, bool
  3. Use snake_case for variable names
  4. input() returns strings—convert with int() or float()
  5. print() displays output

Lab Preview

In Lab 1, you'll:

  • Create your first Python program
  • Practice with variables and types
  • Use input() and print()
  • Do basic calculations