python third day of study
First, make up for yesterday's arrears:
for loop
A for loop in python can loop items of any sequence, such as a list or a string
Each element in a list can be looped
Grammar:
lb = 'asdasd' for a in lb: print(a)
Result entered:
a s d a s d
for can be indexed independently and is a general circular finger retrieval method
The number of loops for is determined by the number of values contained in the loop object, while the number of loops for while is determined by the condition
for loops can also be indexed
Example
for i in range(0,5): # Range without regard to end Range means range (0,5). Range (0,5) means range 0,1,2,3,4 because it does not care about end and end. print(i)
The for loop takes values by index:
name_list = ['nick', 'jason', 'tank', 'sean'] for name in name_list: if name == 'tank': break print(name)
Today's Lesson:
I. Integer and Floating Point Types
2. String Types
3. List Types (List Types are not fully understood and have to be completed tomorrow)
1. Integer int
An integer is a value. Ordered: because an int type can be indexed. The data type is immutable
Data can be invariably divided into two categories:
a. Variable type: if the value changes, the id does not change, indicating that the original value is changed
b. Invariant type: if the value changes, the id changes
Basic use of integers: mobile phone number, qq number, etc..
Defined in:
age = 25 # age = int(25) print(age,type(age))
Other types cast int types:
Only strings of pure numbers can be converted, but decimal points cannot be converted because the int type is integer and decimal points are not recognized
Floating-point and integer types are the same because both are Number types, except that cast converts decimal points to be identical to integers
2. String type:
Strings are the most common data type in python.We can use single quotation marks, double quotation marks, three double quotation marks to create string string types that can be indexed, so string types are ordered type strings whose purpose is descriptive
Definition:
shili = 'My first string example' print(sshili)
1. Strings can be indexed (positive and negative values) and can be changed but cannot be stored
s = 'hello big baby'
print(s[0])
s[0] = 'j' # Index values in lists can only be taken or not changed
2. Slice Value, what is Slice Value?A slice's value is to extract a new small string from a large character (slice's value is ange-like)
s = 'hello big baby' print(s[0:10]) #Step without writing represents step 1 print(s[0:10:3]) # Step represents taking one every few
3. Length len: counts the number of characters in a string
s1 = ' ' print(len(s1))
4. Membership operations in and not in: Determine whether a substring exists in a large string
l = 'ai wei da li mian de yuan gong you : zhang yu long '
if 'zhang1' not in l :
print('000')
elif 'yu' in l :
print('long')
5\strip removes the spaces on the front and back sides of the string, regardless of whether the middle takes only the spaces at both ends
strip() removes spaces at the beginning and end of a string by default; uses built-in methods to unify the use of a period (.)
strip means to remove the space on the left, lstrip means to remove the space on the right
name1 = 'yl' name2 = ' yl '.strip() print(name1 == name2)
Example 2:name3 = '$$$$$$jinyan******'
print(name3.strip('$*')) # Remove trailing characters
Example:username = input(">>>>:").strip() #Automatically recognizes that the input is a string that can be strip ped directly to remove the first space
if username == 'laonanhai':
print('goog job')
Example removes left and right:name4 = '&&&&zhang%%%%%'
print(name4.rstrip('%')) # rstrip() right Right
print(name4,lstrip('&')) # Lstrip() left of lrft
6. Slicing split: For strings organized by a delimiter, split can be used to slice them into lists for values
spit means to split or slice; split() is left to right; and emphasizes that data sliced using split() is a list type
If you do not specify the number of cuts split and rsplit are the same ps because they are cut from within, we do not know
i = 'mx|big|b2' print(i.split('|')) name, daxiao,age = i.split('|') print(name,daxiao, age) print(i.split('b',1)) # split is left to right print(i.rsplit('b',1)) # rsplit() is right-to-left tangent
7. Cycle for in
data = 'sdasfsddasdasdas' for i in data: print(i)
2.lower and upper
lower is lowercase
upper is uppercase
s = 'zHanG' print(s.lower()) # To lower case print(s) print(s.upper()) # Uppercase print(s)
3.startswith and endswith
startswith determines whether or not it starts with something
endswith is interpreted as ending with what or not
if s1.startswith('z'): print('First part test passed!') else: print('First detected as pass') if s1.endswith('e'): print('End detection passed') else: print('Last bit detection failed')
4. Replacement format for Strings
The first is recommended for use by python just like the principle of placeholders and%s
Example:
str1 = 'my name is {} my age is {}'.format('yl',18) print(str1)
Second kind of index placeholder
str1 = 'my name is {1} my age is {0}'.format('yl',18) str1 = 'my na{0}me{1} is {3} my a{2}ge is {4}'.format('yl',18,'zz',56,2) print(str1)
The third kind of placement for named names, that is, keyword placement
str1 = 'my na{yu}me{age} is {yu} my a{yu}ge is {yu}'.format(yu = 'long',age=18) print(str1)
5. Splicing join
Joins elements of a container type into a string using specified characters
i = 'jon|123|zb' res_list = i.split('|') # Separate into lists res_str = '¥'.join(res_list) print(res_str)
6.replace Replacement
- Change the elements in the list
str = 'egon is dsb and egon is sb he has a BENZ' res = str.replace('egon','kevin',1) print(res) print(str) str = 'yu i xsg and yu is nq he have a mm' # Declare a list of STRs res = str.replace('yu','zhang',2) #Declare that a new list uses replace() to replace the data in the original string. print(res) # Print a new string print(str) # Print old string, old string still unchanged
8. isdigit is used to determine whether a character is a pure number
age = input('>>>>:') if age.isdigit(): age = int(age) if age < 23: print('Hello little sister') break else: print('Old Aunt') break else: print('Please type in')
Built-in methods to understand
find lookup; rfind; index; rindex; count count count
s = 'kevin is dsb o and kevin is sb' print(s.find('dsb')) # Returns the index value for the d character print(s.find('xxx')) # Return -1 without error when not found print(s.find('i',0,7)) # You can also limit the scope of a lookup range index by indexing it so that it stops and returns the index location of the lookup value after finding the first print(s.index('o')) # Returns the index value of the character passed print(s.index('i',0,3)) # Returns the index value of the character passed #Errors will occur print(s.count('n')) # Count the number of occurrences of characters
2; center means the middle: the original string has only 4 digits, we want to type 10 digits and we need 4 digits to center to use center
ljust and rjust mean the opposite, one on the left and one on the right
3.expandtabs Expand tab key
s10 = 'a\tbc' print(s10.expandtabs(100)) s5 = 'a\tnu' print(s5.expandtabs(12))
4,coptalize ; swapcase ; title
- coptalize initials uppercase
swapcase case case interchange
Tile capitalizes the first letter of each word
s122 = 'GOkon hjJKO nHk' print(s122.capitalize()) # Capitalize the first letter of a string print(s122.swapcase()) # Interchange of case print(s122.title()) # title case
5.is Digital Series
num1=b'4' #bytes num2=u'4' #unicode,python3 unicode without adding u num3='one' #Chinese Numbers num4='Ⅳ' #Roman Numbers # ''.isnumeric(): unicode, Chinese numeral, Roman numeral is recognized whenever it means a number # print(num1.isnumeric()) #Report errors print(num2.isnumeric()) print(num3.isnumeric()) print(num4.isnumeric()) # # ''.isdecimal(): unicode only recognizes common Arabic numerals print(num2.isdecimal()) print(num3.isdecimal()) print(num4.isdecimal()) # # ''.isdigit(): bytes, Unicode normally uses isDigit to meet demand print(num1.isdigit()) print(num2.isdigit()) print(num3.isdigit()) print(num4.isdigit())
3. List I don't know what to add tomorrow