Variables, values, and expressions
Professor Devin Balkcom
devin@cs.dartmouth.edu
office: Sudikoff 206
Assignment statement:
Strings have quotation marks around them. Variable names don't.
Bad math:
$x = 5$Legal python:
meaning_of_life = 42
Do exercise: big smile from the online notes for chapter 2 now.
01011100 10110101 11011011 00001111 10101010
bit: A binarity digit, with the value 0 or 1.
byte: A group of 8 bits in memory.
I need five volunteers.
01 01 11 00 10 11 01 01 11 01 10 11
b b d a c d b b d b c d
Imagine this is a secret code, with:
code | value |
---|---|
00 | a |
01 | b |
10 | c |
11 | d |
How many bits long should my bit-pattern be to give a unique pattern to every student in CS 1?
How many eight-bit patterns are there?
count | code |
---|---|
1 | 00000000 |
2 | 00000001 |
3 | 00000010 |
... | ... |
256 | 11111111 |
To interpret a value in memory, Python needs to know:
For each variable, Python keeps track of all that for you. (You just use the name, and Python does the rest.)
meaning_of_life = 42
An int is typically 4 bytes long on a modern machine.
$2^{32}$ is $4294967296$
# number of molecules in an avocado avocado_number = 6.02e23 weight_of_ant_in_grams = .004 # these are different! meaning = 42.0 life = 42
Used to represent big or small numbers. 8 bytes.
If floats and ints are both 8 bytes, how can a float hold a number bigger than 9223372036854775807?
lukes_father = "Darth Vader"
We can use one byte per character, using ASCII code.
Fortunately, there are no more than 256 letters, digits, etc. Right?
Not if you write Chinese. A story for another day.
i_love_cs = True
One bit should be enough to represent true or false.
(Actually, Python uses a whole byte. So wasteful.)
You can use an expression anywhere a value is required.
Bad code:
x = 73 y = x / 12 z = x % 12 print y print z
With meaningful variable names:
height_in_inches = 73 feet = height_in_inches / 12 inches = height_in_inches % 12 print feet print inches