1.注释
单行注释: 以#开头
多行注释: 用 ‘’’ ‘’’ 或 “”" “”",三引号将要注释内容包括
2.算数运算符
两个数值类型数据进行运算,结果数据类型为int
3.比较运算符
两个数值类型数据进行运算,结果数据类型为布尔类型,False或True
4.逻辑运算符
返回结果为布尔类型,False或者True
5.位运算符
bin(数值):转化为二进制
6.其他运算符
用于判断
例子1:
letters = ['A', 'B', 'C']
if 'A' in letters:
print('A' + ' exists')
if 'h' not in letters:
print('h' + ' not exists')
例子2:
比较的两个变量均指向不可变类型(注释为结果)
a = ["hello"]
b = ["hello"]
print(a is b, a == b) # False True
print(a is not b, a != b) # True False
例子三:
比较的两个变量均指向可变类型(注释为结果)
a = ["hello"]
b = ["hello"]
print(a is b, a == b) # False True
print(a is not b, a != b) # True False
- is, is not 对比的是两个变量的内存地址
- ==, != 对比的是两个变量的值
- 比较的两个变量,指向的都是地址不可变的类型(str等),那么is,is not 和 ==,!= 是完全等价的。
- 对比的两个变量,指向的是地址可变的类型(list,dict,tuple等),则两者是有区别的。
7.数据类型
int: 整型
float:浮点型
str: 字符串
bool:布尔类型
cuple: 元组 (不可直接修改)
set: 序列(不允许存在重复数据)
list: 列表 [ ](储存大量数据)
dict: 字典 { }( {key:value} ,存储大量数据)
8.输出函数
print() : 打印输出(可放入变量或直接放内容)
9.条件语句
可配合 and / or 使用,条件语句可多次嵌套使用
1.if 语句
满足条件即可输出
if 2 > 1 and not 2 > 3:
print('Correct Judgement!')
2.if…else语句
input(): 输入函数,返回输入内容
temp = input("猜一猜小姐姐想的是哪个数字?")
guess = int(temp) # input 函数将接收的任何数据类型都默认为 str。
if guess == 666:
print("你太了解小姐姐的心思了!")
print("哼,猜对也没有奖励!")
else:
print("猜错了,小姐姐现在心里想的是666!")
print("游戏结束,不玩儿啦!")
3.if…elif…else语句
elif 在 if 的条件前提下,进行约束,elif可以多层
temp = input('请输入成绩:')
source = int(temp)
if 100 >= source >= 90:
print('A')
elif 90 > source >= 80:
print('B')
elif 80 > source >= 60:
print('C')
elif 60 > source >= 0:
print('D')
else:
print('输入错误!')
10.assert 关键词
assert这个关键词我们称之为“断言”,当这个关键词后边的条件为 False 时,程序自动崩溃并抛出AssertionError的异常
注释为结果
my_list = ['lsgogroup']
my_list.pop(0)
assert len(my_list) > 0
# AssertionError