1. bin()函数:将十进制的数字转换成二进制的字符
2. oct()函数:将十进制的数字转换成八进制的字符串
3. hex()函数:将十进制的数字转换成十六进制的字符串
( 方法:除以对应进制,直到0为止,倒着取余数 )
4. 其他进制转换为十进制( int("目标字符串",对应的进制)-->得到对应的十进制数字 )
v1 = int("0b0101100", 2) print(v1) v2 = int("0o123", 8) print(v2) v3 = int("0x6f", 16) print(v3)方法:(其中 ' ** ' 表示幂)
其他进制: abcd --->
十进制: d*进制**0+e*进制**0+c*进制**1+b*进制**2+a*进制**3
正数:
原码 = 反码 = 补码 0000 0001
负数:
最高位为符号位
原码: 1000 0001
反码: 1111 1110 (原码除符号位之外,所有的位数取反)
补码: 1111 1111 (反码 +1)
补码 --> 反码
继续取反 +1
<< :左移
>> :右移
| :按位或
^ : 按位异或
参加运算的两个对象,如果两个相应位为“异”(值不同),则该位结果为1,否则为0。
A^B^B=A
& : 按位与
~ : 按位翻转
1. XX
普通变量的使用方式
2. _XX
单个下划线开始
在某个模块中,如果变量是该格式
使用 ” from 模块 import * “ 的方式无法使用
3. __XX
私有属性或私有方法
名字重整(改名)
重整的原则:
_类名__私有属性或私有方法名
4. __XX__
主要用于方法。这种方法系统会自己调用,自定义方法应避免这种写法
5. XX_
用于区分变量名/方法名
描述
property() 函数的作用是在新式类中返回属性值。
语法
以下是 property() 方法的语法:
class property([fget[, fset[, fdel[, doc]]]])
参数
fget -- 获取属性值的函数fset -- 设置属性值的函数fdel -- 删除属性值函数doc -- 属性描述信息返回值
返回新式类属性。
""" property 的使用 私有属性,提供开放接口,供外界进行访问 """ class Student: def __init__(self, name, age): self.name = name # 私有属性 self.__age = age def getAge(self): return self.__age def setAge(self, age): if isinstance(age, int): # isinstance() 函数来判断一个对象是否是一个已知的类型。 self.__age = age else: print("类型错误") age = property(getAge,setAge) # property() 函数的作用是在新式类中返回属性值。注意:get在前,set在后 s1 = Student("晓晓",18) #print(s1.getAge()) s1.age = 15 print(s1.age) ########################## 分 隔 ######################### """ 装饰器:简化私有属性的访问方式 @property @属性.setter """ class Account: def __init__(self): self.__money = 0 @property def money(self): # getmoney return self.__money @money.setter def money(self, money): # setmoney if isinstance(money, int): self.__money = money else: print("金钱类型错误") m = Account() m.money = 200 print(m.money) m.money = 189.9 print(m.money)