Python for Asshats
Chapter 3
This is a bit of an addendum for the previous doozy of a chapter. This one "should" be a lot less dense.
My shrink says it's "high functioning"
There's a notation we've been using quite a bit and will be using a lot more in the near future that I'd like to codify for you: function call syntax. When the interpreter sees something in the form name(arguments)
, it basically runs whatever code is stored under that name using the arguments inside the parentheses. Later on, I'll show you how to make your own, but for now we'll be using functions already built into Python. For example, the code stored under print
prints the argument you gave it to the screen.
Some functions return a value, which means that it gives an output based on its input. You can think of it as the interpreter replacing the function call with the value it outputs, similarly to accessing a variable (with the notable exception that you cannot write to a function call). Take, for example, the function abs
, which returns the absolute value of a number.
x = abs(-10) + 3 # same as 10 + 3
print(x)
Output:
13
Cool beans.
Bippity bing, your int is now a string!
Converting between types in Python is delightfully easy.
Code:a = int("10") + 2
print(a)
Output:
12
Python declares functions with the names of it's default types — int
, float
, str
, bool
, et cetera — that return the argument converted to the desired type. Type conversion is very useful when you want to create strings dynamically, because you can now concatenate them with other types.
dicks = 20
print("I sucked " + str(dicks) + " dicks today") # number as string
is_a_lot = dicks > 10
print("It is " + str(is_a_lot) + " that thats a lot") # boolean as string
Output:
I sucked 20 dicks today
It is True that thats a lot
So far, we have had to identify our print statements by the order they appear in the program, so it's nice that we can label them now. We can also convert between types of numbers. Converting a float to an int removes the decimal component, and converting an int to a float adds a trailing .0
print(int(2.5)) # truncate to integer
print(float(25)) # add decimal point
Output:
2
25.0
This is very handy when you want to use a float as a list index.
Code:a = [0, 1, 2, 3]
print(a[4 / 2]) # division always gives a float
Output:
Traceback (most recent call last):
File "/home/timgor/test.py", line 2, in <module>
print(a[4 / 2]) # division always gives a float
TypeError: list indices must be integers or slices, not float
4 / 2
gives 2.0
, which is a float despite not having a decimal component. It must be converted to an int before being used as an index.
a = [0, 1, 2, 3]
print(a[int(4 / 2)]) # it's an int now!
Output:
2
Another useful type conversion task is turining strings into numbers, but you should be careful. The program exits if it encounters something it cannot interpret.
Code:print(float("12 uwu"))
Output:
Traceback (most recent call last):
File "/home/timgor/test.py", line 1, in
print(float("12 uwu"))
ValueError: could not convert string to float: '12 uwu'
Computers are stupid and anal, so even decimal points cause an error if you are trying to extract an int from a string.
Code:print(int("12.0"))
Output:
Traceback (most recent call last):
File "/home/timgor/test.py", line 1, in
print(int("12.0"))
ValueError: invalid literal for int() with base 10: '12.0'
In this case, it is necessary to convert the string to a float, and then to an int.
Code:print(int(float("12.0")))
Output:
12
Python provides an excellent tool for debugging types, the type
function. Give it a value, and it returns what type it is.
print(type("i want guido van rossums thick cock inside me"))
print(type(10))
print(type(10.0))
print(type(False))
print(type([1,2,3]))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'list'>
The most inquisitive among you may wonder what type is returned by the type
function, so let's find out.
print(type(type(0)))
Output:
<class 'type'>
Huh.
One of the built in types I didn't mention in the last chapter was the type type. Though, since that function returns a type type of type type...
Code:print(type(type(type(type(type(0))))))
# this can go on forever
Output:
<class 'type'>
Try not to think about that too much.
Listlessness
Strings and lists seem, fundamentally, to be very different things. One is text, another is a sequence of arbitrary data. But surprisingly, both share a lot of features. In fact, strings can be indexed and sliced just like lists can.
Code:string = "cucumber"
print(string[3]) # third character
print(string[2:5]) # characters 2-4
Output:
u
cum
They also both support the handy len
function, which returns the number of items, which are characters in the case of a string.
print(len("long boye"))
print(len([1, 1, 2, 3]))
Output:
9
4
There is also a handy comparison operator for use with strings and lists, in
. In the case of lists, item in list
evalutes to wether any of the items in list
are equal to ==
the specicifed item
.
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] # create a list of primes
print(11 in primes) # True, 11 == primes[4]
print(14 in primes) # False, 14 is not in primes
Output:
True
False
The behavior with strings is a bit different. substring in string
tells you if string
can be sliced in any way that equals substring
rawr = "rawr x3 *nuzzles* how are you *pounces on you*"
print("nuzzle" in rawr) # True, "nuzzle" == rawr[9:15]
print("sanity" in rawr) # False, sanity is nowhere to be found
Output:
True
False
This chapter was basically some miscellaneous additions to Chapter 2. The next chapter will introduce tree LSTM neural network architecture and it's applications in deep learning as was introduced in Improved Semantic Representations From Tree-Structured Long Short-Term Memory Networks (Sheng Tai et al.)
Nah, fuck you. It introduces some basic control statements.