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

Code Variables != Math Variables#

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

In coding: = refers to assignment.

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)

Int#

Integers store whole numbers.
my_integer = 1
another_integer = 321
# integers can be signed
yet_another_integer = -4
type(yet_another_integer)

Float#

Floats store signed, decimal-point numbers.
my_float = 1.0
another_float = -231.45
type(another_float)
print(my_float)
print(another_float)

String#

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

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

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

print('what's your name?')

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.'
print(my_string)
# backslash to "escape" quotation mark
string_quote = "And she said, \"Please teach me Python!\""
string_quote

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

Clicker Question #5#

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

n = 1

m = n
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 = 'COGS18 is the best'
immutable_string = 'hello'
print(immutable_string)

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.