Python基础入门学习小总结
标识符
- 标识符由字母下划线组成,不能数字开头。
a_1 = 1111
print(a_1)
1_a = 2222 — End of statement expected
print(1_a) - 区分大小写
book = 1
print(Book) — Unresolved reference ‘Book’ - 不能使用关键字
for = 1 — ‘:’ expected
print(for)
命名规范
- 小驼峰 myName
- 大驼峰 FirstName
算数运算符
- 算数运算符 + ,-, *, / , //(整除), %(取余) ,**(幂)
扩展 :字符串的加法是拼接字符串
a = ‘123’
b = ‘456’
print(a + b)
注:在python中,加号两边都是字符串才能加法运算
c = ‘123’
d = 456
print(c + str(d))
字符串的乘法 将字符串重复
e = ‘qwer’
f = e * 3
print(f)
赋值运算符,多变量时可以用逗号隔开赋值
x,y,z = 1,3,5
print(x)
print(y)
print(z)
复合赋值运算符 +=,-=,*=,/=,//=(整除), %=(取余)
a = a + 1 --> a += 1
b = b * 3 --> b *= 3
c = c - 2 --> c -= 2
d = d / 4 --> d /= 4
e = e // 2 --> e //= 2
f = f % 5 --> f %= 5
比较运算符,返回的是布尔类型的数据 ==,!=,>,<,>=,<=
== 判断两边的变量是否一致
m = 10
n = 20
print(m == n)
!= 判断两边的变量是否不一致
m = 10
n = 20
print(m != n)
逻辑运算符 and 与, or 或, not 非
and 两边必须都是True才会返回True
or 两边有一个成立就会返回True
not 取反
扩展逻辑运算符性能优化
- and的性能优化,当and的前面的结果为false的情况下,后面代码不再执行
p = 24
p > 8 and print(‘abc’)
p < 8 and print(‘ace’) - or的性能优化,只要一方为True,结果就是True
q = 33
q > 30 or print(‘9999’)
q < 24 or print(‘8888’)