Tuesday, August 21, 2012

Exercises 6-13: Variables and basic input/output

ex6.py:
1:  x = "There are %d types of people" % 10  
2:  binary = 'binary'  
3:  do_not = "don't"  
4:  # I suppose this line shows that you can assign formatted strings to  
5:  # variables as well as printing them straight to stdout  
6:  y = "Those who know %s and those who %s" % (binary, do_not) # 1 (maybe 2)  
7:  print x  
8:  print y  
9:  # This is to show its the value of the string that affects the output  
10:  # rather than the name of the variable  
11:  hilarious = False  
12:  joke_eval = "Isnt that joke so funny? %r"  
13:  print joke_eval % hilarious  # 2 (3)  
14:  # Shows how + concatenates strings  
15:  w="1 2 3..."  
16:  e="4 5 6."  
17:  print w+e  # depending on interpreation = 2/3/4  
18:  # at least 2 strings are put in side other strings  
19:  # depending on interpretation, up 4-5 strings are put in other strings  
20:  # In order for + to be a valid operation, its arguments  
21:  # must be ordered and have a distance between.  
22:  # So, you can add integers, reals, complex numbers, vectors...  
23:  # But strings aren't either of these. So what to do when you call + on a  
24:  # pair of strings? throw an exception? Whoever made python evidently  
25:  # decided the most logical answer to that question is to concatenate the  
26:  # strings. imho, at least as good of an answer as any other.  
ex7.py:
1:  print "Mary had a little lamb."  
2:  print "Its fleece was white as %s." % 'snow'  
3:  print "And everywhere that Mary went."  
4:  print "." * 10 # what'd that do?  
5:  end1 = unichr(67).encode('utf-8')  
6:  end2 = "h"  
7:  end3 = "e"  
8:  end4 = "e"  
9:  end5 = "s"  
10:  end6 = "e"  
11:  end7 = "B"  
12:  end8 = "u"  
13:  end9 = "r"  
14:  end10 = "g"  
15:  end11 = "e"  
16:  end12 = "r"  
17:  # watch that comma at the end. try removing it to see what happens  
18:  print end1 + end2 + end3 + end4 + end5 + end6,  
19:  print end7 + end8 + end9 + end10 + end11 + end12  
20:  # comma seems to squelch the implicit newline character  
21:  # and replaces it with a space.  
ex8.py:
1:  formatter = "%r %r %r %r"  
2:  print formatter % (1,2,3,4)  
3:  print formatter % ('one','two','tree','four')  
4:  print formatter % (True, False, False, True)  
5:  print formatter % (formatter,formatter,formatter,formatter)  
6:  print formatter % (  
7:    "I had this thing",  
8:    "That you couldd type right up",  
9:    "But it didnt sing",  
10:    "So I said gooednight")  
11:  #print formatter % (1,2,3,4)  
12:  # because %r is used instead of %s, its not immediately assuming  
13:  # that variable/constant is a string, so when it gets strings it prints  
14:  # that it is a string but surrounding it in apostrophes.  
ex9.py:
1:  days = "M T W T F S S"  
2:  months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"  
3:  print "dyas:",days  
4:  print "months:",months  
5:  print """  
6:  Theres something goign on here  
7:  with the three dobules 1quoes  
8:  well be able to totype as much as we like  
9:  even 4 lines i fwe want or 56 or 6  
10:  """  
11:  days = "M T W T F S S"  
12:  months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"  
13:  print "dyas:",days  
14:  print "months:",months  
15:  print """  
16:  Theres something goign on here  
17:  with the three dobules 1quoes  
18:  well be able to totype as much as we like  
19:  even 4 lines i fwe want or 56 or 6  
20:  """  
ex10.py:
1:  print "%r" % "I am 6'2\" tall." # escape double-quote inside string  
2:  print "%r" % 'I am 6\'2" tall.' # escape single-quote inside string  
3:  # Huh... %r automatically changes them to single quotes, parsing escaped  
4:  # chars and re-escaping apostrophes. Pretty rocking. I would guess then that  
5:  # \ts and \ns etc. are also not printed for %r prints  
6:  tabby_cat = "\tI'm tabbed in."  
7:  persian_cat = "I'm split\non a line."  
8:  backslash_cat = "I'm \\ a \\ cat."  
9:  fat_cat = """  
10:  I'll do a list:  
11:  \t* Cat food  
12:  \t* Fishies  
13:  \t* Catnip\n\t* Grass  
14:  """  
15:  print tabby_cat  
16:  print persian_cat  
17:  print backslash_cat  
18:  print fat_cat  
19:  for j in range(10000):  # while True:  
20:    for i in ["/","-","|","\\","|"]:  
21:      print "%s\r" % i,  
22:  # CTRL-C in linux/osx, CTRL-Z? perhaps D in windows to break infinite loops  
23:  # somehow \r makes this print on top of the last character printed. very cool.  
24:  # I did not realise this was even possible in shell... :O!  
25:  fat_cat = '''  
26:  I'll do a list:  
27:  \t* Cat food  
28:  \t* Fishies  
29:  \t* Catnip\n\t* Grass  
30:  '''  
31:  print fat_cat  
32:  # seems to do the exact same thing. presumably the choice is made by which  
33:  # chars you want to escape.  
ex11.py:
1:  print "How old are you?",  
2:  age = raw_input()  
3:  print "How tall are you?",  
4:  height = raw_input()  
5:  print "How much do you weigh?",  
6:  weight = raw_input()  
7:  # raw_input() appears to take stdin until a newline  
8:  # and automatically throw away that newline  
9:  print "So, you're %r old, %r tall and %r heavy." % (  
10:    age, height, weight)  
11:  # ah the benefit (besides diagnostic prints) of %r becomes clear here :D  
12:  print "How much are you liking Python now?",  
13:  leadingq1 = raw_input()  
14:  print "How much do you expect to love Python in the future?",  
15:  leadingq2 = raw_input()  
16:  print """  
17:  So, you like Python %r now, and will love Python %r in the future.  
18:  Meaning the rate at which your opinion of Python is changing is  
19:  %r per time unit.  
20:  """ % ( leadingq1, leadingq2, (int(leadingq2)-int(leadingq1)))  
ex12.py:
1:  age = raw_input("How old are you? ")  
2:  height = raw_input("How tall are you? ")  
3:  weight = raw_input("How much do you weigh? ")  
4:  print "So, you're %r old, %r tall and %r heavy." % (  age, height, weight)  
5:  #print sys.argv  
6:  # ^ this didnt work  
ex13.py:
1:  from sys import argv  
2:  # ^ ah right, that solves that then  
3:  #script, first second, third = argv  
4:  #       ^  MY FIRST COMPILE ERROR!  
5:  # Even though Zed keeps telling me to write these down so they dont happen  
6:  # again, in the case of typos, I think I will skip his sage advice.  
7:  script, first, second, third = argv  
8:  print "The script is called:", script  
9:  print "Your first variable is:", first  
10:  print "Your second variable is:", second  
11:  print "Your third variable is:", third  

No comments:

Post a Comment