Python 是由吉多·范罗苏姆(Guido Van Rossum)在 90 年代早期设计。 它是如今最常用的编程语言之一。它的语法简洁且优美,几乎就是可执行的伪代码。
欢迎大家斧正。英文版原作 Louie Dinh @louiedinh 邮箱 louiedinh [at] [谷歌的信箱服务]。中文翻译 Geoff Liu。
注意:这篇教程是基于 Python 3 写的。如果你想学旧版 Python 2,我们特别有另一篇教程。
# 用井字符开头的是单行注释""" 多行字符串用三个引号 包裹,也常被用来做多 行注释"""
1. 原始数据类型和运算符
# 整数3 # => 3# 算术没有什么出乎意料的1 + 1 # => 28 - 1 # => 710 * 2 # => 20# 但是除法例外,会自动转换成浮点数35 / 5 # => 7.05 / 3 # => 1.6666666666666667# 整数除法的结果都是向下取整5 // 3 # => 15.0 // 3.0 # => 1.0 # 浮点数也可以-5 // 3 # => -2-5.0 // 3.0 # => -2.0# 浮点数的运算结果也是浮点数3 * 2.0 # => 6.0# 模除7 % 3 # => 1# x的y次方2**4 # => 16# 用括号决定优先级(1 + 3) * 2 # => 8# 布尔值TrueFalse# 用not取非not True # => Falsenot False # => True# 逻辑运算符,注意and和or都是小写True and False # => FalseFalse or True # => True# 整数也可以当作布尔值0 and 2 # => 0-5 or 0 # => -50 == False # => True2 == True # => False1 == True # => True# 用==判断相等1 == 1 # => True2 == 1 # => False# 用!=判断不等1 != 1 # => False2 != 1 # => True# 比较大小1 < 10 # => True1 > 10 # => False2 <= 2 # => True2 >= 2 # => True# 大小比较可以连起来!1 < 2 < 3 # => True2 < 3 < 2 # => False# 字符串用单引双引都可以"这是个字符串"'这也是个字符串'# 用加号连接字符串"Hello " + "world!" # => "Hello world!"# 字符串可以被当作字符列表"This is a string"[0] # => 'T'# 用.format来格式化字符串"{} can be {}".format("strings", "interpolated")# 可以重复参数以节省时间"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick")# => "Jack be nimble, Jack be quick, Jack jump over the candle stick"# 如果不想数参数,可以用关键字"{name} wants to eat {food}".format(name="Bob", food="lasagna") # => "Bob wants to eat lasagna"# 如果你的Python3程序也要在Python2.5以下环境运行,也可以用老式的格式化语法"%s can be %s the %s way" % ("strings", "interpolated", "old")# None是一个对象None # => None# 当与None进行比较时不要用 ==,要用is。is是用来比较两个变量是否指向同一个对象。"etc" is None # => FalseNone is None # => True# None,0,空字符串,空列表,空字典都算是False# 所有其他值都是Truebool(0) # => Falsebool("") # => Falsebool([]) # => Falsebool({}) # => False
新闻热点
疑难解答