#!/usr/bin/python # Required header that tells the browser how to render the text. print "Content-Type: text/plain\n" # Print a simple message to the display window. print "Hello, World!\n" #!/usr/bin/python # Print the required header that tells the browser how to render the text. print "Content-Type: text/plain\n" # Initialize a few variables. first = 5 middle = 7.5 last = 10 # Print the values. print "first:", first print "middle:", middle print "last:", last #!/usr/bin/python # Print the required header that tells the browser how to render the text. print "Content-Type: text/plain\n" # Initialize a few variables. first, middle, last = 5, 7.5, 10 # Print the values. print "first:", first print "middle:", middle print "last:", last, "\n" # Swap the values. first, last = last, first # Print the values. print "first:", first print "last:", last, "\n" # Right associative assignment. first = last = middle # Print the values. print "first:", first print "middle:", middle print "last:", last #!/usr/bin/python # Print the required header that tells the browser how to render the text. print "Content-Type: text/plain\n" # Integer division resulting in a truncated value. a = 5/2 # Print the value. print "5/2 =", a # Decimal division. a = 5/2.0 # Print the value. print "5/2.0 =", a, "\n" # String addition and multiplication. python_words = "python is" + 3*" really" + " neat." # Print the string. print python_words, "\n" # Print parts of the string. print "The first character is:", python_words[0] print "The last character is:", python_words[-1] print "In the middle is: \n", python_words[1:-1] #!/usr/bin/python # Print the required header that tells the browser how to render the text. print "Content-Type: text/plain\n" # Initialize a few variables. first_name = "Louis" last_name = "" # Empty String age = 21.5 # Test for name. if (first_name and last_name): print "Hello,", first_name, last_name print "It is good to see you, Mr.", last_name elif first_name: print "Hello,", first_name else: print "Who are you?" # Test for age. if (age >= 21): print "You can drink!" else: print "No drinks for you!" #!/usr/bin/python # Print the required header that tells the browser how to render the text. print "Content-Type: text/plain\n" # Initialize a counting variable. counter = 1 # Print the even numbers from 1 to 10. while (counter <= 10): if ((counter%2) == 0): print counter counter = counter +1 print "Finished." #!/usr/bin/python # Print the required header that tells the browser how to render the text. print "Content-Type: text/plain\n" # List of integers. numbers = [0, 1, 2] # Print iterations using numbers. for count in numbers: print "iteration", count # Print iterations using range(3). for count in range(3): print "iteration", count #!/usr/bin/python # Print the required header that tells the browser how to render the text. print "Content-Type: text/plain\n\n" # Define a function with default argument 'age' = 21. def display_vars (first_name, last_name, age = 21): print (first_name, last_name, age) # Call the function with all positionals. display_vars("John", "Smith", 22) # Call the function with all keywords. display_vars(last_name = "Smith", first_name = "John", age = 22) # Call the function with incomplete args; default age used. display_vars("John", "Smith") #!/usr/bin/python # Print the required header that tells the browser how to render the text. print "Content-Type: text/plain\n\n" # Define a global variable gender = "female". gender = "female" # Define a function. def display_vars (first_name, last_name, age): print (first_name, last_name, age, gender) # Define a function that calls 'display_vars' with intended local 'gender'. def go_display(): gender = "male" display_vars("John", "Smith", 22) # Call the function 'go_display'. go_display() #!/usr/bin/python # Print the required header that tells the browser how to render the text. print "Content-Type: text/plain\n\n" # Import the outside class. from exclass import simple_data # Use default constructors to create 'simple_data' objects. store = simple_data() item = simple_data() # Assign them values. store.set_name("Super Store") item.set_name("Month supply of tissue") # Print the values. store.display_name() item.display_name() exclass.py: # Define the class object class simple_data: # Define member functions def set_name(self, info): self.name = info def display_name(self): print self.name