差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
| public:it:python [2014/12/03 16:27] – [从零开始] oakfire | public:it:python [2024/01/15 15:33] (当前版本) – [Tips] oakfire | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== Python ====== | ====== Python ====== | ||
| * [[https:// | * [[https:// | ||
| + | * [[http:// | ||
| ===== Tips ===== | ===== Tips ===== | ||
| * 快速开一个静态http服务: | * 快速开一个静态http服务: | ||
| * In Terminal, type '' | * 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:// | + | * [[http:// |
| - | * <code python> | + | * Learning more: |
| + | * [[http:// | ||
| + | * [[https:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[https:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * Notes:<code python> | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex1: Print | # ex1: Print | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| 行 15: | 行 42: | ||
| python ex1.py | python ex1.py | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex2: Common | # ex2: Common | ||
| # This is a common | # This is a common | ||
| print " | print " | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex3: Numbers and Math | # ex3: Numbers and Math | ||
| # + - / * % < > <= >= | # + - / * % < > <= >= | ||
| 行 24: | 行 53: | ||
| print " | print " | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex4: Variables And Names | # ex4: Variables And Names | ||
| cars = 100 | cars = 100 | ||
| space_in_a_car = 4.0 | space_in_a_car = 4.0 | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex5: More Variables and Printing | # ex5: More Variables and Printing | ||
| my_name = ' | my_name = ' | ||
| 行 34: | 行 65: | ||
| print "My name is %s, i am %d old." % ( my_name, my_age ) | print "My name is %s, i am %d old." % ( my_name, my_age ) | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex6: Strings and Text | # ex6: Strings and Text | ||
| print "My name is: %r" % " | print "My name is: %r" % " | ||
| 行 40: | 行 72: | ||
| print my_name % ' | print my_name % ' | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex7: More Printing | # ex7: More Printing | ||
| print " | print " | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex8: Printing, Printing | # ex8: Printing, Printing | ||
| formatter = "%r %r %r %r" | formatter = "%r %r %r %r" | ||
| 行 50: | 行 84: | ||
| print formatter % (formatter, formatter, formatter, formatter) | print formatter % (formatter, formatter, formatter, formatter) | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex9: Printing, Printing, Printing | # ex9: Printing, Printing, Printing | ||
| # Here's some new strange stuff, remember type it exactly. | # Here's some new strange stuff, remember type it exactly. | ||
| 行 62: | 行 97: | ||
| Even 4 lines if we want, or 5, or 6. | Even 4 lines if we want, or 5, or 6. | ||
| """ | """ | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex10 | # ex10 | ||
| print "\t \\ \" ' | print "\t \\ \" ' | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex11: Asking Questions | # ex11: Asking Questions | ||
| - | print "How old are you?", # We put a "," | + | print "How old are you?", # We put a "," |
| + | #so print doesn' | ||
| age = raw_input() | age = raw_input() | ||
| print "So, you're %r old" % age | print "So, you're %r old" % age | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex12: Prompting People | # ex12: Prompting People | ||
| age = raw_input(" | age = raw_input(" | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex13: Parameters, Unpacking, Variables | # ex13: Parameters, Unpacking, Variables | ||
| from sys import argv | from sys import argv | ||
| 行 84: | 行 124: | ||
| # Nothing impresses me. | # Nothing impresses me. | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex15: Reading Files | # ex15: Reading Files | ||
| print "Type the filename :" | print "Type the filename :" | ||
| 行 90: | 行 131: | ||
| print txt_again.read() | print txt_again.read() | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex16: Reading and Writing Files | # ex16: Reading and Writing Files | ||
| # class file's function: | # class file's function: | ||
| 行 105: | 行 147: | ||
| # Nothing impresses me. | # Nothing impresses me. | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex18: Names, Variables, Code, Functions | # ex18: Names, Variables, Code, Functions | ||
| # create a function by using the word ' | # create a function by using the word ' | ||
| 行 122: | 行 165: | ||
| # Nothing impresses me. | # Nothing impresses me. | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex25: Even More Practice | # ex25: Even More Practice | ||
| # in ex25.py file: | # in ex25.py file: | ||
| 行 133: | 行 177: | ||
| help(ex25) # show help info about ex25.py | help(ex25) # show help info about ex25.py | ||
| help(ex25.break_words) # show help info about function break_words in module ex25 | help(ex25.break_words) # show help info about function break_words in module ex25 | ||
| - | # Typing ' | + | # Typing ' |
| + | # do your import like this: 'from ex25 import * ' which is like saying, " | ||
| from ex25 import * | from ex25 import * | ||
| break_words(" | break_words(" | ||
| 行 140: | 行 185: | ||
| # Nothing. | # Nothing. | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex27: Memorizing Logic | # ex27: Memorizing Logic | ||
| # terms: | # terms: | ||
| 行 147: | 行 193: | ||
| # Nothing impresses me. | # Nothing impresses me. | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex29: What If | # ex29: What If | ||
| # ex30: Else and If | # ex30: Else and If | ||
| 行 159: | 行 206: | ||
| print "We can't decide." | print "We can't decide." | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex32: Loops and Lists | # ex32: Loops and Lists | ||
| the_count = [1, 2, 3, 4, 5] | the_count = [1, 2, 3, 4, 5] | ||
| 行 182: | 行 230: | ||
| print " | print " | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex33: While Loops | # ex33: While Loops | ||
| i = 0 | i = 0 | ||
| 行 194: | 行 243: | ||
| # Nothing impresses me. | # Nothing impresses me. | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex35: Branches and Functions | # ex35: Branches and Functions | ||
| from sys import exit | from sys import exit | ||
| 行 214: | 行 264: | ||
| exit(0) | exit(0) | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex36: Designing and Debugging | # ex36: Designing and Debugging | ||
| # ex37: Symbol Review | # ex37: Symbol Review | ||
| 行 234: | 行 285: | ||
| print '#' | print '#' | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex39: Dictionaries, | # ex39: Dictionaries, | ||
| >>> | >>> | ||
| 行 248: | 行 300: | ||
| print "%r : %r" % (key, value) | print "%r : %r" % (key, value) | ||
| + | # ------------------------------------------------------------------------- | ||
| # ex40: Modules, Classes, and Objects | # ex40: Modules, Classes, and Objects | ||
| # Modules Are Like Dictionaries | # Modules Are Like Dictionaries | ||
| 行 281: | 行 334: | ||
| # ex41: Learning To Speak Object Oriented | # 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. | ||
| + | |||
| </ | </ | ||