Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Summer Girls
Views: 54
Kernel: Python 2 (SageMath)

Basic Python

[This material was heavily influenced by (stolen from) the UC Berkeley Python Bootcamp material.]

Compared to a lot of other programming languages, Python is easy to use and understand. This tutorial is just a quick introduction on how to use Python to get you started, and there are a lot of resources on the internet that will take you further.

Some basic info: What we're using is the Jupyter Notebook (often called "IPython Notebook"). You can type commands in each box (called a "cell"), and then push Shift+Enter to run them. The "+" sign near the top creates a new cell.

We're going to learn by doing. Every once in a while, there will be a Task, which will be a simple problem you have to solve using what you've just learned.

The only two rules we will be following: 1) When in doubt, try it out, and 2) the internet is your friend.

Getting Python to talk to us

print 'My "World"'
My "World"
print 'Hello World!'
Hello World!

Task: How would you say Hello Tim's World! or Hello "my" World!?

# This is a comment. print 'This is not a comment.'
This is not a comment.
'Did I remember to brush my teeth this morning?'
'Did I remember to brush my teeth this morning?'

Python can be a calculator

print 8 - 4
4
# Spaces between characters don't matter print 2+2
2 + 2

Character Data Types

#ints (integers) 2
print 2
#floats 2.0
print 2.
#strings '2'
print 2 +'2'

Task: What happens when you add any combination of ints, floats, and strings? Int+Int, Float+Float, Str+Str, Int+Float, Int+Str

2.1 + 2
"2 + 2"
(3*10 - 26)/5
print 4.0/5

Task: How do we get the right answer to the previous math expression?

type(0.8)

Changing data types:

(3*10 - 26)/float(5)
int(0.8)
float('5')
str(4)
float('a')

Using and Defining Variables

You drop a ball and want to know how far it falls in 1 second. The physics equation is: d=vit+12at2d = v_i t + \frac{1}{2}at^2.

If it starts from rest (vi=0v_i = 0), then d=12at2d = \frac{1}{2}at^2

You can solve the equation with python, using variables:

# In this example, t, accel, and dist are all variables t = 1.0 a = 9.8 d = 0.5*a*t*t print d
4.9
# python remembers what the variables are print t
# but you can always reuse a variable name t = 20 print t
20

After changing t, what will happen to d?

d = 0.5*a*t*t print d
1960.0
print t
20
t = 1.0 # let's reset t to 1 second # some other ways to write the distance equation dist1 = a*(t**2)/2 dist2 = 0.5*a*pow(t,2) print dist1, dist2
4.9 4.9

So now we can calculate the distance, but it seems kind of silly to restate what "dist" is every time we change "t." This is where functions come in handy:

Defining and Using Functions

What is a function?

  • A block of organized, reusable code that is used to perform a single, related action.
  • Provides better modularity for your application and a high degree of code reusing.
  • You can name a function anything you want as long as it:
    1. Contains only numbers, letters, underscore
    2. Does not start with a number
    3. Is not the same name as a built-in function (like print).
def addnums(x,y): return x + y
addnums("hello ", "there")
'hello there'
def yolo(a): return a + " #yolo"
yolo("I ate three eggs for breakfast this morning")
'I ate three eggs for breakfast this morning #yolo'
addnums(0x1f,3.3)
def dist(t, accel): answer = 0.5 * accel * t * t return answer print dist(1, 9.8) print dist(5, 1.6)
4.9 20.0
print dist('cat')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-16-6860321472f5> in <module>() ----> 1 print dist('cat') <ipython-input-15-5365f8063925> in dist(t) 1 def dist(t): 2 accel = 9.8 ----> 3 answer = 0.5 * accel * t * t 4 return answer 5 TypeError: can't multiply sequence by non-int of type 'float'

Task: What if you want the distance formula to work for both Earth and the Moon (where g = 1.6 m/s2^2)? That is, what if you don't want 'accel' to be fixed at 9.8?

Math

Comparing values

a = 3 ; b = 4 a == b # two '=' signs to compare values, one '=' to assign a value
False
a+1 == b
True
a+1.0 == b
True
1=2
File "<ipython-input-24-f7e412d8cdaf>", line 1 1=2 SyntaxError: can't assign to literal
a < 10
True
a < 3
False
a <= 3
True
a != 17
True

Modules

  • A file consisting of Python code you can reference (any file ending in .py is treated as a module).
  • Allows you to logically organize your Python code to make the code easier to understand and use.
  • Can define functions, classes and variables.
  • Pre-existing modules can be very useful.
