Integer and floating-point type; binary conversion; string type; the type of list;

First, the integer (int)
Uses: used to store qq number, phone number, ID number with no letters.
Defined manner: Age Age = 18 is # = int (18 is)
#int ( 'dfdgf') returns the results being given, or #int (34.6) returns an error result is
therefore, int only pure digital conversion string of letters and It will not work with a decimal point.

Base conversion:
other binary decimal conversion:
a) Binary Decimal Conversion:
10 = 1 * (2 ** 1) + 0 * (2 ** 0) 2 =
2) transfected decimal octal:
235 = 2 * (8 * * 2) + 3 * (8 * 1) + 5 * (8 * 0) of 157

3) from hexadecimal decimal (0-9, the AF):
217 = 2 * (2 * 16) + *. 1 (16 **. 1) +. 7 * (16 ** 0)
Print (int ( '1100 is', 2)) # int can pass the first parameter, indicates what is in the end is the first parameter and hexadecimal to decimal
Print (int ( '14',. 8))
Print (int ( 'C', 16))


Other transfer Decimal Hex:
a divider, the other the original divided by the number of binary, ie: hexadecimal decimal conversion is divided by 16, is converted into a binary number is divided by 2 and the like.
So for example the decimal number 12 is converted into a binary '1100'

# 10 hex turn the other band
# 10 hex binary switch
# print (bin (12)) # 0b1100 0b indicates that the following number is a binary number
# Decimal turn octal
# print (oct (12)) # 0o14 0o representation the latter figure is the octal number 14 >>> 1 * (8 * 1) + 4 * (8 * 0))
# 10 hex 16 hex switch
# print (hex (12)) # 0xc 0x indicates that the following number is a hexadecimal number

Summary: integer to a value that does not exist orderly or disorderly, is immutable, whenever there is an index of the data is ordered, for example: List

Float (float)
action: height and weight pay
the salary a float # = 1.1 (1.1)
RES = a float ( '1.11')
Print (type (RES))
floating-point number string, float decimal point can be recognized therein, and can convert it to float.

 

Variable Type: if the value changes, id unchanged, indicating that change the original value of
immutable: the case of value changes, id must change, not change the original value, but the memory space to re-apply for a new value .


2, string


Is a piece of descriptive information having, enclosed by a pair of quotation marks, may be any type of string may be a dictionary, a list of numbers, etc.
#s = 'Hello Baby' # STR ( 'Hello Baby')
# S1 = STR ([1,2,3,4])
# Print (type (S1))
# S2 = STR ({ 'name': 'Jason', 'password': 123})
# Print (type (S2))
. 1 , by index value (forward + reverse take take): you can take, can not change the value of
# S = 'Hello Big Baby ~'
# Print (S [0])
# 2, a slice (care regardless tail, similar to Range, format [x: y: z], x the position specified at the beginning of the cut, y-cut end of the specified location, z representative of step (step not be representative of the large short character string taken directly, step is 1, can not write, originally 1)

# Print (S [0:. 5]) # Hello
# Print (S [0: 10:. 1]) # Hello step is not written by default. 1
# Print (S [0: 10: 2]) # step representing compartment take a few
#Learn negative value
# print (s [-1]) representative of the end of the string by index values
# print (s [0: 5 : -2]) # slice is from left to default values right, step size -2 contradicted
# print (s [5: 0 : -1]) # slice is from left to right in the default values
# print (s [-1: -10 : -1])


3, the length len
statistics is the number of characters in the string, the string is the string spaces
S1 = 'gfdsgd'
Print (len (S1))

4, operation members in and not in
determining whether there is a sub-string in a large string. If, returns a Boolean value (bool) True, not returns False.
Print ( 'Egon' in 'Egon IS DSB and Egon IS SB') # True
Print ( 'G' in 'Egon IS DSB and Egon IS SB') # True
Print ( 'Jason' not in 'Egon IS DSB and Egon IS sb ') # True


5、.strip()

