# 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.

## 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.

• Variables are created when first assigned.

• The type of the variable is determined by Python automatically.

A Variable is data that can change.

## 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

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.

## 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!

Variable names are case sensitive.

## Assigning a value to a Variable

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

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

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

Last updated