Python full stack development class notes_day03

Details of the knowledge points learned today:
1. Basic data type initial:
int: used for calculation.
str: used to store a small amount of data such as: 'Alex', '123':
bool:True, False
list: (list): various data can be put in it, which can store a large amount of data and is easy to operate
The format of the list is: ['name', True, [ ]... ]
tuple: (tuple, also called read-only list)
The format of the tuple is: ('name',True,[ ]...)
dict: (dictionary): stores a large amount of relational data -----------> key-value pairs
The format of dict is: {'name': 'Old Boy', 'name_list': ['Reverse teaching material', 'Xiaohua', 'Xiaohong'...], 'alex':{'age':40,'hobby ':'old_women',...},...}
set: (collection)
The format of set is: {'wusir','alex',...}
2. Int details: bit_length() #The minimum number of digits converted from decimal to binary
i=5
print(i.bit_length()) //3
Convert between int and str: int<-------->str
str------------>int int(str) The condition is that the string must be all numbers
int----------->str str(int)
3.bool detailed explanation:
Convert between bool and int: bool<--------->int
bool--------->int True----->1 False------>0
int------------>boo non-zero is True, zero is False
Convert between bool and str: bool<--------->str
bool----------->str
str-------------->bool is True if not empty (for example: space)
''Empty string--------->False
4.Str details: #str knowledge points: index, slice, step size
s='python12 period'
s1=s[0]
print(s1) //p
Slicing: the head and the tail
Forward slice:
s2=s[0:5] //pytho---------->s2=s[0:6] //python
s3 = s [: 6] // python
s4=s[1:-1] //ython12
s5=s[1:] //ython12
s6=s[:] //python12 period (not s, but copy s)
s7=s[:5:2] //pto ----------> Take one every other, 2 is the step size
s8=s[4: :2] //01 issue
s9=s[2:6:3] //tn------------> Take one every 2, 3 is a step
The reverse slice must be followed by a reverse step
s10=s[-1:-5:-1] //period 21n
#5. Common operation methods of strings
①**capitalize() //Returns a copy of a string with the first character uppercase and the rest lowercase.
s = 'laoNANhai'
s1=s.capitalize()
print(s1) //Laonanhai
②*center(length of string, padding) //Centering, padding is done using the specified padding character (the default is ASCII space). If the width is less than or equal to len(s), the original string is returned.
s2=s.center(20,'$') //$$$$$laoNANhai$$$$$$
③***upper()/lower()
str.upper() //Returns a copy of the string with all uppercase and lowercase characters converted to uppercase.
Note that str.upper().isupper() may be false
s3=s.upper() //LAONANHAI
str.lower() //Returns a copy of the string with all uppercase and lowercase characters converted to lowercase.
Note that str.lower().islower() may be false
s4=s.lower() //laonanhai
upper()/lower() application:
 
④***startswith()/endswith()
str.startswith('character or string', [the start subscript of the slice, [the end subscript of the slice]]) //If the string starts with a prefix, return True; otherwise, return False. prefix can also be a tuple of prefixes to look up. With an optional start, the test string starts at that position. With an optional end, stop comparing strings at that position.
 
