饮冰三年-人工智能-Python-13Python基础之运算符与数据类型

1:算数运算符

  +

  -

  *

  /

  **

  %

  //

2: 成员运算符

   in

   not in  

name = """张三"""
if  "" in name:
    print("OK")
if "" not in name:
    print("Not in")

# 输出结果
# OK
# Not in
View Code

3:比较运算符

  True False

  ==

  >

  <

  >=

  <=

  !=

4:赋值运算符

  +=

  -=

  *=

  /=

  **=

  %=

  //=

5:逻辑运算符

  and

  or

  not

6:基本数据类型

  1:数字 int

      6.1.1 类型转换

       int([x]) -> integer

      int(x, base=10) -> integer  :base表示:N进制

      6.1.2 当前数字的二进制至少用几位表示      

      bit_length()      
strNum = "12"
print(type(strNum),strNum) # 输出结果 <class 'str'> 12
num = int(strNum)
print(type(num),num) # 输出结果 <class 'int'> 12
v = num.bit_length()
print(v) # 输出结果 4
View Code
 布尔值 bool

  字符串 str    

strTest = "aaRon"
v1 = strTest.capitalize(); #首字母大写
print(v1) # 输出结果 Aaron
v2 = strTest.lower(); #变小写
print(v2) # 输出结果 aaron
v3 = strTest.casefold();#变小写 更强大
print(v3) # 输出结果 aaron
v4 =  strTest.center(10,"");#填充
print(v4) # 输出结果 中中aaRon中中中
v5 = strTest.count('r');#计算字符串出现个数,区分大小写
print(v5) # 输出结果    0
v6 = strTest.count('a',1);#计算字符串出现个数,从某个位置开始找
print(v6) # 输出结果    1
v7 = strTest.count('a',2,666);#计算字符串出现个数,从某个位置开始找
print(v7) # 输出结果    0
v8 = strTest.endswith("on");#以XX结尾
print(v8) # 输出结果    True
v9 = strTest.endswith("a",0,2);#以XX结尾  区间应该是[0,2)
print(v9) # 输出结果    True
v10 = strTest.startswith("aa");#以XX开头
print(v10) # 输出结果    True
v11 = strTest.startswith("R",2,6);#以XX开头 区间应该是[2,6)
print(v11) # 输出结果    True
v12 = strTest.find("o");#获取字节首次出现的位置
print(v12) # 输出结果    3
v13 = strTest.find("o",4,5);#获取字节首次出现的位置
print(v13) # 输出结果    -1
strTest2 ="I am {name}, I'm {age} years old";# format格式化
strTest3 ="I am {0}, I'm {1} years old";# format格式化
v14 = strTest2.format(name='Aaron',age=23);
print(v14); # 输出结果I am Aaron, I'm 23 years old
v15 = strTest3.format('Aaron',23);
print(v15); # 输出结果I am Aaron, I'm 23 years old
v16 = strTest2.format_map({"name":"Aaron","age":23});
print(v16); # 输出结果I am Aaron, I'm 23 years old
# v17 = strTest.index('z'); # 此方法不好,容易报错,建议不使用,使用find替代
# print(v17); # 输出结果 报错了
strTest3 = "姓名\t密码\t备注\t\nzhang三\tzhangsanzhangsan\t我的张三,职位管理员\n李四\tlisilisi\t我的lisi,职位普通员工";
v18 = strTest3.expandtabs(20);# 制表符
print(v18);# 姓名                  密码                  备注
          #zhang三              zhangsanzhangsan    我的张三,职位管理员
          #李四                  lisilisi            我的lisi,职位普通员工
View Code

  列表 list

  

  元祖  tuple

  

  字典 dict

猜你喜欢

转载自www.cnblogs.com/YK2012/p/9645058.html