Python3 原始数据类型和运算符

# 整数
    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
# 求余%
    10 % 3 # => 1 # 浮点数的运算结果也是浮点数     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 # 字符串用单引双引都可以     "这是个字符串"     '这也是个字符串'
# 用转义字符\来进行字符串的转义
    'I\'m \"OK\"!' # => I'm "OK"!
# \n表示换行,\t表示制表符,\\表示\本身
    print("\\\t\\") # => \    \
# 原始字符串。r"" 表示""内的字符串不转义
    print(r"\\\t\\") # => \\\t\\
# 用加号连接字符串     "Hello " + "world!" # => "Hello world!" # 字符串可以被当作字符列表     "This is a string"[0] # => 'T' # 用.format来格式化字符串     "{} can be {}".format("strings", "interpolated") # => 'strings can be 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") # => 'strings can be interpolated the old way' # 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

 

posted on 2017-05-30 21:35  cvn  阅读(155)  评论(0编辑  收藏  举报

导航