day5--基本数字类型及内置方法--数字类型和字符串

基本数字类型及内置方法

进制之间的转换

二进制转十进制:0,1

110 1 * (2**2) + 1 * (2**1) + 0 * (2**0) = 4 + 2 + 0 = 6

八进制转十进制:0-7

123 1 * (8**2) + 2 * (8**1) + 3 * (8**0) = 64 + 16 + 3 = 83

十六进制转十进制:0-9 a-f

321 3 * (16 ** 2) + 2 * (16 ** 1) + 1 * (16**0) = 768 + 32 + 1 = 801
"""
十进制转2进制,8进制,十六进制
print(bin(23))
print(oct(457))
print(hex(234))
2进制,8进制,十六进制转十进制
print(int('10111', 2))
print(int('711', 8))
print(int('34454', 16))

1.整型和浮点型

  • =========================类型总结==========================

    有序or无序  -----有索引的类型都是有序的    无序

    可变or不可变 -------  int整型是不可变类型

    值变id不变就是可变类型

    值变id也变就是不可变类型

  • ========================基本方法===========================

  • 用途:用于记录小数,身高,体重,薪资

  • 定义方式: height = 1.0  # height = float(1.0)

  • 常用方法:

  • 数学计算

============================类型总结============================

有序or无序 : 无序

可变or不可变 : 不可变

存一个值or存多个值 : 存一个值

2.字符串

========================基本方法===========================

用途: 用于存储一些描述性信息,名字。。

定义方式:

第一种:
s1 = '大象'
第二种:
s2 = "大象2"
第三种:
s3 = '''大象3'''
s4 = """大象4"""
print(s1,s2,s3,s4)
以上三种方式没有任何区别,但是不能混用
补充:字符串前面加一个小写的人r,代表转义,把具有特殊意义的字符转化为普通的。

优先掌握的知识点

  • 索引取值(正向取、反向取),只能取不能存

    s1 = 'hello world'

    print(s1[4])  # 正向取
    print(s1[-7]) # 反向取

    o
    o

     

  • 索引切片   :截取字符串中的一小段字符串

    s1 = 'hello world'

    print(s1[2:5])
    打印结果:llo
    print(s1[4:])
    打印结果:o world
    print(s1[:5])
    打印结果:hello
    print(s1[0:-2:2])
    打印结果:hlowr
    print(s1[::-1])
    打印结果:dlrow olleh
  • 成员运算:in   not in

    s1 = 'hello world'
    print("o" in s1)
    结果:True
    print("o" not in s1)
    结果:False
    in 表示o在s1里面,判断的正确。第二个是不再里面,判断错误
  • strip :去除字符串左右两边的空格,中间不算

  • input无论接受的是什么类型,一定返回的是字符串

    name = input(">>:").strip()
    print(len(name))

    >>:       sean
    4

    a1 = '$$$$sean$$$'
    print(a1.strip("$"))

    sean
  • split : 切分: 对字符串进行切分,可以指定切分的分隔符,返回是一个列表

    a1 = 'sean 18 male'

    print(a1.split())

    ['sean', '18', 'male']
  • len() : 获取当前数据中的元素的个数

    a1 = 'hello'
    print(len(a1))

    5
  • for

    a1 = 'hello'

    for i in a1:
       print(i, end=" ")
    h e l l o
  • find findindex indexcount

    find # 查找当前字符串中某个元素的位置,返回索引,找不到返回-1
    s1 = '你今天吃饭吃了吗?'
    print(s1.find("?",))

    index # 查找当前字符串中某个元素的位置,返回索引,找不到返回异常
    print(s1.index("?"))

    count # 统计当前字符串中某一个元素的个数
    print(s1.count("吃"))
  • centerljust justzfill

    print("欢迎光临".center(8,"-"))    在两边加

    print("欢迎光临".ljust(30,"-"))   在右边加-
    print("欢迎光临".rjust(30,"-"))   在左边加-

    print("欢迎光临".zfill(50))       增加序列对其
  • s1 = """
    sean 18 male
    tank 84 female
    """
    print(s1.expandtabs())
    空格中加数字是每个元素之间的间隔。默认8个空格
  • is系列

    a = b'10'
    b = '10'
    c = '十'
    d = 'IV'
    print(type(a))

    print(b.isnumeric())
    print(c.isnumeric())
    print(d.isnumeric())
    判断是否是数值
  • isdigit: unicode,bytes

    isdigit判断 unicode  bytes

    字符串的本质是unicode  bytes

    print(a.isdigit())
    print(b.isdigit())

需要掌握知识点

  • strip,rstrip,  lstrip

    strip: 去除两边的

    rstrip:去除右边

    lstrip:去除左边

    inp = input(">>:").lstrip("*")
    print(inp)
    print(len(inp))

    >>:*****sean****
    sean****
    8                        
  • lowerupper

    改变元素大小写

    s1 = 'Hello world'

    print(s1.upper())
    print(s1.lower())

    HELLO WORLD
    hello world
  • startswithendswith  : 判断当前字符串是否以。。。开头,或者以。。结尾,返回的一定是布尔值

    print(s1.startswith("He"))
    print(s1.endswith('ld'))

    True
    True
  • .format()

    name = 'sean'
    age = 19
    print("你的名字是:{1},你的年龄是:{0}".format(name,age))

    你的名字是:19,你的年龄是:sean
  • f-string:

    通过大括号接收变量,在字符串前面一定要加一个小写f,,,,在python3.6以后才有

    print(f"你的名字是:{name},你的年龄是:{age}")
  • split rsplit

    s1 = "name,age,gender"
    print(s1.split(",",1)) # 可以指定切分的次数

    ['name', 'age,gender']
  • join : 将(列表)中每个元素按照前面字符串中的分隔符进行拼接

    sean','18','male']
    print("|".join(l1))

    sean|18|male
  • replace:将字符串中的元素进行替换,参数,先老值,再新值

    s1 = 'sean,18'
    print(s1.replace("sean",'大象'))

    大象,18
  • isdigit() : 判断当前字符串中的数据,是否是一个数字,返回布尔值

    score = input("please input your score:")

    if score.isdigit():
      score = int(score)
      if score >= 90:
          print('优秀')
    else:
      print("你tmd能不能好好输")
       
    结果:please input your score:90
    优秀

    please input your score:98.9
    你tmd能不能好好输

    他智能识别整数型。浮点数识别不了

    =========================类型总结==========================

    有序or无序  : 有序

    可变or不可变  :不可变类型

    存一个值or存多个值  : 存一个值

原文地址:https://www.cnblogs.com/lishuangjian/p/11799915.html