Python basis (c)

Today the main content

  • 整型


      Hexadecimal conversion

  • 字符串



      Detailed part of the method

  • for loop

First, the integer (int)

Range (a) Integer

    32: 31 ~ -2 ** 2 ** 31-1

    64: 63 ~ -2 ** 2 ** 63-1

(B) hex conversion

  1. Decimal to binary:

      Short division: 2 and other short, in addition to 0 to the quotient, rounded up to the next I

    • bin () function: to convert the decimal number to binary

      num = 13
      print(bin(num))  # 1101
  2. Binary decimal conversion

      Arithmetic method: right to left by 2 ^ n (n is 0, 1, …), and finally adding

    • int () function: the second argument passed several binary number, corresponding to the first argument hexadecimal

      num = "1101"
      print(int(num, 2))  # 13

(C) immutable data type

  1. What is immutable / mutable data

      Can be modified in situ data is variable data type

      You can not place the modified data (changes will open up new memory space) are immutable data types

  2. Integer data type is immutable

  3. id () function: View the memory address

    a = 10
    print(id(a))  # 1956675984
    a = a + 1
    print(id(a))  # 1956676016

Second, a string (str)

(A) Index

  1. What is the index?

      Index is the index, python index is zero-based

  2. Index only to an ordered data type

  3. Index indicates

      str [index]

  4. Positive index and anti Index

      Positive index: From left to right 0,1,2 …

      Anti Index: right to left -1, -2, -3 …

    a = "粪发涂墙的郭老湿"
    print(a[0])  # 粪
    print(a[3])  # 墙
    print(a[5])  # 郭
    print(a[-3])  # 郭
  • Note: When the index exceeds the maximum index value will complain

    a = "粪发涂墙的郭老湿"
    print(a[10])  # IndexError: string index out of range

(B) slices

  • The above example, if you want to get “fat dung plastering” can only be carried out one by one and then stitching index

    a = "粪发涂墙的郭老湿"
    a1 = print(a[0])
    a2 = print(a[1])
    a3 = print(a[2])
    a4 = print(a[3])
    print(a1 + a2 + a3 + a4)  # 粪发涂墙

    Is not too much trouble, you can use sliced

  1. Slice ordered only to use the data type

  2. Slice format

    • str[start: end: step]
    • The default is 1 step

  3. Features: care regardless of Ding

    • From the top of the slice without entering a start;

      Cut to the end without the input end;

      Cut from the beginning to the end do not enter

    a = "粪发涂墙的郭老湿"
    mes = a[:4]  # 从开头切不用输入start
    print(mes)  # 粪发涂墙

      step to step, step interval may be utilized slices

    a = "粪发涂墙老污郭"
    print(a[::2])  # 粪涂老郭
    • If the step is negative, the reverse sections

      Note: If the reverse slices, start and end positions are taken from the back

    a = "粪发涂墙老污郭"
    print(a[::-2])  # 郭老涂粪
    print(a[-2:-6:-1])  # 污老墙涂

      The string can be reversed with a microtome

    a = "粪发涂墙老污郭"
    a_inversion = a[::-1]
    print(a_inversion)  # 郭污老墙涂发粪

    When the index exceeds the maximum index value is not an error

