louist87@gmail.com
Sep. 2015
python shell command?x86 assembly wants you to think in terms of CPU registers
section .data
str: db 'Hello world!', 0Ah
str_len: equ $ - str
section .text
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, str
mov edx, str_len
int 80h
mov eax, 1
mov ebx, 0
int 80h
Brainfuck wants you to think in terms of memory addresses and pointers:
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Python wants you to think in terms of physical objects:
print "Hello world!"
In Python, everything is an object!
Two levels of analysis:
Adding integers:
1 + 1
Dividing floats:
38.7 / 3.2
Adding strings:
"Hello " + "world!"
Adding an integer to a string:
"I am " + 28 + " years old"
| Type Name | Python object | Use case | Caveats |
|---|---|---|---|
| Boolean | bool |
Representing truth | |
| Integer | int |
Representing whole numbers | Beware of integer division! Try 10 / 2 |
| Float | float |
Representing decimal fractions | Beware of floating-point errors! Try .1 * 3 |
| String | str |
Representing text | Beware of accents and diacriticals! |
| List | list |
Maintaining an ordered sequence of objects that may change. Useful for homogenous types | |
| Tuple | tuple |
Maintainging an immutable ordered sequence Useful for heterogeneous types. | |
| Dictionary | dict |
Associating one object (the key) to another (the value) | Dicts are unordered |
| Set | set |
Keeping track of values that were encountered. Testing for the presence of multiple values. | Sets are unordered and contain at most one of each item. |
Computers build upon automata by allowing for data-dependent computation. How do we change the behavior of a program based on the value of a variable?
x = 10
if x > 10:
print("Greater than 10")
elif x:
print("Less than 10, but nonzero")
else:
print("exactly 0")
if and elif evaluate the expression that follows them. If the statement evaluates to True, the block of code is executed.
All objects have a Boolean truth value:
bool(0)
bool(1)
bool("")
bool("false")
bool([])
bool([1, 2, 3])
Zero and null values evaluate to False. Everything else evaluates to True.
| Symbol | Meaning | Example |
|---|---|---|
< |
Less than | 1 < 4 |
> |
Greater than | 2.3 > 0 |
== |
Equal to | 1 == True |
!= |
Not equal to | True != False |
= is for assignment, i.e.: n_subjects = 20== is for boolean evaluation, i.e.: {} == FalseThis will throw an error:
x = "hello"
if x = "hello":
print("success!")
Write a script to test if two strings are equal. If they are equal, print the string, otherwise print a message indicating that they are not equal. Bonus: edit your script to print the string in all capital letters. (Hint: look at the methods for str objects!)
Write a script to test if a dictionary contains the key “x”. The expression "x" in d (where d is a dictionary) will evaluate to True if "x" is a key in the dictionary. If the key is present, print its associated value. If it is absent, do the same for the key "y". If niether key is present, print a message explaining the dictionary is empty.
Functions are the basic element of abstraction in most programming languages. They are used to:
Functions take in objects, and return objects. Using a function is referred to as calling. Function calls are denoted by parentheses, e.g.:
sum([1, 3, 6, 2])
Functions are defined via the def keyword:
def sum(terms):
"""Add all elements of a list together and return the result"""
s = 0
for i in terms:
s += i
return i
Create a function that takes three arguments a, b, and c that correspond to the three sides of a right triangle with c being the hypotenuse. The function should calculate the length of the side for which None was passed using the Pythagorean theorem. For example: calculate_missing_side(3, 8, None) should return 8.544...
Create a function called map2 that takes a function as it’s first argument and a list of integers as it’s second argument. map2 should call the function passed into it once for each argument in the list and return a list containing all the results. Next, write a function called double that doubles a number. Call map2 as follows: map2(double, range(10)).