Variables#

  • Definition

  • Namespace

  • Code Style

  • Variable types

Vocab#

  • Variable: The name given to something in programming that stores some information

  • Definition/Assignment/Declaration: To create a variable using the assignment operator =

Programming With Python#

Programming: a way to ask computer to store values (variables), and do things with them (operations).`

# This is a comment. You can write a comment by using a `#`
my_variable = 12 

my_other_variable = 13 # Comments can be 'inline', like this one

Defining Variables#

In programming, variables are things that store values. Variables are defined with name = value.
my_var = 1  # `my_var` is a variable

# This defines another variable
other_var = 'variables are cool'
# once you create a variable it's stored in your namespace
other_var

Variable Analogy: A Party Cup#

A cup has a general purpose.

It’s good for some things (storing things, particularly liquids) and not for others.

If we were to compare this to programming…imagine a cup at a party:

  • the party cup would be the variable type

  • It would be designed to store liquids

  • When defined, we would specify its contents and write our name on the cup

Things we could do with this variable:

  • pour more liquid into the same cup

  • pour another cup with the same liquid

  • pour another cup with a different liquid

  • pour liquid between cups (changing both’s “values”)

Regardless, this type of variable (party cup) has a purpose (store liquids). The specific liquids (values) differ between cups. But the operations are all specific to the fact that it is a cup.

Not all equal signs are created equal#

In mathematics: = refers to equality (as a statement of truth).

In coding: = refers to assignment. == refers to equality

In your head, when you are reading code like

x=3

you should say it to yourself as:

“we assign the value 3 to the variable x”

Math: What is x?

\(y = 10x + 2\)

Code: What is x?

x = x + 1

Clicker Question #1#

After executing the following code, what will be the value of my_var?

my_var = 2 

my_var = my_var + 1

print(my_var)
  • A) 2

  • B) 3

  • C) “my_var + 1”

  • D) This code will fail

Clicker Question #2#

After executing the following code, what will be the value of diff_var?

diff_var = my_variabel - my_var

print(diff_var)
  • A) 4

  • B) 9

  • C) “my_variable - my_var”

  • D) This code will fail

Assignment Notes#

  • In programming = means assignment

  • There can be more than one assignment in a single line

  • Anything to the right of the = is evaluated before assignment

    • This process proceeds from right to left

Declaring Variables Cheat Sheet#

  • Names are always on the left of the =, values are always on the right

  • Names are case sensitive

  • Variables must start with letters (or underscores)

    • After that, they can include numbers

    • They cannot include special characters (like &, *, #, etc)

  • Python doesn’t care what you name your variables

    • Humans do care. Pick names that describe the data / value that they store

Reserved Words#

There are 33 words that are not allowed to be used for variable assignment in Python 3.6.

False None True and as assert break
class continue def del elif else except
finally for from global if import in
is lambda nonlocal not or pass raise
return try while with yield
# you will get an error if you try to assign a variable to one of these words
try = 6

Kernels#

The kernel is the thing that executes your code. It is what connects the notebook (as you see it) with the part of your computer that runs code.

Your kernel also stores your namespace - all the variables and code that you have declared (executed).

It can be useful to clear and re-launch the kernel. You can do this from the ‘kernel’ drop down menu, at the top, optionally also clearing all ouputs. Note that this will erase any variables that are stored in memory.

Namespace#

The namespace is the 'place' where all your currently defined code is declared - all the things you have stored in active memory.
whos?
# You can list everything declared in the namespace with '%whos'
%whos

Code Style#

Code style considerations do NOT affect the functionality of your code, but DO affect readability.

Get in good habits now!

For variable assignment:

  • we use a single space around assignment operator =

  • we use snake_case for variable names (All lowercase, underscores between words)

  • use informative variable names…something that tells you a bit about what’s being stored

  • Ideal: my_variable = 6

  • Avoid: MyVariable=6

Variable Types#

Every variable has a type, which refers to the kind of variable that it is, and how the computer stores that data.
# Declare a variable
variable_name = 1

# You can always ask Python 'what type is this variable' using:
type(variable_name)

Numbers#

Integers store whole numbers. Floats store signed, decimal-point numbers
my_integer = 1
my_float = 2.5
# integers and floats can be signed
another_int = -4
another_float = -2.0
# checking the type of a variable
type(another_float)
float

String#

Strings store characters, as text.
my_string = 'words, words, words'

# Note that strings can be defined with either '' or ""
and_another = "and some more"
print(my_string)
type(my_string)

Quotation Marks#

About those quotation marks…

my_string = 'This is a single-quoted string.'
my_string
my_string = "This is a double-quoted string."
my_string

Note that Python will put single quotes around it, even if you specify double quotes.

A general principle is to pick something and be consistent. In this course, I’ll do my best to only use single quotes.

Aside: What if you want to print a quotation mark?#

  • use double quotes outside with apostraphe inside quotes

  • use an escape \ (backslash) before character

# double quotes on outside; single quote inside
my_string = "i wan't to see a quote."
my_string
# backslash to "escape" quotation mark
string_quote = "And she said, \"Please teach me Python!\""
string_quote

Clicker Question #3#

After executing the following code, what will the type of var_a be?

var_a = -17.5
  • A) String

  • B) Int

  • C) Float

  • D) Boolean

  • E) None

Clicker Question #4#

After executing the following code, what will the type of var_b be?

var_b = '-17.5'
  • A) String

  • B) Int

  • C) Float

  • D) Boolean

  • E) None

Boolean#

Booleans store True or False.
my_bool = True
another_bool = False
type(another_bool)

None#

None is a special type that stores None, used to denote a null or empty value.
the_concept_of_nothing = None
type(the_concept_of_nothing)

Clicker Question #5#

After executing the following code, what will the type of the variable m be?

n = 1
a = 'm'
m = n
  • A) String

  • B) Int

  • C) Float

  • D) Boolean

  • E) None

Clicker Question #6#

After executing the following code, what will the type of the variable m be?

m = 1
a = m
m = 'a'
type(m)
  • A) String

  • B) Int

  • C) Float

  • D) Boolean

  • E) None

Mutable vs Immutable#

The variable types we’ve talked about today are all immutable. This means they cannot be altered after they’re created.

immutable_string = 'COGS18 is the best!'
immutable_string[4]
# cannot change part of the string after creation
immutable_string[4] = '0'

Python does have mutable types. We’ll talk about these later in the course, and these are where aliasing shines!

Indentation#

Just a brief word on indentation.

Python does care about whitespace.

You will get an error if Python runs into unanticipated whitespace.

a = 1
    b = a
    
    print(b) 

There are times when indentation will be required and expected. We’ll discuss these in future lectures.