#!/usr/bin/env python import ex_name ex_name.title("ex27: Memorizing Logic") #Codes in the book (some words may be changed): # print """ The Truth Terms: * and * or * not * != (not equal) * == (equal) * >= (greater-than-equal) * <= (less-than-equal) * True * False """ print """ """ ##################################### ex_name.title("ex28: Boolean Practice")
#!/usr/bin/env python import ex_name ex_name.title("ex38: Doing Things to Lists") #Codes in the book (some words may be changed): # ten_things = "Apples Oranges Crows Telephone Light Sugar" print "Wait there's not 10 things in that list, let's fix that." stuff = ten_things.split(' ') more_stuff = [ "Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy" ] # If < 10 is better idea? while len(stuff) != 10: next_one = more_stuff.pop() print "Adding: ", next_one stuff.append(next_one) print "There is %d items now." % len(stuff) print "There we go: ", stuff print "Let's do some things with stuff." print stuff[1] # print the 2nd item print stuff[-1] # print the last item print stuff.pop() # pop up the last item
#!/usr/bin/env python import ex_name ex_name.title("ex32: Loops and Lists") #Codes in the book (some words may be changed): # the_count = [1, 2, 3, 4, 5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for-loop goes thru a list. for number in the_count: print "This is count %d" % number # same as above. for fruit in fruits: print "A fruit of type: %s" % fruit # also we can go through mixed lists too # notice we have to use %r since we dont know what's in it. for i in change: print "I got %r" % i # we can also build lists, first start with an empty one elements = [] # then use the range function to do 0 to 5 counts # The range() function only does numbers from the first to the last, not including # the last. So it stops at two, not three in the above. This turns out to be the most common way
#!/usr/bin/python import ex_name ex_name.title("ex21: Functions can return something") # Using "=" and "return" to set variables to be *a value from a function*. #Codes in the book (some words may be changed): # # Return the result of "a + b" to variable. # function ==> variable def add(a, b): print "ADDING %d + %d" % (a, b) return a + b def subtract(a, b): print "SUBTRACTING %d - %d" % (a, b) return a - b def multiply(a, b): print "MULTIPLYING %d * %d" % (a, b) return a * b def divide(a, b): print "DIVIDING %d / %d" % (a, b)
import ex_name ex_name.title("ex36: Designing and Debugging") # My Game.
#!/usr/bin/env python import ex_name # ex18: Names, Variables, Code, Functions ex_name.title("ex18: Names, Variables, Code, Functions") # Functions do: name pieces of code, take arguments and make mini-scripts (or tiny commands). #Codes in the book (some words changed): # def print_two(*args): arg1, arg2 = args print "arg1: %r, arg2: %r" % (arg1, arg2) def print_two_again(arg1, arg2): print "arg1: %r, arg2: %r" % (arg1, arg2) def print_one(arg1): print "arg1: %r" % arg1 def print_none(): print "I got nothing." print "* Output of the code in book, try to write your code and run it to get the following output:" print_two("Hello", "World") print_two_again("Hello", "Again") print_one("One") print_none() #
#!/usr/bin/env python import ex_name ex_name.title("ex34: Accessing Elements of Lists") #Codes in the book (some words may be changed): # animals = ['bear', 'python', 'peacock', 'kangaroo', 'whale', 'platypus'] print "The aninal at 1 is %s." % animals[1] print "The 3rd animal is %s." % animals[2] print "The 1st animal is %s." % animals[0] print "The animal at 3 is %s." % animals[3] print "The 5th animal is %s." % animals[4] print "The animal at 2 is %s." % animals[2] print "The 6th animal is %s." % animals[5] print "The animal at 4 is %s." % animals[4] ##### index = ['1st', '2nd', '3rd', '4th', '5th', '6th'] for i in range(0, 6): print "The %s animal is at %d and is a %s." % (index[i], i, animals[i])
#!/usr/bin/env python import ex_name ex_name.title("ex29: What If") #Codes in the book (some words may be changed): # people = 20 cats = 30 dogs = 15 if people < cats: print "Too many cats! The world is doomed!" if people > cats: print "Not many cats! The world is saved!" if people < dogs: print "The world is drooled on!" if people > dogs: print "The world is dry!" dogs += 5 if people >= dogs: print "People are greater than or equal to dogs." if people <= dogs: print "People are less than or equal to dogs."
#!/usr/bin/env python import ex_name ex_name.title("ex24: More Practice") #Codes in the book (some words may be changed): # print "Let's practice everything." print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.\n' # how about double quotes print "how about double quotes:" print "You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.\n" poem = """ \tThe lovely world with logic so firmly planted cannot discern \n the needs of love nor comprehend passion from intuition and requires an explanation \n\t\twhere there is none. """ print "--------------" print poem print "-" * 14 five = 10 - 2 + 3 - 6 print "This should be five: %s" % five
#!/usr/bin/env python import ex_name ex_name.title("ex39: Dictionaries, Oh Lovely Dictionaries") #Codes in the book (some words may be changed): # ## creating a mapping of state to abbreviation. states = { 'Oregon': 'OR', 'Florida': 'FL', 'California': 'CA', 'New York': 'NY', 'Michigan': 'MI', 'Beijing': 'BJ' } # create a basic set of states and some cities in them. cities = { 'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville', } # add some more cities cities['NY'] = 'New York' cities['OR'] = 'Portland' cities['BJ'] = 'Beijing' # print out some cities
#!/usr/bin/env python import ex_name from sys import argv # ex20: Functions and Files ex_name.title("ex20: Functions and Files") #Codes in the book (some words may be changed): # script, input_file = argv def print_all(f): print f.read() def rewind(f): f.seek(0) def print_a_line(line_count, f): print line_count, f.readline() # Open the test file. current_file = open(input_file) # print "* Output of the code in book, try to write code by yourself and run it to get the following output:" #
#!/usr/bin/env python import ex_name ex_name.title("ex35: Branches and Functions") #Codes in the book (some words may be changed): # from sys import exit def gold_room(): print "This room is full of gold. How much do you take?" next = raw_input(">") if "0" in next or "1" in next: how_much = int(next) else: dead("Man, learn to type a number.") if how_much < 50: print "Nice, you're not greedy, you win!" exit(0) else: dead("You are greedy bastard!") def bear_room(): print "There is bear here." print "The bear has a bunch of honey." print "The fat bear is in front of another door."