(Iii) Method Detailed

  • All uppercase

    All lowercase

    N determines whether to begin

    N is determined whether to end

    Calculate the number n appears

    Remove the blank ends, \ n, \ t

    N to cut


    Determining whether comprised of a digital, Chinese letters

    It determines whether or not composed of Chinese letters

    It determines whether or not composed of Arabic numerals

    Determining whether constituted by a decimal digit

    str.replace(old, new)
  1. str.upper()

      The string all uppercase, only letters entered into force, is not being given to other

    a = "zxdznb"
    new_a = a.upper()
    print(a)  # 不可变类型,原字符串不变
    print(new_a)  # ZXDZNB
  2. str.lower()

      All lowercase string, only letters entered into force, is not being given to other

    a = "ZXDZNB"
    new_a = a.lower()
    print(a)  # 不可变类型,原字符串不变
    print(new_a)  # zxdznb
  • Application: login verification code is not case sensitive

    verify_code = "eF7d"
    while True:
      user = input("账号:")
      pwd = input("密码:")
      while True:
          vc = input("请输入验证码:")
          if vc.upper() == verify_code.upper():  # 全部变为大写
      if user == "ZXD" and pwd == "zxd123":
  1. str.startswith(n)

      Determining whether the string begins with parameters n, the output is a Boolean value

      You can also determine the beginning of the slice

    a = "zxdznb"
    print(a.startswith("z"))  # True
    print(a.startswith("x"))  # False
    print(a.startswith("x", 1, 5))  # True
    print(a.startswith("z", 1, 5))  # False
  2. str.endswith(n)

      It determines whether the end of string parameters n, the output is a Boolean value

      You can also determine the end of the slice

    a = "zxdznb"
    print(a.endswith("b"))  # True
    print(a.endswith("x"))  # False
    print(a.endswith("n", 1, 5))  # True
    print(a.endswith("z", 1, 5))  # False
  3. str.count(n)

      N number of calculation parameters appearing in the string

      The number of slices may also be calculated variables n occurring

    a = "zxdznb"
    print(a.count("z"))  # 2
    print(a.count("z", 0, 3))  # 1
  4. str.strip()

      The default string ends remove spaces, line breaks (\ n-), tab (\ t)

      You can also specify the content removed

    a = "   zxdznb   "
    a1 = a.strip()
    print(a1)  # zxdznb
    a2 = a1.strip("b")
    print(a2)  # zxdzn
  • Application: When removing the user enter the account password, wrong hand to play more spaces

    while True:
      user = input("账号:").strip()
      pwd = input("密码:").strip()
      if user == "ZXD" and pwd == "zxd123":
  1. str.split()

      In default spaces, line breaks (\ n-), tab (\ t) cutting string, a list is obtained,

      You can also specify the character cutting

      May also fill the second argument can specify the number of cut

    a1 = "zxd znb"
    lst1 = a1.split()
    print(lst1[0], lst1[1])  # zxd znb
    a2 = "zxd,znb"
    lst2 = a2.split(",")
    print(lst2[0], lst2[1])  # zxd znb
    a3 = "xdznbznb"
    lst3 = a3.split("z", 1)
    print(lst3[0], lst3[1])  # xd nbznb
  2. str.replace(old, new)

      The character string replaced with a new character

      You can specify the number of replacement, the default Replace All

    a = "zxdhnbhnb"
    new1_a = a.replace("h", "z")
    print(new1_a)  # zxdznbznb  # 全部替换
    new2_a = a.replace("h", "z", 1)
    print(new2_a)  # zxdznbhnb  # 替换一次
  3. str.isalnum()

      Determining whether the string of digits, Chinese, letters

    a1 = "zxd666"
    a2 = "zxd_666"
    print(a1.isalnum())  # True
    print(a2.isalnum())  # False
  4. str.isalpha()

      Determining whether the string of letters, Chinese composition

    a1 = "zxd最nb"
    a2 = "zxd666"
    print(a1.isalpha())  # True
    print(a2.isalpha())  # False
  5. str.isdigit()

      Determining whether a string of Arabic numerals

    a1 = "666"
    a2 = "六六六"
    print(a1.isdigit())  # True
    print(a2.isdigit())  # False
  6. str.isdecimal()

      Determining whether a string of decimal digits

    a1 = "66"
    a2 = "0x42"
    print(a1.isdecimal())  # True
    print(a2.isdecimal())  # False

Three, for circulation

The difference between (a) and while loops for the

    for loop: Limited circulation

    while loop: infinite loop

(B) for loop structure

  • for i in xxx:

      for: Keyword

      i: variable name

      in: Keyword

      xxx: iterables

(C) len () function

    Length, generic query function data for all data types

(D) Examples for loop

  • A face questions

    for i in "abcde":
    print(i)  # 循环最后给变量i赋值的是e,没有变动e一直指向e
  • Understanding for circulation and function top-down enforcement mechanisms

    num = 1
    count = 3
    while count:
      for i in "abc":
          print(i + str(num),end=",")
      count -= 1
      num += 1

Leave a Reply