Tags

1. Basic

  • Data Types: integers, floats (with decimals), booleans (True/False)
  • Whitespace: indented 4 spaces
  • Comments: start with # similar to R. SAS starts with *  For python, if you want to write multiple lines, use triple quotation marks. “””
  • Arithmetic operations: +, _, *, /, ** (exponential), % (modulo, remainder left over)
  • String-related functions: str(), len(), upper(), lower()  **Attention: use dot notation for upper() and lower(), example: my_string.upper()
  • Advanced print technique:  print “%s” % (my_string)
  • Datetime library:  from datetime import datetime
  • Print out current date and time together in the form mm/dd/yyyy hh:mm:ss:  (\ keeps the print function to read on)

print str(now.month) + “/” + str(now.day) + “/” + str(now.year) +\
‘ ‘ + str(now.hour) + “:” + str(now.minute) + “:” + str(now.second)      

  • Boolean Operators
    —————————
    True and True is True
    True and False is False
    False and True is False
    False and False is False

    True or True is True
    True or False is True
    False or True is True
    False or False is False

    Not True is False
    Not False is True

  •  1. not is evaluated first;   2. and is evaluated next;  3. or is evaluated last;
  • print the result to the second decimal:  print (“%.2f” % numeric_variable)
  • if, elif, and else: remember to use : after the if, elif and else; indented four spaces for return function

 

2. Examples 

(1) Give raw answer and print out raw answer

name = raw_input(“What is your name?”)
quest = raw_input(“What is your quest?”)
color = raw_input(“What is your favorite color?”)

print “Ah, so your name is %s, your quest is %s, ” \
“and your favorite color is %s.” % (name, quest, color) 

(2) Choose 1 side and output the corresponding result

def clinic():
    print “You’ve just entered the clinic!”
    print “Do you take the door on the left or the right?”
    answer = raw_input(“Type left or right and hit ‘Enter’.”).lower()
    if answer == “left” or answer == “l”:
        print “This is the Verbal Abuse Room, you heap of parrot droppings!”
    elif answer == “right” or answer == “r”:
        print “Of course this is the Argument Room, I’ve told you that already!”
    else:
        print “You didn’t pick left or right! Try again.”
        clinic()

clinic() 

 

 

 

 

 

 

 

Advertisements