By the end of this week, you should be able to:
“A computer is like a Swiss Army knife that you can configure for countless tasks. Many people spend hours clicking and typing to perform repetitive tasks, unaware that the machine they’re using could do their job in seconds if they gave it the right instructions.”
# A simple expression
2 + 2
2
) and operators (such as +
)2 + 2
is evaluated down to a single value, 4
Operator  Operation  Example  Evaluates to… 

** 
Exponent  2**3 
8 
% 
Modulus/remainder  22%8 
6 
// 
Integer division/floored quotient  22//8 
2 
/ 
Division  22/8 
2.75 
* 
Multiplication  3*5 
15 
 
Subtraction  52 
3 
+ 
Addition  2+2 
4 
Test Question:
If today was a Tuesday and you wanted to know what day of the week it would be 100 days from now, which operator would help?
Answer:
Finding the remainder is really handy at solving problems like this. 100 % 7 = 2
. So if today was a Tuesday, then 100 days from now would be a Thursday since that is 2 days later in the week than a Tuesday.
Test Question:
How many weeks will have passed 100 days from now?
Answer:
Integer division can help solve this problem. 100 // 7 = 14
. So 14 full weeks will have passed 100 days from now (plus 2 additional days).
**
*
/
//
%
+

>>> 2 + 3 * 6
20
>>> (2 + 3) * 6
30
>>> 48565878 * 578453
28093077826734
>>> 2 ** 8
256
>>> 23 / 7
3.2857142857142856
>>> 23 // 7
3
>>> 23 % 7
2
>>> 2 + 2
4
>>> (5  1) * ((7 + 1) / (3  1))
16.0
>>> 5 +
File "<stdin>", line 1
5 +
^
SyntaxError: invalid syntax
>>> 42 + 5 + * 2
File "<stdin>", line 1
42 + 5 + * 2
^
SyntaxError: invalid syntax
Common Data Types
Data Type  Examples 

Integers  2 ,1 ,0 ,1 ,2 ,3 ,1042 
Floatingpoint numbers  1.25 ,1.0 ,0.0 ,0.5 ,1.35442 
Strings  a ,aa ,Hello World! ,9 
SyntaxError: EOL while scanning string literal
often happens when the final single quote character at the end of the string is missing>>> 'Hello world!
SyntaxError: EOL while scanning string literal
+
adds to values together if they are int
or float
, but it is the concatenation operator>>> 'Alice' + 'Bob'
'AliceBob'
>>> 'Alice' + 42
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
'Alice' + 42
TypeError: Can't convert 'int' object to str implicitly
*
is the multiplication operator with int
or float
, but with two strings it is a replication operator>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'
>>> 'Alice' * 'Bob'
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
'Alice' * 'Bob'
TypeError: can't multiply sequence by nonint of type 'str'
>>> 'Alice' * 5.0
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
'Alice' * 5.0
TypeError: can't multiply sequence by nonint of type 'float'
spam = 42
is an assignement statement>>> spam = 40
>>> spam
40
>>> eggs = 2
>>> spam + eggs
42
>>> spam + eggs + spam
82
>>> spam = spam + 2
>>> spam
42
>>> spam = 'Hello'
>>> spam
'Hello'
>>> spam = 'Goodbye'
>>> spam
'Goodbye'
It can be only one word.
It can use only letters, numbers, and the underscore, _
, character.
It can’t begin with a number.
Valid and Invalid Variable Names
Valid variable names  Invalid variable names 

balance 
currentbalance (hyphens are not allowed) 
currentBalance 
current balance (spaces are not allowed) 
current_balance 
4account (can’t begin with a number) 
_spam 
42 (can’t begin with a number) 
SPAM 
total_$um (special characters like $ ) 
account4 
hello (special characters like ' are not allowed) 
Week_2_Code
folder in file StartingOff.py
Comments
# This program says hello and asks for my name
The print()
function
print('Hello world!')
print('What is your name?') # ask for their name
The input()
function
myName = input()
The len()
function
print('The length of your name is:')
print(len(myName))
>>> len('hello')
5
>>> len('My very energetic monster just scarfed nachos.')
46
>>>len('')
0
The str()
, int()
, and float()
functions
>>> str(0)
'0'
>>> str(3.14)
'3.14'
>>> int('42')
42
>>> int('99')
99
>>> int(1.25)
1
>>> int(1.99)
1
>>> float('3.14')
3.14
>>> float(10)
10.0
#Binary numbers in Python are always prepended with '0b'.
#So instead of writing 101 you would write 0b101
#0b101 is equal to 5
print("The binary number 101 converts to:")
print(0b101)
#We can also add binary numbers
print("5 + 5 =")
print(0b101 + 0b101)
#or we can convert back and forth
numberOfDaysInWeek = 0b111
print("There are " + str(numberOfDaysInWeek) + " days in a week")
print("And 7 in binary is " + bin(7))
print(0b11111111)
print(0xff)
The binary number 101 converts to:
5
5 + 5 =
10
There are 7 days in a week
And 7 in binary is 0b111
255
255
#Hexadecimal numbers in Python are always prepended with '0x'.
#So instead of writing ff you would write 0xff
#0xff is equal to 255
print("The hexadecimal number ff converts to:")
print(0xff)
#We can also add binary numbers
print("11 + 13 =")
print(0xa + 0xc)
#or we can convert back and forth
numberOfDaysInYear = 0x16d
print("There are " + str(numberOfDaysInYear) + " days in a week")
print("And 365 in hex is " + hex(365))
#Convert 56 from decimal to hexadecimal
print(0b10010)
The hexadecimal number ff converts to:
255
11 + 13 =
22
There are 365 days in a week
And 365 in hex is 0x16d
18
Introducing Python Chapter 1  A more detailed look at the same materials as this week’s programming lectures.
Think Python Chapters 1 & 2  A different approach from a programming book.
CodeAcademy’s Python lesson  CodeAcademy provides a pretty, handson interactive lesson. It is a similar experience to the selfpaced exercises in PyCharm. Unit 1 & 2 are the best fit for this week’s materials.