public:it:python

这是本文档旧的修订版!


Python

  • 快速开一个静态http服务: 进入文件夹, 然后 python -m SimpleHTTPServer 80
  • In Terminal, type pydoc raw_input Read what it says about “raw_input”. If you're on Windows try python -m pydoc raw_input instead. Get out of pydoc by typing q to quit.
  • # ex1: Print
    # -*- coding: utf-8 -*-
    print "I'd much rather you 'not'."
    print 'I "said" do not touch this.'
    print "世界,你好"
    # on terminal, run ex1.py
    python ex1.py
     
    # ex2: Common
    # This is a common
    print "common" #This is a common too.
     
    # ex3: Numbers and Math
    # + - / * % < > <= >=
    # more: **
    print "Result is ", 2*2-3/1, "+", 7%6,"=", 2, "is", 2>1, 2**8
     
    # ex4: Variables And Names
    cars = 100
    space_in_a_car = 4.0
     
    # ex5: More Variables and Printing
    my_name = 'lucy'
    my_age = 16
    print "My name is %s." % my_name
    print "My name is %s, i am %d old." % ( my_name, my_age )
     
    # ex6: Strings and Text
    print "My name is: %r" % "lucy"  # Print "My name is:'lucy'".
    print "My name is: '%s'" % "lucy"  # Print the same.
    my_name = "My name is: %r"
    print my_name % 'lucy' # Print the same.
     
    # ex7: More Printing
    print "a" + "b" + "c" * 5 # Print "abccccc"
     
    # ex8: Printing, Printing
    formatter = "%r %r %r %r"
    print formatter % (1, 2, 3, 4)
    print formatter % ("one", "two", "three", "four")
    print formatter % (True, False, False, True)
    print formatter % (formatter, formatter, formatter, formatter)
     
    # ex9: Printing, Printing, Printing
    # Here's some new strange stuff, remember type it exactly.
    days = "Mon Tue Wed Thu Fri Sat Sun"
    months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
    print "Here are the days: ", days
    print "Here are the months: ", months
    print """
    There's something going on here.
    With the three double-quotes.
    We'll be able to type as much as we like.
    Even 4 lines if we want, or 5, or 6.
    """
    # ex10
    print "\t \\ \" '\"' "
     
    # ex11: Asking Questions
    print "How old are you?", # We put a "," (comma) at the end of each print line. This is so print doesn't end the line with a newline character and go to the next line.
    age = raw_input()
    print "So, you're %r old" % age
     
    # ex12: Prompting People
    age = raw_input("How old are you ?")
     
    # ex13: Parameters, Unpacking, Variables
    from sys import argv
    script, first, second, third = argv
    print "The script is called:", script
    print "Your first variable is:", first
    print "Your second variable is:", second
    print "Your third variable is:", third
     
    # ex14: Prompting and Passing
    # Nothing impresses me.
     
    # ex15: Reading Files
    print "Type the filename :"
    filename= raw_input("> ")
    txt= open(filename)
    print txt_again.read()
     
    # ex16: Reading and Writing Files
    # class file's function:
    # close : like save
    # read: 
    # seek:
    # readline -- Reads just one line of a text file.
    # truncate -- Empties the file. Watch out if you care about the file.
    # write('stuff')
    # writeline
    my_file.open('tmp.txt', 'w')
    my_file.write('first line \n')
     
    # ex17: More files
    # Nothing impresses me.
     
    # ex18: Names, Variables, Code, Functions
    # create a function by using the word 'def'
    def my_function(*args):
        arg1,arg2=args
        print "arg1:%r, arg2:%r" % (arg1, arg2)
    #or
    def print_two_again(arg1, arg2):
        print "arg1: %r, arg2: %r" % (arg1, arg2)
     
    # ex19: Functions and Variables 
    # ex20: Functions and Files
    # ex21: Functions Can Return Something by "return"
    # ex22: What Do You Know So Far?
    # ex23: Read Some Code
    # ex24: More Practice
    # Nothing impresses me.
     
    # ex25: Even More Practice
    # in ex25.py file:
    def break_words(stuff):
        """This function will break up words for us."""
        words = stuff.split(' ')
        return words
    # usage ex25.py in Terminal:
    import ex25
    ex25.break_words("Hello world") # call function 'break_words' in ex25.py
    help(ex25) # show help info about ex25.py
    help(ex25.break_words) # show help info about function break_words in module ex25
    # Typing 'ex25' is annoying. A shortcut is to do your import like this: 'from ex25 import * ' which is like saying, "Import everything from ex25."
    from ex25 import *
    break_words("Hello world")
     
    # ex26: Congratulations, Take a Test!
    # Nothing.
     
    # ex27: Memorizing Logic
    # terms:
    and, or, not, !=, ==, >=, <=, True, False
     
    # ex28: Boolean Practice
    # Nothing impresses me.
     
    # ex29: What If
    # ex30: Else and If
    # ex31: Making Decisions
    people = 20
    cats = 30
    if cars > people:
        print "We should take the cars."
    elif cars < people:
        print "We should not take the cars."
    else:
        print "We can't decide."
     
    # ex32: Loops and Lists
    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 through a list
    for number in the_count:
        print "This is count %d" % number
    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 don't 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
    for i in range(0, 6):
        print "Adding %d to the list." % i
        elements.append(i)
    # now we can print them out too
    for i in elements:
        print "Element was: %d" % i
     
    # ex33: While Loops
    i = 0
    numbers = []
    while i < 6:
        numbers.append(i)
        i += 1;
    for num in numbers:
        print num
  • public/it/python.1417591652.txt.gz
  • 最后更改: 2014/12/03 15:27
  • oakfire