Basic data types (a). Primitive data types and operators

Python by Guido van Rossum (Guido Van Rossum) designed in the early 1990s. It is now one of the most popular programming languages. Its syntax is simple and beautiful, almost pseudo-code executable.

Note: This tutorial is written based on Python 3. Source code download: https://learnxinyminutes.com/docs/files/learnpython3-cn.py

#用井字符开头的是单行注释
""" 多行字符串用三个引号
 包裹,也常被用来做多
 行注释
"""

1. Original data types and operators

# 整数
3 # => 3
# 算术没有什么出乎意料的
1 + 1 # => 2
8 - 1 # => 7
10 * 2 # => 20
# 但是除法例外,会自动转换成浮点数
35 / 5 # => 7.0
5 / 3 # => 1.6666666666666667
# 整数除法的结果都是向下取整
5 // 3 # => 1
5.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
# 布尔值
True
False
# 用not取非
not True # => False
not False # => True
# 逻辑运算符,注意and和or都是小写
True and False # => False
False or True # => True
# 整数也可以当作布尔值
0 and 2 # => 0
-5 or 0 # => -5
0 == False # => True
2 == True # => False
1 == True # => 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
# 字符串用单引双引都可以
"这是个字符串"
 这也是个字符串 
# 用加号连接字符串
"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 # => False
None is None # => True
# None,0,空字符串,空列表,空字典都算是False
# 所有其他值都是True

bool(0) # => False
bool("") # => False
bool([]) # => False
bool({}) # => False
Past Articles

The basic data types (six or seven): module, advanced usage
of basic data types (five): class
basic data types (four) function
basic data types (c) process control and iterators
basic data types (two) and a set of variables

Subsequent updates more, if you like I can look oh ~

Guess you like

Origin blog.csdn.net/wsh8906/article/details/93633111