-
Notifications
You must be signed in to change notification settings - Fork 0
Home
I make the following assumptions in this Lab note.
Textwrangler is installed in your system.
Python is also installed, OSX comes with this already installed.
You have a working knowledge of commandline (if not, go through commandline notes)
Lets get it on!
ps. I use a macbook, there are variations for powershell in windows
Open terminal and change (cd) into a directory to work from. Mind the location you choose.
Open a new file in textwrangler.
Type in the following words exactly as they appear. (Include the word print)
print "Hello world"
print "I don't like typing"
print "But, its a way of life!"
print "So beat it!"
Save this as ex1.py.
Go back to terminal and run the command below. Makes sure you are in the folder that contains the file that has just been created. Use pwd or ls to confirm This applies to all other exercises after this.
python ex1.py
What you should see
Hello world
I don't like typing
But, its a way of life!
So beat it
cat > ex1b.py (see what cat does from commandline notes, use crtl q to exit out of cat when you are done typing)
print "Hello world"
# print "I dont like typing"
print "But, its a way of life!"
print "So beat it sweetheart"
run
python ex1b.py
What you should see
Hello world
But, its a way of life!
So beat it sweetheart
cat > ex2.py
# I am trying to find out how to insert comments
# This is how I do it
print "This will work" # Lets see
# Fingers crossed!
python ex2.py
What you should see
This will work
The following are symbols recognized by python.
- plus
- minus
/ slash
- asterisk
% percent (modulus) < less-than
greater-than
<= less-than-equal
= greater-than-equal
cat > ex3.py
print "Life is all about numbers, that's where the answers are!!"
print "Maybe python knows this too!"
print "Number", 19 + 23
print "Toes", 5 + 5 * 6
print "Compare", 5 > -2
print "Really?", 6 > 7
print 5 % 20
print 2 ** 5
print 5 / 2
# floating numbers
print 5.0 / 2.0
python ex3.py
Life is all about numbers, that’s where all the answers are!!
Maybe python knows this too!
Number 42
Toes 35
Compare True
Really? False
5
32
2
2.5
cat > ex4.py
cars = 100
space_in_car = 4.0
drivers = 30
passangers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_car
average_passangers_per_car = passangers / cars_driven
print "There are", cars, "cars available"
print "etc"
python ex4.py There are 100 cars available Etc
cat > ex5.py
my_name = "Maria"
my_state_of_mind = 'tired'
cause_of_problem = 'sitting too long'
recommended_remedy = 'dancing'
dosage = 3
song = 'smooth criminal'
time = 10
length_in_weeks = 12
print "lets talk about %s." % my_name
print "she is %s" % my_state_of_mind
print "the main cause of this problem is %s" % cause_of_problem
print "we recommend %s, for %d weeks, %d times a day, to the song %r by MJ!" %(recommended_remedy, length_in_weeks, dosage, song)
python ex5.py
lets talk about Maria.
she is tired
the main cause of this problem is sitting too long
we recommend dancing, for 12 weeks, 3 times a day, to the song 'smooth criminal' by MJ!
cat > ex6.py
x = "the girl who wants to and reads %d books a day" % 10
c = 'books'
do_not = "the girl who reads %d %s!. Ouch! my eyes hurt!" % (2, c)
y = "There are two people trapped inside me, %s and %s" %(x, do_not)
print y
diagnosis_x = "%s - conformist!" % x
diagnosis_y = "%s - wants to find out by doing it!, not reading it!" % do_not
print "The 'doctor' said: %r" % diagnosis_x
print "He also said: %s" % diagnosis_y
n = 'Am so finished!!'
o = ' Polarised forever!'
print " My dignosis?"
print n + o
Run python ex6.py
There are two people trapped inside me, the girl who wants to and reads 10 books a day and the girl who never reads books!. Ouch! my eyes hurt!
The 'doctor' said: 'The girl who wants to and reads 10 books a day. - conformist!'
He also said: The girl who never reads books!. Ouch! my eyes hurt! - wants to find out by doing it!, not reading it!
My dignosis?
Am so finished!! Polarised forever!
Ex 7:
cat ex7.py
print "Mary had a little lamb."
print "Its fleece was white as %s." % 'snow'
print "And everywhere that Mary went."
print "." * 10 # what'd that do?
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
# watch that comma at the end. try removing it to see what happens
print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end
cat > ex8.py
formatter = "%r %r %r %r %r %r %r %r %r"
print formatter % (1, 2, 3, 4, 5, 6, 7, 8, 9)
print formatter % ("one", "two", "three", "four", "four", "four", "four" , "four" , "four" )
print formatter % (1, 1, 2, 3, 5, 8, 13, 21, "." * 3)
print formatter % (formatter, formatter, formatter, formatter, formatter, formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight.", "Gotcha", "Gotcha", "Gotcha", "Gotcha", "Gotcha" )
Run python ex8.py
1 2 3 4 5 6 7 8 9
'one' 'two' 'three' 'four' 'four' 'four' 'four' 'four' 'four'
1 1 2 3 5 8 13 21 '...'
'%r %r %r %r %r %r %r %r %r' '%r %r %r %r %r %r %r %r %r' '%r %r %r %r %r %r %r %r %r' '%r %r %r %r %r %r %r %r %r' '%r %r %r %r %r %r %r %r %r' '%r %r %r %r %r %r %r %r %r' '%r %r %r %r %r %r %r %r %r' '%r %r %r %r %r %r %r %r %r' '%r %r %r %r %r %r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.' 'Gotcha' 'Gotcha' 'Gotcha' 'Gotcha' 'Gotcha'
cat > ex9.py
days = " mon tue wed thur"
months = "jan\nfeb\nmar\napr"
print " days of the week:", days
print " months of the year:", months
print
"""10
There are some nights when.
sleep plays coy,
aloof and disdainful.
And all the wiles
that I employ to win
its service to my side
are useless as wounded pride,
and much more painful. """
Run python ex9.py
days of the week: mon tue wed thur
months of the year: jan
feb
mar
apr
There are some nights when.
sleep plays coy,
aloof and disdainful.
And all the wiles
that I employ to win
its service to my side
are useless as wounded pride,
and much more painful.