Remove both sides of the character string around ".strip ()", first from the left side of the string inside the strip to clear the specified content, encounter other characters stop, then jump to the right to delete the specified content. Empty parentheses, it means delete the spaces on both sides of the string.
INPUT = username ( '>>>:') Strip ().
IF username == 'Jason':
Print ( 'Good Job')
Strip () remove the default string end to end space
method using the built-uniform symbol period (. )
NAME1 = 'Jason'
NAME2 = 'Jason' .strip ()
Print (== NAME1 NAME2)
NAME3 = 'ON $$$$ $$$$$ EG $'
Print (name3.strip ( '$'))


Can understand

= NAME4 'Jason & # ¥% *)'
Print (name4.strip ( '# ¥%)'))
The rstrip () the lstrip ()
NAME5 = 'Jason $$$$ $$$$'
Print (name5.lstrip ( ' $ ')) # clears the left of the specified character
print (name5.rstrip (' $ ') ) # clears the right of the specified character
6, sliced (split)

Segmentation split: some delimiter character strings for tissue, can be cut into split list, and further the value for
Data = 'n-JASO | 123 | Handsome'
Print (data.split ( '|'))
username , password, info = data.split ( ' |') # extract the assignment
print (username, password, info)
stressed: split out segmentation data type is a list of
print (data.split ( 'o') ) # cut in fact, in order from left to right
order print (data.split ( 'o', 1)) # actually cut from left to right in
order of print (data.rsplit ( 'o', 1)) # actually cut the left to right
if you do not specify the number of cuts "1", and then split the string rsplit it is all cut, as the left or right cut is implemented inside, and show the effect of the string is the same as
8 strings case conversion: .lower () ,. Upper ()
Lower string is converted to all lowercase letters;
Upper string letters all converted to uppercase;

9, startwith and endwith is to determine what is the beginning of a string of characters, the return value is TRUE or FALSE.

10, three play format (format recommended do Python output format), where the placement station to '{}', similar to direct with placeholders% s and D%
. 1) by location with placeholder% consistent s principle;
. str1 = 'My name IS {} My Age IS {}' the format ( 'Jason', 18 is)
. str1 = 'My name IS {} My Age IS {}' the format (18 is, 'Jason', )
2) by index placeholder, the specified input index number within braces, may comprise free stations;
. str1 = 'My. 1} {0} {My name IS {0} {0} Age IS' the format ( 'Egon ', 18)
3) The third placeholder name names (keyword parameter passing);
str1 =' '. the format (name = "My name {name} {IS} My Age Age IS {name} {name} Jason ', age = 18)

11, splice (join)

= Data 'Jason | 123 | Handsome'
res_list = data.split ( '|')
Print (res_list)
. res_str = '$' the Join (res_list) # container types spliced into a plurality of elements by a specified character string
print (res_str)

12, replace (old, new, count) old is the old string, string new new is the new, replacement count representative of the number of
STR = 'Egon Egon IS and IS DSB SB of He has BENZ A'
RES = str.replace ( ' Egon ',' Kevin ',. 1)
Print (RES)
Print (STR)
13,8, isdigit # determines whether a string contains purely digital
the while True:
Age = INPUT (' >>>: ')
iF Age. isdigit ():
Age = int (Age)
IF Age> 28:
Print ( 'good aunt')
the else:
Print ( "you can take input fuck ')


Built-in method you need to know


# 1, find, rfind, index , rindex return position of a character are located index value
S = 'Kevin Kevin IS and O IS DSB SB'
# Print (s.find ( 'DSB')) d # returns the character is located index value
# Print (s.find ( 'XXX')) #
# Print (s.find ( 'I', 0,3)) # can also be limited by the look index
# print (s.index ( 'o ')) # returns the index where the transmitted character
# print (s.index (' i ' , 0,3)) # returns the index where the transmitted character

Count count
number # print (s.count ( 'n' )) # statistical character appears

center

Left and right sides of the original string of the specified character symmetrically filled
S9 = 'Jason'
Print (s9.center (. 11, '*')) return '*** jason ***'


light

Element string on the left, the right fill new specified string, and the string together with the original value reaches a specified number of
ps: print (s9.ljust (40, '$'))

rjust

Supra, the original string in the right, filling the new specified string to the left, together with the original string and reaches the specified value of the number of
PS: Print (s9.rjust (40, ''))

zfill: "Z" represents a ZERO, filled with zeros filled in the right side of the original string

print(s9.zfill(40))

14, expandtabs
S10 = 'A \ TBC' # \ T Representative extended space
#print (s10.expandtabs (100))

15, captalize converted to uppercase first letter, other letters to lowercase

s12 = 'hElLo WoRLD sH10'
print(s12.capitalize()) # Hello world

swapcase invert case string
name = 'zaHNg ChAO'
Print (name.swapcase ()) # ZAhnG ChAO


title initials converted to uppercase, lowercase other
name = 'zaHNg ChAO'
Print (name.title ()) #Zahng the Chao
16, IS digital series
num1 = b'4 '#bytes, binary digits
num2 = u'4 '#unicode ,, a digital computer to identify, without adding to python3 u is the Unicode
num3 =' One '# Chinese digital
num4 =' Ⅳ '# Roman numerals
#' '.isnumeric (): unicode , Chinese numerals, Roman numerals, as long as identification numbers are expressed
Print (num2.isnumeric ())
Print (num3.isnumeric ())
Print (num4.isnumeric ())

# '' .Isdecimal (): unicode recognizes only ordinary Arabic numerals
Print (num2.isdecimal ())
Print (num3.isdecimal ())
Print (num4.isdecimal ())

# '' .Isdigit (): bytes , under normal circumstances the use of unicode isdigit already meet the needs of the
Print (num1.isdigit ())
Print (num2.isdigit ())
Print (num3.isdigit ())
Print (num4.isdigit ())
Therefore, a value of the string is stored as it is indexed, it is ordered; when the value of the index, the index value corresponding powerless;
when a built-in method, and not change itself, the result is returned to another value.

list file
definitions: there may be [] values within a plurality of any type, a comma-separated
three kinds of writing specifications list:
. 1) my_girl_friends = [ 'Alex', 'wupeiqi', 'yuanhao', 4,5]
2) = my_girl_friends List ([...])

