有c Java基础者阅读较好
1. 变量
(标识,一个变量由标识 类型 值 组成)
a=10
print(id(a),type(a),a)
#140703780702160 <class 'int'> 10
2. == 与 is
==比较的是值
is 比较的id
用法刚好的Java equals相反
a=10
b=10
print(a==b) #T
print(a is b) #T id一样
lst1=[10,20,30]
lst2=[10,20,30]
print(lst1 == lst2)#T
print(lst1 is lst2) #F id不一样
3. 布尔运算
-
and 并且 两个T为T
-
or 或者
-
not 非
-
in 与not in
s="hello"
print("e" in s)
print("a" in s)
print("e" not in s)
print("a" not in s)
True
False
False
True
4. 运算符优先级
先后顺序如图所示
5. intput
intput 里面写提醒语句 返回的是str类型 需要的话 要类型转换
6.对象的布尔值
print(bool(False))
print(bool(""))
print(bool(0))
print(bool(None))
print(bool([])) # 空列表
print(bool(list())) # 空列表
print(bool(())) #空元组
print(bool(tuple()))#空元组
print(bool({})) #空字典
print(bool(dict()))#空字典
print(bool(set())) #空集合
# 以上输出都是False
# 其他对象都为T
7. 程序的组织结构
-
顺序结构
程序重上到下 依次执行
-
分支结构
a =100 if a>10: print("ok") else: print("no")
a = int(input("输入成绩判断等级"))
if a>0 and a<=100:
if a>=80 :
print("优秀")
elif a>=60:
print("及格")
else:
print("不及格")
else:
print("输入错误")
- 条件表达式
中间判断为T 值为左 判断为F 值为右