以一段代码为实例快速入门Python2.7
Python由Guido Van Rossum发明于90年代初期,是目前最流行的编程语言之一,因其语法的清晰简洁我爱上了Python,其代码基本上可以 说是可执行的伪代码。
非常欢迎反馈!你可以通过推特@louiedinh或louiedinh AT gmail联系我。
备注:本文是专门针对Python 2.7的,但应该是适用于Python 2.x的。很快我也会为Python 3写这样的一篇文章!
# 单行注释以井字符开头 """ 我们可以使用三个双引号(")或单引号(') 来编写多行注释 """ ########################################################## ## 1. 基本数据类型和操作符 ########################################################## # 数字 3 #=> 3 # 你预想的数学运算 1 + 1 #=> 2 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 # 除法略显诡异。整数相除会自动向下取小于结果的最大整数 11 / 4 #=> 2 # 还有浮点数和浮点数除法(译注:除数和被除数两者至少一个为浮点数,结果才会是浮点数) 2.0 # 这是一个浮点数 5.0 / 2.0 #=> 2.5 额...语法更明确一些 # 使用括号来强制优先级 (1 + 3) * 2 #=> 8 # 布尔值也是基本类型数据 True False # 使用not来求反 not True #=> False not False #=> True # 相等比较使用== 1 == 1 #=> True 2 == 1 #=> False # 不相等比较使用!= 1 != 1 #=> False 2 != 1 #=> True # 更多的比较方式 1 < 10 #=> True 1 > 10 #=> False 2 <= 2 #=> True 2 >= 2 #=> True # 比较操作可以串接! 1 < 2 < 3 #=> True 2 < 3 < 2 #=> False # 可以使用"或'创建字符串 "This is a string." 'This is also a string.' # 字符串也可以相加! "Hello " + "world!" #=> "Hello world!" # 字符串可以看作是一个字符列表 "This is a string"[0] #=> 'T' # None是一个对象 None #=> None #################################################### ## 2. 变量与数据容器 #################################################### # 打印输出非常简单 print "I'm Python. Nice to meet you!" # 赋值之前不需要声明变量 some_var = 5 # 约定使用 小写_字母_和_下划线 的命名方式 some_var #=> 5 # 访问之前未赋值的变量会产生一个异常 try: some_other_var except NameError: print "Raises a name error" # 赋值时可以使用条件表达式 some_var = a if a > b else b # 如果a大于b,则将a赋给some_var, # 否则将b赋给some_var # 列表用于存储数据序列 li = [] # 你可以一个预先填充的列表开始 other_li = [4, 5, 6] # 使用append将数据添加到列表的末尾 li.append(1) #li现在为[1] li.append(2) #li现在为[1, 2] li.append(4) #li现在为[1, 2, 4] li.append(3) #li现在为[1, 2, 4, 3] # 使用pop从列表末尾删除数据 li.pop() #=> 3,li现在为[1, 2, 4] # 把刚刚删除的数据存回来 li.append(3) # 现在li再一次为[1, 2, 4, 3] # 像访问数组一样访问列表 li[0] #=> 1 # 看看最后一个元素 li[-1] #=> 3 # 越界访问会产生一个IndexError try: li[4] # 抛出一个IndexError异常 except IndexError: print "Raises an IndexError" # 可以通过分片(slice)语法来查看列表中某个区间的数据 # 以数学角度来说,这是一个闭合/开放区间 li[1:3] #=> [2, 4] # 省略结束位置 li[2:] #=> [4, 3] # 省略开始位置 li[:3] #=> [1, 2, 4] # 使用del从列表中删除任意元素 del li[2] #li现在为[1, 2, 3] # 列表可以相加 li + other_li #=> [1, 3, 3, 4, 5, 6] - 注意:li和other_li并未改变 # 以extend来连结列表 li.extend(other_li) # 现在li为[1, 2, 3, 4, 5, 6] # 以in来检测列表中是否存在某元素 1 in li #=> True # 以len函数来检测列表长度 len(li) #=> 6 # 元组类似列表,但不可变 tup = (1, 2, 3) tup[0] #=> 1 try: tup[0] = 3 # 抛出一个TypeError异常 except TypeError: print "Tuples cannot be mutated." # 可以在元组上使用和列表一样的操作 len(tup) #=> 3 tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6) tup[:2] #=> (1, 2) 2 in tup #=> True # 可以将元组解包到变量 a, b, c = (1, 2, 3) # 现在a等于1,b等于2,c等于3 # 如果你省略括号,默认也会创建元组 d, e, f = 4, 5, 6 # 看看两个变量互换值有多简单 e, d = d, e #现在d为5,e为4 # 字典存储映射关系 empty_dict = {} # 这是一个预先填充的字典 filled_dict = {"one": 1, "two": 2, "three": 3} # 以[]语法查找值 filled_dict['one'] #=> 1 # 以列表形式获取所有的键 filled_dict.keys() #=> ["three", "two", "one"] # 注意 - 字典键的顺序是不确定的 # 你的结果也许和上面的输出结果并不一致 # 以in来检测字典中是否存在某个键 "one" in filled_dict #=> True 1 in filled_dict #=> False # 试图使用某个不存在的键会抛出一个KeyError异常 filled_dict['four'] #=> 抛出KeyError异常 # 使用get方法来避免KeyError filled_dict.get("one") #=> 1 filled_dict.get("four") #=> None # get方法支持一个默认参数,不存在某个键时返回该默认参数值 filled_dict.get("one", 4) #=> 1 filled_dict.get("four", 4) #=> 4 # setdefault方法是一种添加新的键-值对到字典的安全方式 filled_dict.setdefault("five", 5) #filled_dict["five"]设置为5 filled_dict.setdefault("five", 6) #filled_dict["five"]仍为5 # 集合 empty_set = set() # 以几个值初始化一个集合 filled_set = set([1, 2, 2, 3, 4]) # filled_set现为set([1, 2, 3, 4, 5]) # 以&执行集合交运算 other_set = set([3, 4, 5, 6]) filled_set & other_set #=> set([3, 4, 5]) # 以|执行集合并运算 filled_set | other_set #=> set([1, 2, 3, 4, 5, 6]) # 以-执行集合差运算 set([1, 2, 3, 4]) - set([2, 3, 5]) #=> set([1, 4]) # 以in来检测集合中是否存在某个值 2 in filled_set #=> True 10 in filled_set #=> False #################################################### ## 3. 控制流程 #################################################### # 创建个变量 some_var = 5 # 以下是一个if语句。缩进在Python是有重要意义的。 # 打印 "some_var is smaller than 10" if some_var > 10: print "some_var is totally bigger than 10." elif some_var < 10: print "some_var is smaller than 10." else: print "some_var is indeed 10." """ For循环在列表上迭代 输出: dog is a mammal cat is a mammal mouse is a mammal """ for animal in ["dog", "cat", "mouse"]: # 可以使用%来插补格式化字符串 print "%s is a mammal" % animal """ while循环直到未满足某个条件。 输出: 0 1 2 3 """ x = 0 while x < 4: print x x += 1 # x = x + 1的一种简写 # 使用try/except块来处理异常 # 对Python 2.6及以上版本有效 try: # 使用raise来抛出一个错误 raise IndexError("This is an index error") except IndexError as e: pass # pass就是什么都不干。通常这里用来做一些恢复工作 # 对于Python 2.7及以下版本有效 try: raise IndexError("This is an index error") except IndexError, e: # 没有"as",以逗号替代 pass #################################################### ## 4. 函数 #################################################### # 使用def来创建新函数 def add(x, y): print "x is %s and y is %s" % (x, y) return x + y # 以一个return语句来返回值 # 以参数调用函数 add(5, 6) #=> 11 并输出 "x is 5 and y is 6" # 另一种调用函数的方式是关键字参数 add(x=5, y=6) # 关键字参数可以任意顺序输入 # 可定义接受可变数量的位置参数的函数 def varargs(*args): return args varargs(1, 2, 3) #=> (1, 2, 3) # 也可以定义接受可变数量关键字参数的函数 def keyword_args(**kwargs): return kwargs # 调用一下该函数看看会发生什么 keyword_args(big="foot", loch="ness") #=> {"big": "foo", "loch": "ness"} # 也可以一次性接受两种参数 def all_the_args(*args, **kwargs): print args print kwargs """ all_the_args(1, 2, a=3, b=4)输出: [1, 2] {"a": 3, "b": 4} """ # 在调用一个函数时也可以使用*和** args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} foo(*args) #等价于foo(1, 2, 3, 4) foo(**kwargs) # 等价于foo(a=3, b=4) foo(*args, **kwargs) # 等价于foo(1, 2, 3, 4, a=3, b=4) # Python的函数是一等函数 def create_adder(x): def adder(y): return x + y return adder add_10 = create_adder(10) add_10(3) #=> 13 # 也有匿名函数 (lamda x: x > 2)(3) #=> True # 有一些内置的高阶函数 map(add_10, [1, 2, 3]) #=> [11, 12, 13] filter(lamda x: x > 5, [3, 4, 5, 6, 7]) #=>[6, 7] # 可以使用列表推导来实现映射和过滤 [add_10(i) for i in [1, 2, 3]] #=> [11, 13, 13] [x for x in [3, 4, 5, 6,7 ] if x > 5] #=> [6, 7] #################################################### ## 5. 类 #################################################### # 创建一个子类继承自object来得到一个类 class Human(object): # 类属性。在该类的所有示例之间共享 species = "H. sapiens" # 基本初始化构造方法 def __init__(self, name): # 将参数赋值给实例的name属性 self.name = name # 实例方法。所有示例方法都以self为第一个参数 def say(self, msg): return "%s: %s" % (self.name, msg) # 类方法由所有实例共享 # 以调用类为第一个参数进行调用 @classmethod def get_species(cls): return cls.species # 静态方法的调用不需要一个类或实例的引用 @staticmethod def grunt(): return "*grunt*" # 实例化一个类 i = Human(name="Ian") print i.say("hi") # 输出"Ian: hi" j = Human("Joel") print j.say("hello") # 输出"Joel: hello" # 调用类方法 i.get_species() #=> "H. sapiens" # 修改共享属性 Human.species = "H. neanderthalensis" i.get_species() #=> "H. neanderthalensis" j.get_species() #=> "H. neanderthalensis" # 调用静态方法 Human.grunt() #=> "*grunt*" {% endhighlight %}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.