Note that the string can be sliced ​​when encountering start and end, and the slices are separated by commas
s5=s.startswith('l') // True
s6=s.startswith('lao') // True
s7=s.startswith(‘N’,3,6) // True
str.endswith('character or string', [the start subscript of the slice, [the end subscript of the slice]]) //same as str.swith
print(str.endswith('oN',0,6)) #False
print(str.endswith('oN',0,4)) #True
⑤*swapcase() //Returns a copy of a string with uppercase characters converted to lowercase and vice versa.
print(str.swapcase()) #LAOnanHAI
Please note that ----->s.swapcase().swapcase()==s is not necessarily correct???
Add a little new knowledge: ord() and hex( )
ord() #Given a string representing a Unicode character, return an integer representing the Unicode code point of that character. For example, ord('a') returns the integer 97 and ord('Euro') (the euro sign) returns 8364. This is the inverse of chr().
hex( ) #Convert an integer number to a lowercase hexadecimal string prefixed with 0x. If x is not a Python int object, you must define an index() method that returns an integer.
⑥*title() //The first letter of each word that is not separated by letters must be capitalized
print("they're tom's best friend.".title())
#They'Re Tom'S Best Friend.
⑦*** Find the index by element find() / index()
str.find('string', [start index of slice, [end index of slice]])
//Returns the index of the first occurrence of the string to be searched in the original string (or within the slice range). If not found, return -1.
str = 'laoNANhai'
print(str.find('A',2,5)) #4 (The returned index value is not the index value of the slice but the index value of the original string)
print(str.find('A',2,4)) #-1 (find() cannot find return -1)
str.index('string', [start index of slice, [end index of slice]])
//Returns the index of the first occurrence of the string to be searched in the original string (or within the slice range). If it is not found, an error will be reported.
str = 'laoNANhai'
print(str.index('A',2,4)) #ValueError: substring not found
⑧***strip(): The main function is to remove spaces, newlines, and tabs at the front and rear of the string
str = '\tAlex\n'
print(str) # Alex
 
str1 = str.strip()
print(str1) #Alex
One of the applications of strip():
The second application of strip():
str = 'ablabaexsba'
print(str.strip('a')) #blabaexsb
print(str.strip('abs')) #labaex
Two extended -----> one is lstrip(), the other is rstrip()
print(str.lstrip('a')) #blabaexsba
print(str.rstrip('a')) #ablabaexsb
 
print(str.lstrip('abs')) #labaexsba
print(str.rstrip('abs')) #ablabaex
⑨****split('string for splitting', the maximum number of splits)
Separated by spaces by default
s = 'wusir alex taiba'
print(s.split()) #['wusir', 'alex', 'taiba']
s = 'wusir,alex,taiba'
print(s.split(',')) #['wusir', 'alex', 'taiba']
s = 'QwusirQalexQtaiba'
print(s.split('Q')) #['', 'wusir', 'alex', 'taiba']
s = 'QwusirQalexQtaiba'
print(s.split('Q',2)) #['', 'wusir', 'alexQtaiba']
⑩*join (iterable object) ---------------> In some cases, the list (list) can be converted into a string (str) Note: list---- ---------->str can only use strings
s = 'alex'
s1 = '*'.join(s)
print(s1) #a*l*e*x
 
list_of_s = ['alex','wusir','taibai']
s = ' '.join(list_of_s)
print(s,type(s)) alex wusir taibai <class 'str'>
⑪repalce ('replaced string', 'replaced string', [the number of times the replaced string needs to be replaced])
 
 
#6. Common common methods of primitive data types
①len (string, byte, tuple, list or set (such as dictionary) --------------------> total number
s = 'ssfjaskhjkahe987#%2sl'
print (len (s)) # 21
②count('the string to count the number of occurrences', [the beginning of the slice, [the end of the slice]])
----------------> Calculate the number of occurrences of certain elements, which can be sliced
s = 's2sfjaskhjkahe987#%2sl'
print(s.count('j',2,-4)) #2
③format() formatted output:
 
④isalnum()----------------------------> Whether the string is composed of letters or numbers. Returns true if all characters in the string are alphanumeric and there is at least one character, otherwise.
⑤isalpha()---------------------> Whether the string consists of letters only. Returns true if all characters in the string are alphabetical and there is at least one character, otherwise true.
⑥isdigit()------------------------->The string consists of digits only. Returns true if all characters in the string are digits and there is at least one character, otherwise. Formally, a digit is a character with the attribute value digit=digit or digit=decimal.
s = 'jinxin123'
print(s.isalnum()) #True
print(s.isalpha()) #False
print(s.isdigit()) #False
#7. Use while and for loops to output each character of the string respectively.
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325141638&siteId=291194637
Recommended