- python 变量无类型,但值里面有类型。 动态类型语言(python&javascript)
- Subtraction
num = 10
print(num / 2, num // 3, num // -3)
# 5.0, 3, -4 向下取整
int(num / 3)
# 不用向下取整的办法
- reverse 3-digit number
def res(num):digit1 = num % 10digit2 = num // 10 % 10digit3 = num // 100return digit1 * 100 + digit2 * 10 + digit3print(res(123))
- 判断两个浮点数是否相等不能直接用==
print(-4.5 * 2.9 == -13.05) # False
print(abs(-4.5 * 2.9 - -13.05) < 0.00001) # 1e-5 = 10^(-5)
-
运算优先级 operation precedence
not > and > or -
计算闰年
# 公元年份为4的倍数但非100的倍数,为366天闰年。
# 公元年份为400的倍数,(1600年及2000年)为闰年。
def is_leap_year(year):if(year % 4 == 0 and year % 100 != 0 or year % 400 ==0):return Truereturn Falseprint(is_leap_year(2040))
- 交换变量
def swag(num1, num2):temp = num1num1 = num2num2 = tempreturn num1, num2
print(swag(12, 34))# python中可以直接,元组赋值
num1, num2 = num2, num1
-
name variable
google.github.io/styleguide/pyguide.html -
python中的权限控制access control
默认成员变量都是public
在成员变量前加_
线,即设置为protected,建议仅在类中和子类中访问。
加__
,为private
可以通过对象名➕类名➕变量名访问私有变量 :stu1._Student.__score