A basic data types .python
1. int ==> integer. Use main math for trekking into ⾏
2. str ==> terminated string, a small amount of data can be stored and Fixing a corresponding operation trekking
3. bool ==> true and false judgments, True, False
4. list ==> zoomed large amount of data storage. Using a [] are shown in Table PLAY
5. tuple => tuple, can not change the raw Using a hair ⽣ () shown in Table PLAY
6. dict ==> dictionary, save key-value pairs, ⼀ the same amount of data you can save a lot zoomed
7. set ==> set zoomed save a lot of data volume. Can not be repeated. In fact does not save the value of dict
II. Integer (int)
In python3 all integers are of type int, but in python2 if the data amount zoomed relatively large. Use will use type long. Long type does not exist in the python3
Trekking can perform while the integer operation: BIT_LENGTH (). Calculation Using binary integer representing ⼆ in memory with a length ⻓
III. Boolean value (bool)
Only the value True, False. Bool value is not operating.
Conversion issues:
str => int int(str)
int => str str(int)
int => bool bool (int). 0 are non-zero True is False
bool => int int (bool) True is 1, False 0
str => bool bool (str) string is an empty string False, True is not empty
bool => str str (bool) converting a bool value corresponding to the "value"
IV. String (str)
The character strings to be connected. Use in python with ',', '', '' 'due to the content of the string is called a string.
4.1 slices and index
1. Index The index is a subscript. Remember, the subscript starts at 0
S1 = ' wild lily spring ' Print (S1 [0]) # Get the first 0 Print (S1 [. 1 ]) Print (S1 [2 ]) Print (S1 [. 3 ]) Print (S1 [. 9]) # No 9, a cross-border, being given Print (S1 [-1]) # -1 represents the inverse of a first
2. slice, we can use the index used to intercept the content portion terminated string
Syntax: STR [start: end] Rule: care regardless Ding, taken from the start to the end start position taken but not including end
= S2 ' wild lily spring ' Print (S2 [0: 3]) # 0 obtained from 3, 3 does not contain the results: lily Print (S2 [. 5:. 9]) # If the maximum on the right, corresponding to is eligible for the last Print (S2 [. 4:]) # if you want to get the last, the last one may not give Print (S2 [-1: -5]) # acquired from -1 to -5 this is to obtain any results . right number from -1 how you count the number can not -5. Print (S2 [-5: -1]) # .. ⽜ b, to get the data, but care regardless of how Ding taken after ⼀. ? a it Print (s2 [: - 1]) # this is the inverse of the first frame to take a Print (s2 [:]) # is output
Leaping interception
Print (S2 [1: 5: 2]) # from the first frame beginning to take, get to the fifth, every 2 to take a result: one hundred also Analysis: 1: 5 => Lily also => one hundred also Print (S2 [:. 5: 2]) # . scratch every fifth to take two ⼀ a Print (S2 [4 :: 2]) # . 4 from the start to get taken every two months ⼀ Print ( S2 [-5 :: 2]) # . taken from -5 to take every two months ⼀ Print (S2 [-1: -5: -1]) # step ⻓ long time is -1 on the right. the value of the left Print (S2 [-5 :: -. 3]) # .. from the beginning to the penultimate 5 starts every three months take ⼀
Step ⻓ length: If is an integer, taken from left to right then from right to left with a negative value is set to a default 1...
Slice syntax: str [start: end: step] start: start position end: end position step: step length ⻓
Related operating methods ⽅ 4.2 terminated string
Remember, terminated string objects are immutable, any operation on the original string is a string not have any influence
1. zoomed ⼩ write and turn
= S ' the I Was A Boy PRETTY ' Print (s.capitalize ()) # capitalized Print (s.lower ()) # all lowercase Print (s.upper ()) # all uppercase Print (s.title () ) # each separated by a special character letters capitalized Print (s.swapcase ()) # case interchangeable Print (s.casefold ()) # is converted to lowercase differences, and the lower of: lower () on a these character support is not good enough .casefold () is valid for all the letters, such as Eastern Europe ⼀ some letters Print (s.center (10, ' * ' )) # stretched to 10, the original string in the middle. fill the remaining positions * Print (s.strip ()) # Strips spaces if the left and right ends of the cut break. An empty string will then it must appear in the left and right ends. Please note pit Print (s.split ()) # string cutting, according to cut Print (s.replace does ( ' Was ' , ' AM ' )) # replace Was to AM the while . 1 : username = iNPUT ( ' enter username: ' ) password = iNPUT ( ' enter password: ' ) IF username == ' Wang ' and password = = ' 123 ' : Print (' Login successful ' ) the else : Print ( ' login failed ' )
2. Output Formatting
s1 = " My name is% s, year% s years old, I like% s " % ( ' alex ' , 18, ' basketball ' ) Print (s1) s2 = ' My name is {} year {} years old, hobby {} ' .format ( ' baby ' , 18, ' tennis ' ) Print (s2) S3 = ' my name is {1}, {2} this year, like 0} { ' .format ( ' soccer ' , ' Ada ' , 19 ) Print (S3) S4 = 'My name is {name}, {age} years old this year, like Hobby} { ' .format (name = ' Bob ' , Age = 20, Hobby = ' movement ' ) Print (s4)
4. Find
= S13 " My name is sylar, I like python, java, c and other programming your language and words. " RET1 = s13.startswith ( " Sylar " ) # determine whether to begin with Sylar RET2 = s13.endswith ( " your language and speech " ) # whether the 'language' end RET3 = s13.count ( " a " ) # Find the number of "a" appears ret4 = s13.find ( " Sylar " ) # Find 'sylar' position appears ret5 = s13.index ( " Sylar " ) # seek index. Note. If you can not find the index. program will complain
5. Analyzing conditions
= S14 " 123.16 " S15 = " abc " S16 = " _abc! @ " # whether letters and numbers Print (s14.isalnum ()) Print (s15.isalnum ()) Print (s16.isalnum ()) # if the alphabet Print (s14.isalpha ()) Print (s15.isalpha ()) Print (s16.isalpha ()) # whether consist of numbers, excluding the decimal point Print (s14.isdigit ()) Print (s14.isdecimal ( )) Print (s14.isnumeric ()) # this more cattle B. Chinese have identification. Print (s15.isdigit ()) print(s16.isdigit())
6. Calculate length of string
= S18 " I am your eyes, I am also A " RET = len (S18) # calculate string ⻓ degree Print (RET)
Note: len () is a python's built-in functions to access ⽅ way so as not ⼀ you remember len () and print () ⼀ as a trekking on the ⾏.
7. iteration
We can use the toilet for loop to Lilly (acquisition) of each string of the string a character ⼀
grammar:
for a variable amount in iterables:
Pass
Iterables: You can ⼀ object ⼀ a value out of
S19 = " Hello everyone, I'm VUE, front-end kids you okay.? " # while loop index = 0 while index < len (S19): Print (S19 [index]) # use the index sections to complete characters Find index = index +. 1 # for loop, each of the s19 ⼀ out a character assigned to the former C for C in s19: Print (C) '' ' in in two ways: 1. in is for each element of the acquired variables assigned to the front. 2. not for the. determines whether xxx appears in str. '' '