Variables#
Review
Code Style
Variables Activity
Declaring Variables Cheat Sheet#
Names are always on the left of the
=
, values are always on the rightNames 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
Python is dynamically typed - variable type is determined on creation; every variable has a type
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
a = 6
Cell In[1], line 2
try = 6
^
SyntaxError: expected ':'
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
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
Activity time: Variables#
Complete all questions in this Google Form (https://forms.gle/YbYagsGaHyjSe2Ta7) and then click submit.
You are encouraged to:
Talk with your neighbor
Try the questions out in your notebok to check your understanding
If you have questions, I’m happy to come over as we work on this!
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]
'1'
# cannot change part of the string after creation
immutable_string[4] = '0'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[9], line 2
1 # cannot change part of the string after creation
----> 2 immutable_string[4] = '0'
TypeError: 'str' object does not support item assignment
Python does have mutable types. We’ll talk about these soon!
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)
Cell In[14], line 2
b = a
^
IndentationError: unexpected indent
There are times when indentation will be required and expected. We’ll discuss these in future lectures.