Plastic, string, Boolean, for loop

A, int int

  1. For calculating and comparing, in python3 are all integers of type int

  2. Binary and decimal conversion

    • Decimal to binary:

      Converted into binary integer: modulo two addition, in reverse order, zero padding high

      14 % 2 = 0
      7  % 2 = 1
      3  % 2 = 1
      1  % 2 = 1
      0
      14转换为二进制为:0000 1110

      First converted into negative integer corresponding to the positive integers, converted into binary, the binary negated, then add the results of a

    • Binary conversion to decimal:

      First padded binary digits, if the first is 0 for a positive integer, 1 is the first representative of a negative integer

      • Positive integer: padded bits after the bits are binary value corresponding to the lower side of the multiplication, and then obtained by adding the decimal

        1010  
        # 先补齐位数  =>  0000 1010     128  64   32  16   8   4   2   1 常用的8位
        2**3 *1 + 2**2 * 0 + 2**1 * 1 + 2**0 * 0  =  10
      • Negative integers, the first of a binary negated, then converted

  3. bit_length()

    Conversion to binary decimal seek length occupied

Second, the Boolean value

  • True real number other than 0 non-empty string

  • False False 0 empty string

  • Type conversion: convert what you want to type, you will need to use this type of conversion quotes

    a = "hahahahaha"
    b = "123456"
    c = 123456
    a1 = bool(a)  => True
    b1 = int(b)   => 123456
    c1 = str(c)   => "123456"

Third, the common definition of the string operation

  1. For storing small amount of data

  2. python with ',' ',' '' '' ' "" "" "" string is enclosed

  3. Common Operations

    1. Index: Index is the index, from left to right, starting from 0; right to left, starting at -1

      num = "1234567890"
      print(num[1])  => 2
      print(num[4])  => 5
      print(num[-1]) => 0
      print(num[-2]) => 9
      print(num[21]) # 索引超出范围,报错
    2. Slice: Using an index to the contents of the string taken

      • Syntax: str [start: end: step]

      • Rule: care regardless tail, start capturing the start, end position to intercept, but not the end.

        num = "0123456789"
        num[::] == num[:]  # 从头取到尾
        print(num[0:3])  =>  012 # 不包含索引3对应的"3"
        print(num[5:8])   =>  567
        print(num[5:])    =>  56789  # 想取到最后,end可以不写
        print(num[5:20])  =>  56789  # 索引超出范围,不会报错
        print(num[-1:-5:-1]) => 9876 # step步长决定取值的方向,默认为1
        print(num[1:7:2]) =>  135    # str步长决定取值的步子大小,即每step个取一个
    3. Common method

      • Remember, strings are immutable objects, so any operation will not have any impact on the original string
      name = "Hello World"
      print(name.upper()) => HELLO WORLD  字母全部转换成大写
      print(name.lower()) => hello world  字母全部转换成小写
      print(name.startswith("H"))  => True  是否以"H"开头
      print(name.endswith("d"))    => True  是否以"d"结尾
      print(name.count("o"))       => 2     统计"o"出现的次数
      print(name.replace("H","A",次数)  => Aello World  # 将H替换成A,默认全部替换,可以输入替换次数
      
      a = "  Alex  "
      print(a.strip()) => Alex  # 去掉两端的某些元素,()内可以指定元素,默认是空白(空格和\n)
      
      b = "1,2,3"
      print(b.split(",")) => ["1","2","3"]  # 分割,可以指定用于分割的元素,分割后损失这个元素,默认以空格分隔,返回一个列表。
      • It is judged Series
      name.isdigit()   # 判断是不是阿拉伯数字,有漏洞
      name.isdecimal() # 判断是不是十进制,可以用于判断数字
      name.isalpha()   # 判断是不是中文和字母
      name.isalnum()   # 判断是不是中文,字母和数字
    4. The third string formatting

      name = "alex{}wusir{}"
      print(name.format("结婚了","要结婚了")) # 按照位置顺序填充
      
      name = "alex{1}wusir{0}"        # 按照索引位置填充
      print(name.format("结婚了","要结婚了"))
      
      name = "alex{a}wusir{b}"
      print(name.format(a="结婚了",b="要结婚了"))  # 指名道姓填充
    5. Calculation of the character string ⻓

      len (string) - factory function - all types can be used, in addition to the number of elements required int and bool

Four, for circulation and range

  1. for loop

    # for 变量 in  可迭代对象:
    #    pass
    s = "1234567890"
    for each in s:          # 遍历字符串
        print(each)         # 1 2 3 4 5 6 7 8 9 0
    print(each)             # 0   注意
  2. Placeholder

    pass: placeholder

  3. range

    • Syntax range (start, end, step): range, from start to End, does not contain End, step 1 step defaults

    • python3 print range () will print itself, python2 will print out a list

      for each in range(1,10):
         print(each)
      # 1  2  3  4  5  6  7  8  9

Guess you like

Origin www.cnblogs.com/douzi-m/p/11904683.html