. 3) L = List ( 'ABC')

list for the internal loop principle value is then stuffed into one list to
1, by accessing the index value (Forward + Reverse Access Access): may be taken to keep
l = [1,2,3, . 4]
Print (L [0:. 4:. 1])
Print (L [0 ::]) represents from 0 (the first number list) to start counting, to obtain a list of all values, do not write steps, i.e. steps of 1
Print (L [. 5 :: - 1]) -1 represents the inverse value in steps of 1, the list of all values taken
Print (ID (L))
L [0] = 69
Print (ID (L))
Print (L)
2, a slice (care regardless of the end, step)
L = [11,22,33,44,55]
L1 = [99,88,77,66]
1. Add a tail portion 66
l.append ( 66) # Note append data values can be added as an element in the list of
print (l)

2.insert anywhere additive element
l.insert (2,96) # additive element at an arbitrary position by the index
print (l) # Note insert a data element values can be added as a list

3. Add the container type data
L = [11,22,33,44,55]
L1 = [99,88,77,66]
l.append (L1)
l.insert (-1, L1)
l.extend (L1 ) # l1 internal principle for one loop is appended to the tail of the list, i.e., by adding the same to a list of two lists
l.extend ([45,]) # 45 and 45 it must be added to the l is a container, in order to 45 l added to the
Print (l)


4, del # del apply to any file you delete
res1 = l.pop () # tail pop (Chinese meaning "pop-up")
RES2 = l.pop ()
RES3 = l.pop ()
Print (res1, RES2, RES3 )


res1 = l.pop (0) # Specify the index pop-up element according to the index
print (res1)


res = l.remove (33) # specify the value to remove elements
Print (L)
Print (RES)

 

 

 

 

 

 



 

Guess you like

Origin www.cnblogs.com/zhangchaocoming/p/11129915.html