# Single line comments start with a number symbol.
""" Multiline strings can be written
using three "s, and are often used
as comments
"""
##多行的文字可以是使用三个 " ,通常被用作评论
####################################################
## 1. Primitive Datatypes and Operators
####################################################
# You have numbers
3 # => 3
# Math is what you would expect
1 + 1 # => 2
8 - 1 # => 7
10 * 2 # => 20
35 / 5 # => 7
# Division is a bit tricky. It is integer division and floors the results
# automatically.
5 / 2 # => 2
# To fix division we need to learn about floats.
2.0 # This is a float
11.0 / 4.0 # => 2.75 ahhh...much better
# Result of integer division truncated down both for positive and negative.
5 // 3 # => 1 输出不大于结果的整数,浮点数也满足
5.0 // 3.0 # => 1.0 # works on floats too
-5 // 3 # => -2
-5.0 // 3.0 # => -2.0
# Note that we can also import division module(Section 6 Modules)
# to carry out normal division with just one '/'.
from __future__ import division
11/4 # => 2.75 ...normal division 在from _future_ import division的表示下,/ 表示输出浮点数,// 输出不大于结果的整数
11//4 # => 2 ...floored division
# Modulo operation
7 % 3 # => 1
# Exponentiation (x to the yth power)
2**4 # => 16 阶乘
# Enforce precedence with parentheses
(1 + 3) * 2 # => 8
# Boolean Operators
# Note "and" and "or" are case-sensitive
True and False #=> False
False or True #=> True
# Note using Bool operators with ints
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True
2 == True #=> False
1 == True #=> True
# negate with not
not True # => False
not False # => True
# Equality is ==
1 == 1 # => True
2 == 1 # => False
# Inequality is !=
1 != 1 # => False
2 != 1 # => True
# More comparisons
1 < 10 # => True
1 > 10 # => False
2 <= 2 # => True
2 >= 2 # => True
# Comparisons can be chained! 比较表达式是可以链接的
1 < 2 < 3 # => True
2 < 3 < 2 # => False
# Strings are created with " or ' 字符串的创建可以使用 单引号或者双引号
"This is a string."
'This is also a string.'
# Strings can be added too! 字符串的连接
"Hello " + "world!" # => "Hello world!"
# Strings can be added without using '+'
"Hello " "world!" # => "Hello world!"
# ... or multiplied 多次输出相同的内容
"Hello" * 3 # => "HelloHelloHello"
# A string can be treated like a list of characters 字符串可以被看做 字符的联表
"This is a string"[0] # => 'T'
#String formatting with % 字符串的格式化
#Even though the % string operator will be deprecated on Python 3.1 and removed
#later at some time, it may still be good to know how it works.
x = 'apple' #单引号用来单个单词,双引号用来句子
y = 'lemon'
z = "The items in the basket are %s and %s" % (x,y)
# A newer way to format strings is the format method.
# This method is the preferred way
"{} is a {}".format("This", "placeholder")
"{0} can be {1}".format("strings", "formatted")
# You can use keywords if you don't want to count.
"{name} wants to eat {food}".format(name="Bob", food="lasagna")
# None is an object None 是一个特殊存在
None # => None
# Don't use the equality "==" symbol to compare objects to None
# Use "is" instead 用is来比较
"etc" is None # => False
None is None # => True
# The 'is' operator tests for object identity. This isn't
# very useful when dealing with primitive values, but is
# very useful
Learn Python in Y Minutes(有时间再慢慢翻译)
最新推荐文章于 2023-05-18 10:15:00 发布