Python 2 hour intro: Difference between revisions

no edit summary
imported>Jesstess
(Created page with "This material is intended for a 1.5 - 2 hour interactive lecture. ==Math== 100px Math in Python looks a lot like math you type into a calculator. A...")
 
imported>Jesstess
No edit summary
Line 55:
 
The two previous expressions produce the same result. You only need to make one of the numbers in each fraction have a decimal. When the Python interpreter goes to do the division, it notices that one of the numbers in the fraction cares about decimals and says "that means I have to make the other number care about decimals too".
 
==Types==
 
[[File:Geometry.png|150px]]
 
There's a helpful <b>function</b> (more on what a function is in a second) called <code>type</code> that tells you what kind of thing -- what <b>data type</b> -- Python thinks something is. We can check for ourselves that Python considers '1' and '1.0' to be different data types:
 
<pre>
type(1)
type(1.0)
</pre>
 
So now we've seen two data types: <b>integers</b> and <b>floats</b>.
 
I used the term 'function' without explaining what it is -- we'll talk about functions more in a bit, and write our own, but for now know these things:
 
* Functions encapsulate some useful bit of work. We save that useful bit of work inside the function so we don't have to type it over and over again every time we want to use it. So, for example, some nice person decided that being able to determine the type of an object was useful, so he or she put the Python code that figures out an object's type into the function <code>type</code>, and now we all get to use it, instead of having to write it ourselves.
* Functions are sort of like functions in math class. You provide input to a function and it produces output. The <code>type</code> function takes data as an input, and produces what type of data the data is (e.g. an integer or a float) as output.
* To use a function, write the name of the function followed by an open parenthesis, what the function takes as input (we call that input the <b>arguments</b> to the function), and then a close parenthesis.
 
So in this case 'type' is the name of the function, and it takes one argument; in the example we first give <code>type</code> an argument of 1 and then give it an argument of 1.0.
 
===Command history===
Line 269 ⟶ 248:
 
Like the <code>type</code> function from before, the <code>str</code> function takes 1 argument. In the above example it took the integer 1. <code>str</code> takes a Python object as input and produces a string version of that input as output.
 
===String length===
 
There's another useful function that works on strings called <code>len</code>. <code>len</code> returns the length of a string as an integer:
 
<pre>
print len("Hello")
print len("")
fish = "humuhumunukunukuapuaʻa"
length = str(len(fish))
print fish + " is a Hawaiian fish whose name is " + length + " characters long."
</pre>
 
===Quotes===
Line 324 ⟶ 291:
print "A" * 40
print "ABC" * 12
h = "Happy"
b = "Birthday"
print (h + b) * 10
</pre>
 
Line 337 ⟶ 301:
1.
<pre>
total = 1.53 - 1/2 + ((-2.0/2) - (1.0/2))5
print total
type(total)
</pre>
 
Line 348 ⟶ 311:
c = "fox jumps over the lazy dog"
print "The " + a * 3 + " " + b * 3 + " " + c
</pre>
 
3.
<pre>
print 2.0 * 123 + str(2.0) * 123
</pre>
 
4.
 
(Remember, copying and pasting is fine here in this practice section -- we'll go back to typing out the code for part 2)
 
<pre>
a = "| (_| -()- -()- -()- -()- | -()- -()- -()- -()- ||\n"
b = "|_\_|_/___|__|__|__|___|__|___|__|___________________________||\n"
c = "|________________________________|__|__()_|__()_|__()__|_____||\n"
d = " ___|)_______________________________________________________\n"
e = "|_/(|,\____/_|___/_|____/_|______|___________________________||\n"
f = "|___/____________________________|___________________________||\n"
g = "| | | () | () | () | | ||\n"
h = "|__\___|.________________________|___\_|___\_|___\_|___|_____||\n"
i = "|__/|_______/|____/|_____/|______|___________________________||\n"
j = "|_____/__________________________|____\|____\|____\|_________||\n"
k = "|____/___________________________|___________________________||\n"
l = "|__/___\_._______________________|__|__|__|__|__|__|___|_____||\n"
 
print d + f + i + e + b + g + a + c + l + h + j + k
</pre>
 
Anonymous user