print sqrt(4)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-32-1b1e4f39c953> in <module>() ----> 1 print sqrt(4) NameError: name 'sqrt' is not defined
import math
math.cos(0)
1.0
math.cos(math.pi)
-1.0
math.sqrt(4)
2.0
from datetime import datetime
now = datetime.now()
print (now.year, now.month, now.day, now.hour, now.minute, now.second)
(2016, 7, 14, 17, 51, 33)
from math import sqrt as poo
poo(16)
4.0

Strings

Strings are sequences of characters. They can be made of letters, numbers, and symbols. We've actually already seen strings already:

x = "Hello, World!" print x
print "Hello, World!\nI want food." # '\n' means 'new line'
print "I said, 'I want food.'"
print "I said, "I want food.""
print "I said, \"I want food.\""

Backslashes ( \ ) tell python to be ready for special characters, called "escape characters". Some examples are:

\n = newline \t = tab \a = bell
# Triple quotes are useful for strings on multiple lines y = '''Four score and seven minutes ago, you all learned some basic stuff with Python and you were all blown away!''' print y

A string is really just a group of characters. You don't have to always work with the whole string, you can do things to parts of a string:

s = 'cats' print s print s[0] print s[2]
print s[-1] print s[-2]
print s[1:3]

Once you've created a string, you can't change just one piece of it.

mygrade = 'D+' print mygrade
mygrade[0] = 'A'
# You can put strings together with the '+' sign. This is caled 'concatenation.' c = "cat" ; d = "dog" print c + d
print c + " and " + d
# You can do multiple concatenations with the '*' sign print c*3 +de
print 'I have ' + 3 + c
# We have to cast the 3 (an int) as a string print 'I have ' + str(3) + c
print c print len(c) # len() tells you the length of things

Task: Print out a googol (1 followed by 100 zeros without literally typing '0' 100 times).

Control flow (loops)

Indentation in Python puts things inside of the loop or acts as the "then" of an "if" statement. Other languages may use curly braces {}.

x = 1 print x
x = -1 if x > 0: # colons indicate the beginning of a control statement print "yo" else: # unindenting tells Python to move to the next case print "dude" print "I'm done" # unindenting also tells Python the control statement is done
dude I'm done
# You can have multiple cases x = 30 if x < -10 or x == 30: print "yo" elif x > 10 and x <= 15: # 'elif' is short for 'else if' print "dude" if x > 20: print "sweet" else: print "sup"
yo sweet
1==1.0
# An example of a 'for' loop: for x in range(5): print x
0 1 2 3 4
print range(6)
[0, 1, 2, 3, 4, 5]
for x in ("now", "we", "got", "bad", "blood"): print x
now we got bad blood
lyrics = ("you", "know", "it", "used", "to", "be", "mad", "love") for word in lyrics: print word,
you know it used to be mad love
print lyrics[2] #3 is an index
it
print range(6) [0]
x = 0 while x < 5: print pow(3,x) x += 1 # if you don't do this you will be stuck in an infinite loop!

Task: Repeat the previous example (the pow(3,x) one) but with a For loop instead of a While loop, and without using pow().

Data structures in python

We've already seen examples of tuples in the T Swift examples:

lyrics = ("you", "know", "it", "used", "to", "be", "mad", "love") print type(lyrics)

Tuples are immutable, which means they are unchangeable. Once you've defined a tuple, you can't change it. (We saw this is true for strings as well.)

lyrics[0] = "I"
print lyrics[3]
print lyrics[3:]
print lyrics[::-1]
print lyrics[3::2]
print lyrics[5:2:-1]

Lists are similar to tuples, but they can be changed:

myList = [4,1,2,7,3,0,8,2,3,9] print myList
print myList[4:0:-1]
myList[3] = 'banana' print myList
newList = [ 10, 37, [2, 3, 2], 8 ] print newList
# Adding to a list myList.append('kitties') print myList
newList2 = [ [1, 2, 3] , [4, 5, 6] , [7, 8, 9] ] print newList2

Task: How would you print out the rows in "newList2" one row at a time?

for i in newList2: print i
for i in range(len(newList2)): print newList2[i]

Making Pictures

from PIL import Image img = Image.new('RGB', (500,500), 'black') img.save('black.png')

Go back to your files. Image file black.png should appear. Open it, then right click on the image to open in a new window. This is an easy way to access it and reload it as you make changes. Try changing the size and default color.

