Basic data types and built-in method

Priority grasp

1, int int

  Uses: general definitions for integer, such as age, phone number, etc.

  Defined manner: age = 18 i.e., age = int (18)

  Commonly used mathematical calculation

 

  Int can be used to convert between hexadecimal: print (int ( '110', 2))

               print(int('123', 8))

               print(int('801', 16))

  Type Summary: int is disordered, immutable type can store a value.

 

 

2, float type

  Use: the recording decimals, height, weight, etc.

  Defined method: height = 1.80 i.e., height = float (1.80)

  For mathematical calculations

  A summary: float is disordered, immutable, you can store a value

 

 

3, strings

  Usage: used to store descriptive information, name, etc.

  Defined method: s = '' s = '' '' s = '' '' '' s = '' '' '' '' '' '' quoting no difference, but can not mix added: string before adding r escape the special meaning of the symbol into a string

  Common methods: 1, the index value (forward can also be reverse, not only take deposit)

        

= S1 ' Hello World ' 
Print (S1 [. 4]) # forward taken o 
Print (S1 [-7]) # reverse takes o

       2, slice index: intercepting a short string string

= S1 ' Hello World ' 
Print (S1 [2:. 5]) # take LLO 
Print (S1 [. 4:]) # fetch World O 
Print (S1 [:. 5]) # fetch Hello 
Print (S1 [0: -2: 2]) # setting step 2, taken hlowr 
Print (S1 [:: -1]) # reversed string dlrow olleh

        3, members of the operation: in, not in returns a Boolean value

        4, strip: removing the left and right sides of the character string or symbol space

INPUT = name ( ' Enter name ' ) .strip ()
 Print (name) 

A1 = ' *** *** LZN ' 
Print (a1.strip ( ' * ' ))

        5, split: segmentation, segmentation of the string can be specified sliced ​​separator (space by default) returns a list of

a1 = 'lzn*25*male'
print(a1.split('*'))

# ['lzn', '25', 'male']

       6, the length len: Get the current string length, i.e. the number of characters

        print (referred to as ( "Hello"))

        >>> 5

            7, the cycle for: each one removed character string

s = 'hello world'
for i in s:
    print(i)

Need to know

      8, restrip \ lstrip: removal of the right spaces \ resection left blank

      9, lower \ upper: case conversion

         str.lower () will all become English string lowercase

         str.upper () will string all uppercase English

      10, startswith \ endswith: determine what the current string begins, returns a Boolean value

      11, formatted output format: {} pass the index value may also be specified by value

Print ( ' My name is {}, {} years old this year ' .format ( ' LZN ' , 25 ))
 # >>> My name lzn, 25 years old this year 
Print ( ' My name is {0}, {1} years old this year ' . format ( ' LZN ' , 25 ))
 # >>> my name lzn, 25 years old this year 
Print ( ' my name is {1}, {0} years old this year ' .format ( ' LZN ' , 25 ))
 # >>> me called 25 years old this year lzn

       12, split \ rsplit: \ from right to left in order to be segmented in accordance with the character string from left to right, and can specify the number of cuts separator.

 

str = 'lzn*25*male'
print(str.rsplit('*', 1))
>>> ['lzn*25', 'male']

      13, jion: each element can be spliced ​​to the specified object separator iterations, the result is a string of splicing

 

l1 = ['lzn', '25', 'male']
print('|'.join(l1))
>>>lzn|25|male

 

      14, replace: the replacement string elements

        Syntax: replace ( 'Oldest', 'new content')

        

str = 'lzn,18'
print(str.replace('lzn', 'Bill'))
>>>'Bill,18'

      15, isdigit: determining whether a string of numbers is a pure, returns a Boolean value 

num = '1233211234567'
print(num.isdigit())
>>>True

 

Learn operation

      16, find: find the location (range can be specified) of an element in the current string, returns the index, not found -1

      17, index: with the find, can not find abnormal returns

      18, count: count the number of an element of the current string

      19, center \ ljust \ rjust \ zfill: setting the total width, centered \ Left \ right justified \ right alignment, the width of the string with filled symbols not specified, zfill filled with 0

      20, is series

 

A summary of the string: ordered, immutable, you can store a value

4. List

    Uses: for storing the value of one or more different types of

    Defined by:, separated by a comma between each value by [] stored value

      1, the index value (can be positive and negative), it may be desirable to deposit

         l1 = [1,2,3,4,5]

         print(l1[2])

         >>>3

      2, the index sliced

         print(l1[1: 4])

         >>>[2,3,4]]

      3, add

      3.1 append (): added value, can only be added to the last one, you can only add a value

          l1.append(1000)

          print(l1)

          >>>[1,2,3,4,5,1000]

      3.2 insert (): the value is inserted, the insertion position specified by the index

          l1.insert(3, 999)

          print(l1)

          >>>[1,2,3,999,4,5]

       3.3 extend (): a plurality of values ​​can be increased (the object must be iterative)

          l1.extend([6,7,8])

          print(l1)

          [1,2,3,4,5,6,7,8,9]

        4, delete

        4.1 remove (): delete the specified value (the first value to delete only appear)

        l1.remove(1)

        print(l1)

        >>>[2,3,4,5]

        4.2 pop (): do not pass a default value beginning from the last delete, delete the value of the return value

        l1.pop()                                    l1.pop(2)

        print(l1)                                    print(l1)

        >>>[1,2,3,4]        >>>[1,2,4,5]

Guess you like

Origin www.cnblogs.com/littleb/p/11798589.html