> Source URL: /demos/cs1-path/lectures/lecture-01.slides
---
lecture_number: 1
title: Variables & Types
outcomes: [1]
date: Week 1
---

# 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

```python
age = 20
name = "Alex"
```

---

## Creating Variables

Use `=` to **assign** a value to a variable.

```python
# 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`)

```python
# 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`:

```python
# 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:

```python
# Good
student_count = 25
average_score = 87.5

# Avoid
x = 25
temp = 87.5
```

---

## Data Types

Python has several built-in types:

| Type    | Description     | Example              |
| ------- | --------------- | -------------------- |
| `int`   | Whole numbers   | `42`, `-7`, `0`      |
| `float` | Decimal numbers | `3.14`, `-0.5`       |
| `str`   | Text (strings)  | `"Hello"`, `'World'` |
| `bool`  | True/False      | `True`, `False`      |

---

## Integers (int)

Whole numbers, positive or negative:

```python
age = 20
temperature = -5
population = 1000000
```

You can do math with integers:

```python
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:

```python
price = 19.99
pi = 3.14159
temperature = 98.6
```

Division always gives a float:

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

---

## Strings (Text)

Text enclosed in quotes (single or double):

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

Combine strings with `+`:

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

---

## Booleans (True/False)

Only two possible values:

```python
is_student = True
has_graduated = False
```

Result from comparisons:

```python
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:

```python
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:

```python
# 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:

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

Print multiple values:

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

---

## The input() Function

Get input from the user:

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

Note: `input()` always returns a string.

```python
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

```python
# 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](../labs/lab-01.spec.md), you'll:

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

---

## Questions?


---

## Backlinks

The following sources link to this document:

- [Lecture 1: Variables & Types](/demos/cs1-path/index.path.llm.md)
- [Lecture 1: Variables & Types](/demos/cs1-path/labs/lab-01.spec.llm.md)
- [Lecture 1: Variables & Types](/demos/cs1-path/course.schedule.llm.md)