from PIL import Image img = Image.new('RGB', (550,500), 'black') print img.size[1] img.save('black.png')
500

Task: How can you print just the width? Just the height?

from PIL import Image img = Image.new('RGB', (550,500), 'chartreuse') pixels = img.load() # creates a "pixel access map" - numbers pixels left to right and top to bottom, # and allows you to change individual pixels print pixels[0,0] # prints RGB code for the top left pixel img.save('black.png')
(127, 255, 0)
from PIL import Image img = Image.new('RGB',(500,500),'black') pixels = img.load() for x in range(img.width): for y in range(img.height): if (y-249)>(x-249)**2: pixels[x,y] = (255,255, 255) img.save('line.png')
from PIL import Image import math img = Image.new('RGB',(500,500),'black') pixels = img.load() xcenter = 250 ycenter = 250 for x in range(img.width): for y in range(img.height): dist = math.sqrt( (y-ycenter)**2 + (x-xcenter)**2 ) pixels[x,y] = (int(dist), 0, 0) img.save('circle.png')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-82-9ae41daf449f> in <module>() 11 for y in range(img.height): 12 dist = math.sqrt( (y-ycenter)**2 + (x-xcenter)**2 ) ---> 13 pixels[x,y] = (dist, 0, 0) 14 15 img.save('circle.png') TypeError: integer argument expected, got float
from PIL import Image img = Image.new('RGB', (500,500), 'black') pixels = img.load() for i in range(img.size[0]): # for every pixel across (using i coordinate) for j in range(img.size[1]): # for every pixel down (using j coordinate) pixels[i,j] = (255,255,255) img.save('color.png')
from PIL import Image img = Image.new('RGB', (500,500), 'black') pixels = img.load() for i in range(img.size[0]): # for every pixel across (using i coordinate) for j in range(img.size[1]): # for every pixel down (using j coordinate) if i == 50 or i == 100 or i == 150: pixels[i,j] = (255,255,255) #if i == j: #pixels[i,j] = (255, 255, 255) img.save('lines.png')

Task: How can you make a white line 10 pixels wide?

Task: How can you make a diagonal white line?

from PIL import Image img = Image.new('RGB', (500,500), 'black') pixels = img.load() for i in range(img.size[0]): for j in range(img.size[1]): pixels[i,j] = (i/2, j/2, 255) img.save('gradient.png')
from PIL import Image import math img = Image.new('RGB', (500,500), 'black') pixels = img.load() x = 25 y = 200 for i in range(img.size[0]): for j in range(img.size[1]): dist = math.sqrt((i - x)**2 + (j - y)**2) # distance from point (x,y) to pixel (i,j) pixels[i,j] = (int(dist), 0, 0) img.save('circle.png')
from PIL import Image import math img = Image.new('RGB', (500,500), 'black') pixels = img.load() x = 25 y = 200 for i in range(img.size[0]): for j in range(img.size[1]): dist = math.sqrt((i - x)**2 + (j - y)**2) pixels[i,j] = (int(dist), 0, 0) if dist > 18 and dist < 22: # What does this do pixels [i,j] = (255, 255, 255) img.save('circle.png')
from PIL import Image import math img = Image.new('RGB', (500,500), 'black') pixels = img.load() x = 25 y = 200 x2 = 400 y2 = 100 for i in range(img.size[0]): for j in range(img.size[1]): dist = math.sqrt((i - x)**2 + (j - y)**2) pixels[i,j] = (255 - int(dist), 255, 0) dist2 = math.sqrt((i - x2)**2 + (j - y2)**2) pixels[i,j] = (pixels[i,j][0], pixels[i,j][1] - int(dist2), pixels[i,j][2]) img.save('circle.png')

Project:

Create your own picture, modifying pixels however you like. Be creative and challenge yourself!

If you get stuck or it doesn't work, it can help writing out in words on paper or in a word doc exactly what you want it to do, then translate it to python code.

When you're done, copy the image into an email and send it to [email protected]

from PIL import Image import math img = Image.new('RGB', (500,500), 'black') pixels = img.load() x = 25 y = 200 for i in range(img.size[0]): for j in range(img.size[1]): dist = math.sqrt((i - x)**2 + (j - y)**2) pixels[i,j] = (int(dist), 0, 0) if dist > 18 and dist < 22: pixels [i,j] = (255, 255, 255) img.save('picture.png')