代码是现实世界事物在计算机世界的映射。
写代码是将现实世界的事物用计算机语言描述。
Number 数字(整数int,浮点数float,复数), 其他语言 单精度float, 双精度double; 其他语言:short、lang、int。python的数字除了有int、float类型之外,它还有complex类型(复数)。
2进制要在数字的前面加上0b
print(0b10) # 2
print(0b11) # 3
8进制要在数字的前面加上0o
print(0o10) # 8
print(0o11) # 9
16进制要在数字的前面加上0x
print(0x10) # 16
print(0x11) # 17
python自带的 bin() 函数实现其他进制转换成二进制
print(bin(10)) # 0b1010
print(bin(0o7)) # 0b111
print(bin(0xE)) # 0b1110
python自带的 int() 函数实现其他进制转换成十进制
print(int(0b111)) # 7
print(int(0o77)) # 63
python自带的 hex() 函数实现其他进制转换成16进制
print(hex(88)) # 0x58
print(hex(0o7777)) # 0xfff
python自带的 oct() 函数实现其他进制转换成8进制
print(oct(0b111)) # 0o7
print(oct(0x777)) # 0o3567
其实布尔类型可以说是数字类型的一种.
print(bool(1)) # True
print(bool(2)) # True
print(bool(2.2)) # True
print(bool(-1)) # True
print(bool(0)) # False
print(bool("abc")) # True
print(bool("")) # False
print(bool([1, 2, 3])) # True
print(bool([])) # False
print(bool({1, 2, 3})) # True
print(bool({})) # False
print(bool(None)) # True
str字符串,如何表示字符串?单引号,双引号,三引号
print(type('hello world')) #
print(type("hello world")) #
print(type("Let's go")) #
print(type('Let"s go')) #
上面代码可以看出,单引号或者双引号在表示字符串的时候必须是成对出现的!
如果非得不成对出现,可以添加反斜线。如下操作:
print(type('Let\'s go')) #
但是为了代码书写的更加优雅,不建议在运用字符串的时候添加反斜线。
python推荐每行书写代码个数不要超过80个字符。
三引号可以表示字符串换行,也可以表示注释,三个单引号和三个双引号表示的结果是一样的:
string = '''hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world
'''
print(string)
结果如下,完美展示换行:
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world,
hello world
还可以使用反斜线:
string = 'hello\
123213'
print(string) # hello123213
需要转义的字符:
print('hello\nworld')
输出结果:
hello
world
上面代码中怎么也把\n一起打印出来?其实可以添加r或者R修饰符
print(r'hello\nworld')
输出结果:
hello\nworld
添加反斜线也可以
print('hello\\nworld')
# hello\nworld
print('hello ' + 'world') # hello world # 字符串相加运算
print('hello'*3) # hellohellohello # 字符串与数字相乘运算
print('hello'[1]) # e # 字符串截取
print('hello world'[0: -1]) # hello worl # 切片
print('hello world'[0:]) # hello world # 切片
print(type([1, 2, 3])) #
LIST = [1, 2, 3, 4, 5, 6]
print(LIST[0]) # 1
print(LIST[3]) # 4
print(LIST[0: 2]) # [1, 2]
print(LIST[-1:]) # [6]
1). 列表相加
LIST1 = [1, 2]
LIST2 = [3, 4]
print(LIST1 + LIST2) # [1, 2, 3, 4]
2). 列表复制
LIST = [1, 2]
print(LIST * 3) # [1, 2, 1, 2, 1, 2]
列表的逻辑判断
LIST = [1, 2, 3, 4, 5, 6]
print(3 in LIST) # True
print(10 in LIST) # False
print(3 not in LIST) # False
列表的属性很多,在这里不在一一赘述。
TUPLE = (1, 2)
print(type(TUPLE)) #
print(TUPLE * 3) # (1, 2, 1, 2, 1, 2)
TUPLE1 = (1, 2, 3, 4)
TUPLE2 = (4, 5, 6)
print(TUPLE1[0]) # 1
print(TUPLE1[0: 2]) # (1, 2)
print(TUPLE1 + TUPLE2) # (1, 2, 3, 4, 4, 5, 6)
# ord返回ascll码
print(ord("w")) # 119
print(ord("W")) # 87
无序,唯一
print(type({1, 2, 3, 4})) #
print({1, 2, 2, 3, 3, 4, 5}) # {1, 2, 3, 4, 5}
print(len({1, 2, 3})) # 3
print(1 in {1, 2, 3}) # True
print(1 not in {1, 2, 3}) # False
print({1, 2, 3} - {1}) # {2, 3} 差集
print({1, 2, 3} & {1}) # 交集
print({1, 2, 3} | {1, 3, 5, 7, 9}) # {1, 2, 3, 5, 7, 9} 并集
SET = set()
print(type(SET)) #
print(type({})) #
键值对 key value, 不可以存在相同的键。
dict1 = {1: 1, 2: 2, 3: 3}
dict2 = {"W": 1, "E": 2, "R": 3}
dict3 = {(1, 2, 3): 1, "1": 2, "R": 3} # 可以使用元组当做key
print(type(dict1))
print(dict2["W"])
print(dict3[(1, 2, 3)]) # 1