day5_7.3 various types of data operations function

Yesterday added:

  1. In writing code, there are always some branch to write out, in order not to affect the running of the whole system, you can use keywords to skip pass. Such as

count=0
while count<10:
    if count<5:
        print(count)
        count+=1
    else:
        pass

  If you normally do not write else statements and start running, an error, the program can pass through the normal run up.

  2. In use the range can be defined by a third step size parameter, such as

for i in range (1,10,2):
    print(i)

  When the step size is set to 2, i will fold into two iterations, so that the output of the odd-numbered 1-9 into 1-9 original output.

Content Today

1. The use of data types, the definition of frequently used functions and operation methods.

  1. integer (int)

  Uses: keep an array of integers, defined phone number number qq, ID number, and so on.

  Definition method:

age=18
#age=(int)18

  This type of conversion data only within the range of rotation conversion, the image can not float to integer conversions, which are very different languages ​​and c:

a=int('123412')
b=int(23.12)

  Will be output as a type of int 123412, b will be given, when the string appears also being given non-numeric, or floating-point numbers.

  Decimal conversion function:

  Decimal to binary conversion

bin = D (12 is )
 # >>> ob1100 
# OB back indicates that the following number is a binary

  Decimal converted to octal

OCT = D (12 is ) 
 # >>> Oo14 
# behind the octal number represents Oo

  Converting decimal to hexadecimal

Hex = D (12 is )
 # OXC >>> 
# hex number represented by the back Ox

  Binary, octal, hexadecimal to decimal conversion

print(int('10',2))
print(int('20',8))
print(int('20',16))

   The final result is output as "2,16,32", the second parameter represents a hexadecimal number, and the value of the first parameter is a string to be passed.

  2. Float:

  Action: recording electrical data with decimals, such as height, weight, etc., more precise.

  Definition method:

a=3.14
#a=float(3.14)

  Floating-point data string also be converted to floating point.

  3. string:

  Uses: a data type descriptive.

  Definition method:

str1 = ' I is a string. 1 ' 
str2 = " I string 2 " 
Str3 = '' I string 3 '' '

  These three can be defined strings.

  [1] by index value (the value of the forward and reverse value)

str1="abcdefg"
print(str1[1],str1[-1])

  In the above code, respectively, with values ​​of the forward and backward values, values ​​obtained respectively for the wind "b" and "g"

  [2] sliced

  In operation of the string, which can take a range of characters in a string, such as

str1="abcdefg"
print(str1[0:5:1])

  Run the code above results "abcde", 0-5 which takes a string representing the interval of 1 represents a step size, i.e., a character output per one character.

  In the slice, the direction of which may be cut from right to left, from right to left as long as the interval setting step arranged to traverse the negative to the left, for example:

str1="abcdefg"
print(str1[6:0:-2])

  From section 6-1 (right-left opening and closing), the step size is -2, the output thereof should be gec

  Length [3]

  Uses: count the number of characters in a string.

= str1 ' I string ' 
Print (len (str1))

  5 is operating results, i.e. the number of symbols in the string.

  [4] member operator

  Uses: determining whether a string is the string corresponding string:

print('lzx' in 'my name is lzx')
print('lzx' not in 'my name is lzx')

  The first output is lzx appears in the back of the characters, the results clearly true, the second is not lzx output is not in the back of the string, the results are false.

  【5】strip()

  Purpose: to remove the spaces on both sides of the string, in the middle of reserved characters.

str3='    222     i am iron man   22222 '
print(str3.strip())
print(str3.strip(' 2 '))

  The first output of the associated string 2, the default value given five function strip is to be removed on both sides of the space, and when a given number of values, will be removed in accordance with the character string of the inner and outer sides.

  When the removal, Python only sequentially from outside to inside the character recognition, the character if the middle of the block, the character is not crossed, the removal work so far. example:

str1='!?<1314<?!'
str2='!?<1314<?}!'
print(str1.strip('!?<'),str2.strip('!?<'))

  str1 completely clear the extra characters, and when str2} symbols to be blocked, so not all identified prior to, intermediate character count.

  rstrip (), lstrip () respectively with a left Clear Clear, i.e. the clearance around the string.

str1='!?<1314<?!'
str2='!?<1314<?!'
print(str1.lstrip('!?<'))
print(str1.rstrip('!?<'))
#1314<?!
#!?<1314

  In addition to the results of a symbol on the left, in addition to the right to a symbol.

  【6】split()

  Uses: for slicing at the symbol string, the final output in the form of a list. example:

str1='www.baidu.com'
list1=str1.split(".")
print(list1)
#>>>['www', 'baidu', 'com']

  You can see the split in the string parameter is actually divided in accordance with what the symbol, and the output is a list. If the need to cut part of the string, the second parameter to its required set:

str1='www.baidu.com'
list2=str1.split('.',1)
print(list2)

  The result is the final output [ 'www', 'baidu.com'], only a first dividing string.

  rsplit. (), the string is segmented from right to left, to select a specified number of times if the segmentation, and split rsplit effect is the same.

 

Guess you like

Origin www.cnblogs.com/LZXlzmmddtm/p/11128583.html