# Variables

## Introduction

In Math class, you learned that a variable is a symbol for a number that you don't know yet.  In Computer Science, a variable is information that is stored in a program.

{% embed url="<https://docs.google.com/presentation/d/12qaq8XfcTtMK1USXR9vO-hpnmCZlphi-OHMerYIP0Dg/edit?usp=sharing>" %}

## Variables in Python

* A variable name refers to a particular object.
* A variable is a location in memory where a programmer can store a value. &#x20;
* Variables are created when first assigned. &#x20;
* The type of the variable is determined by Python automatically.

{% hint style="info" %}
A Variable is data that can change.&#x20;
{% endhint %}

## Python Data Types

| Data Type | Explanation                                                                                                     | Examples             |
| --------- | --------------------------------------------------------------------------------------------------------------- | -------------------- |
| int       | positive or negative whole numbers                                                                              | 1, 2, 3, 4, 5        |
| float     | floating point numbers (numbers with a decimal)                                                                 | 1.1, 2.2, 3.3        |
| string    | a set of characters (letters, numbers, and/or special characters) enclosed by single or double quotation marks. | "hamster", 'gerbils' |
| boolean   | Boolean values are either True or False                                                                         | True, False          |

{% hint style="warning" %}
&#x20;Variable names can be any length.  However, stick to variable names that are short, sweet and to the point.  Good variable names in Python will make your code easy to read and understand.
{% endhint %}

## Variable Name Rules

| Rule                                                                     | Examples                                                                                                                                                                                                        |
| ------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Must begin with a letter or underscore.                                  | x, y, points, robux, money, friends                                                                                                                                                                             |
| Other characters can be letters, numbers or underscore.                  | x2, roblox\_player, jail\_break                                                                                                                                                                                 |
| Are case sensitive                                                       | **student** is different than **Student**                                                                                                                                                                       |
| Python has keyword/reserved words that you cannot use as variable names. | False, class, finally, is, return, None, continue for, lambda, try, True, def, from, nonlocal, while, and, del global, not, with, as, elif, if, or, yield, assert, else, import, pass, break, except, in, raise |
| Can be any length greater than or equal to 1 but...                      | you’re the one who has to remember what it is!                                                                                                                                                                  |

{% hint style="danger" %}
Variable names are case sensitive.
{% endhint %}

## Assigning a value to a Variable

To assign a value to a variable, use the = sign.

```python
x = 3
name = "Sam"
robux = 100.50
```

## Variable Roles

Variables have typical uses that are called variable roles.  Variable roles are not the same as types of variables (integer, float, string, boolean, etc.) since the concept of roles is a classification telling what kind of a task a variable has in a certain program.

{% embed url="<https://docs.google.com/presentation/d/1VjP4oyFKJfGDoww18OaUF1Fz_m3-h7Ijj6Jh9iD8vf4/edit?usp=sharing>" %}

| Role         | Pattern                                                 | Example                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ------------ | ------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Constant     | Assigned once                                           | <p>Assigned at the beginning of a program or at the head of a code block.  By convention, make this variable name in all caps. </p><p></p><p>Example:</p><p><strong>RATE</strong> = 7</p>                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| Most recent  | Fluctuating                                             | <p>Assigned user input.</p><p></p><p>Example:</p><p><strong>name</strong> = input(“What’s your name? “)</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| Accumulator  | Running total                                           | <p>1. Initialize</p><p>2. Accumulate</p><p>3. Print<br></p><p>Example:</p><p><strong>sum</strong> = 0 # initialize to 0.</p><p>for x in range(3):</p><p>   num = int(input("Give me a number: "))</p><p>   <strong>sum</strong> = <strong>sum</strong> + num</p><p>print(<strong>sum</strong>)</p>                                                                                                                                                                                                                                                                                                                          |
| Best-so-far  | Record holder                                           | <p>Keep track of the record holder while iterating.<br></p><p>Pattern:</p><p>1. Initialize to worst-possible value before the loop or first value in a sequence of values.</p><p>2. During iteration, compare to best-so-far and assign to the new value, if needed.</p><p>3. After the loop, best-so-far will be the best-so-far.<br></p><p>Example:</p><p>x = 0</p><p>while x < 3:</p><p>     num = int(input("Give me a number: "))</p><p>     if x == 0:</p><p>         <strong>max</strong> = num</p><p>     else:</p><p>         if num > max:</p><p>            <strong>max</strong> = num</p><p>     x = x + 1 </p> |
| One-Way Flag | <p>Variable will be reset once condition is met<br></p> | <p>Pattern:</p><p>1. Initialize the flag so the condition has not been met.</p><p>2. With each iteration, check for the condition and change the flag if true.<br></p><p>Example:</p><p><strong>done</strong> = False</p><p>while not done:</p><p>     <strong>done</strong> = True</p>                                                                                                                                                                                                                                                                                                                                     |
| Stepper      | Step through a pre-determined sequence of values        | <p>Iterates a specific number of times.</p><p></p><p>Example:</p><p><strong>x</strong> = 0</p><p>while <strong>x</strong> < 5:</p><p>    print(<strong>x</strong>)</p><p>    x = x + 1</p><p></p><p>for x in range(5):</p><p>    print(x)</p>                                                                                                                                                                                                                                                                                                                                                                               |
| Walker       | Elements of iterator                                    | <p>Elements of a list during iteration.<br></p><p>Example:</p><p>for <strong>x</strong> in friends:</p><p>   print(<strong>x</strong>)<br></p><p>Elements of a dictionary during iteration.<br></p><p>Example:</p><p>for <strong>key</strong> in d:</p><p>    print(key, d\[<strong>key</strong>])</p>                                                                                                                                                                                                                                                                                                                      |
| Temporary    | Needed only for a short period of time.                 | <p>The tax is computed into the temporary tax </p><p></p><p>Example:</p><p>income = float(input("Income: "))</p><p>TAXRATE = .25</p><p><strong>tax</strong> = TAXRATE \* income</p>                                                                                                                                                                                                                                                                                                                                                                                                                                         |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.pythonclassroom.com/python-variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
