差别
这里会显示出您选择的修订版和当前版本之间的差别。
后一修订版 | 前一修订版 | ||
public:it:python [2014/09/22 19:16] – 创建 oakfire | public:it:python [2024/01/15 15:33] (当前版本) – [Tips] oakfire | ||
---|---|---|---|
行 1: | 行 1: | ||
====== Python ====== | ====== Python ====== | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | ===== Tips ===== | ||
* 快速开一个静态http服务: | * 快速开一个静态http服务: | ||
+ | * In Terminal, type '' | ||
+ | * python为动态语言, | ||
+ | * python 变快的 9 个技巧:[[https:// | ||
+ | * 拼接字符串的速度, | ||
+ | * Faster List Creation: Use “[]” Over “list()” 用 '' | ||
+ | * Faster Membership Testing: Use a Set Over a List | ||
+ | * Faster Data Generation: Use Comprehensions Over For Loops 用内包代替循环 | ||
+ | * Faster Loops: Prioritize Local Variables 优先使用局部变量 | ||
+ | * Faster Execution: Prioritize Built-In Modules and Libraries 优先用内置模块和库 | ||
+ | * Faster Function Calls: Leverage Cache Decorator for Easy Memoization | ||
+ | * Faster Infinite Loop: Prefer “while 1” Over “while True” 但是现代解释器基本都优化过, '' | ||
+ | * Faster Start-Up: Import Python Modules Smartly | ||
+ | ===== 从零开始 ===== | ||
+ | * [[http:// | ||
+ | * Learning more: | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * Notes:< | ||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex1: Print | ||
+ | # -*- coding: utf-8 -*- | ||
+ | print " | ||
+ | print 'I " | ||
+ | print " | ||
+ | # on terminal, run ex1.py | ||
+ | python ex1.py | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex2: Common | ||
+ | # This is a common | ||
+ | print " | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex3: Numbers and Math | ||
+ | # + - / * % < > <= >= | ||
+ | # more: ** | ||
+ | print " | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex4: Variables And Names | ||
+ | cars = 100 | ||
+ | space_in_a_car = 4.0 | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex5: More Variables and Printing | ||
+ | my_name = ' | ||
+ | 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" % " | ||
+ | print "My name is: ' | ||
+ | my_name = "My name is: %r" | ||
+ | print my_name % ' | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex7: More Printing | ||
+ | print " | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex8: Printing, Printing | ||
+ | formatter = "%r %r %r %r" | ||
+ | print formatter % (1, 2, 3, 4) | ||
+ | print formatter % (" | ||
+ | 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 = " | ||
+ | print "Here are the days: ", days | ||
+ | print "Here are the months: ", months | ||
+ | print """ | ||
+ | There' | ||
+ | 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 "," | ||
+ | #so print doesn' | ||
+ | age = raw_input() | ||
+ | print "So, you're %r old" % age | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex12: Prompting People | ||
+ | age = raw_input(" | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex13: Parameters, Unpacking, Variables | ||
+ | from sys import argv | ||
+ | script, first, second, third = argv | ||
+ | print "The script is called:", | ||
+ | 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(' | ||
+ | # writeline | ||
+ | my_file.open(' | ||
+ | my_file.write(' | ||
+ | |||
+ | # ex17: More files | ||
+ | # Nothing impresses me. | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex18: Names, Variables, Code, Functions | ||
+ | # create a function by using the word ' | ||
+ | def my_function(*args): | ||
+ | arg1, | ||
+ | print " | ||
+ | #or | ||
+ | def print_two_again(arg1, | ||
+ | print "arg1: %r, arg2: %r" % (arg1, arg2) | ||
+ | |||
+ | # ex19: Functions and Variables | ||
+ | # ex20: Functions and Files | ||
+ | # ex21: Functions Can Return Something by " | ||
+ | # 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): | ||
+ | """ | ||
+ | words = stuff.split(' | ||
+ | return words | ||
+ | # usage ex25.py in Terminal: | ||
+ | import ex25 | ||
+ | ex25.break_words(" | ||
+ | help(ex25) # show help info about ex25.py | ||
+ | help(ex25.break_words) # show help info about function break_words in module ex25 | ||
+ | # Typing ' | ||
+ | # do your import like this: 'from ex25 import * ' which is like saying, " | ||
+ | from ex25 import * | ||
+ | break_words(" | ||
+ | |||
+ | # ex26: Congratulations, | ||
+ | # 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 = [' | ||
+ | change = [1, ' | ||
+ | # 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 " | ||
+ | elements.append(i) | ||
+ | # now we can print them out too | ||
+ | for i in elements: | ||
+ | print " | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex33: While Loops | ||
+ | i = 0 | ||
+ | numbers = [] | ||
+ | while i < 6: | ||
+ | numbers.append(i) | ||
+ | i += 1; | ||
+ | for num in numbers: | ||
+ | print num | ||
+ | | ||
+ | # ex34: Accessing Elements of Lists | ||
+ | # Nothing impresses me. | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex35: Branches and Functions | ||
+ | from sys import exit | ||
+ | def gold_room(): | ||
+ | print "This room is full of gold. How much do you take?" | ||
+ | choice = raw_input("> | ||
+ | if " | ||
+ | how_much = int(choice) | ||
+ | else: | ||
+ | dead(" | ||
+ | |||
+ | if how_much < 50: | ||
+ | print "Nice, you're not greedy, you win!" | ||
+ | exit(0) | ||
+ | else: | ||
+ | dead(" | ||
+ | |||
+ | def dead(why): | ||
+ | print why, "Good job!" | ||
+ | exit(0) | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex36: Designing and Debugging | ||
+ | # ex37: Symbol Review | ||
+ | # ex38: Doing Things to Lists | ||
+ | ten_things = " | ||
+ | print "Wait there are not 10 things in that list. Let's fix that." | ||
+ | stuff = ten_things.split(' | ||
+ | more_stuff = [" | ||
+ | while len(stuff) != 10: | ||
+ | next_one = more_stuff.pop() | ||
+ | print " | ||
+ | stuff.append(next_one) | ||
+ | print "There are %d items now." % len(stuff) | ||
+ | print "There we go: ", stuff | ||
+ | print " | ||
+ | print stuff[1] | ||
+ | print stuff[-1] # whoa! fancy | ||
+ | print stuff.pop() | ||
+ | print ' ' | ||
+ | print '#' | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex39: Dictionaries, | ||
+ | >>> | ||
+ | >>> | ||
+ | lucy | ||
+ | >>> | ||
+ | >>> | ||
+ | {1:' | ||
+ | >>> | ||
+ | >>> | ||
+ | {1:' | ||
+ | # | ||
+ | for key, value in stuff.items(): | ||
+ | print "%r : %r" % (key, value) | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex40: Modules, Classes, and Objects | ||
+ | # Modules Are Like Dictionaries | ||
+ | # Classes Are Like Modules | ||
+ | # Objects are Like Import | ||
+ | # Getting Things from Things | ||
+ | # I now have three ways to get things from things: | ||
+ | ## dict style | ||
+ | mystuff[' | ||
+ | ## module style | ||
+ | mystuff.apples() | ||
+ | print mystuff.tangerine | ||
+ | ## class style | ||
+ | thing = MyStuff() | ||
+ | thing.apples() | ||
+ | print thing.tangerine | ||
+ | |||
+ | # A First Class Example: | ||
+ | class Song(object): | ||
+ | def __init__(self, | ||
+ | self.lyrics = lyrics | ||
+ | def sing_me_a_song(self): | ||
+ | for line in self.lyrics: | ||
+ | print line | ||
+ | |||
+ | happy_bday = Song([" | ||
+ | " | ||
+ | " | ||
+ | bulls_on_parade = Song([" | ||
+ | "With pockets full of shells" | ||
+ | happy_bday.sing_me_a_song() | ||
+ | bulls_on_parade.sing_me_a_song() | ||
+ | |||
+ | # ex41: Learning To Speak Object Oriented | ||
+ | # ex42: Is-A, Has-A, Objects, and Classes | ||
+ | super | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex43: Basic Object-Oriented Analysis and Design | ||
+ | # ex44: Inheritance Versus Composition | ||
+ | class Parent(object): | ||
+ | def override(self): | ||
+ | print " | ||
+ | def implicit(self): | ||
+ | print " | ||
+ | def altered(self): | ||
+ | print " | ||
+ | class Child(Parent): | ||
+ | def override(self): | ||
+ | print "CHILD override()" | ||
+ | def altered(self): | ||
+ | print " | ||
+ | super(Child, | ||
+ | print " | ||
+ | |||
+ | dad = Parent() | ||
+ | son = Child() | ||
+ | dad.implicit() | ||
+ | son.implicit() | ||
+ | dad.override() | ||
+ | son.override() | ||
+ | dad.altered() | ||
+ | son.altered() | ||
+ | |||
+ | # multiple inheritance | ||
+ | class SuperFun(Child, | ||
+ | pass | ||
+ | # Using super() with __init__ | ||
+ | class Child(Parent): | ||
+ | def __init__(self, | ||
+ | self.stuff = stuff | ||
+ | super(Child, | ||
+ | | ||
+ | # ex45: You Make a Game | ||
+ | # Nothing impresses me. | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex46: A Project Skeleton | ||
+ | try: | ||
+ | from setuptools import setup | ||
+ | except ImportError: | ||
+ | from distutils.core import setup | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex47: Automated Testing | ||
+ | nosetests | ||
+ | from nose.tools import * | ||
+ | assert_equal | ||
+ | # Test files go in " | ||
+ | |||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex48: Advanced User Input | ||
+ | # A tuple is nothing more than a list that you can't modify. | ||
+ | # It's created by putting data inside two " | ||
+ | first_word = (' | ||
+ | # Exceptions and Numbers | ||
+ | def convert_number(s): | ||
+ | try: | ||
+ | return int(s) | ||
+ | except ValueError: | ||
+ | return None | ||
+ | |||
+ | # ------------------------------------------------------------------------- | ||
+ | # ex49: Making Sentences | ||
+ | # ex50: Your First Website | ||
+ | # ex51: Getting Input from a Browser | ||
+ | # ex52: The Start Of Your Web Game | ||
+ | # Nothing impresses me. | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | |||