1. Introduction
Weak Type A variable can be assigned a value different from the original type
x = 1
x = "1" #不加分号, it is possible to assign a value of x to a string again
With or not, and or not
/#除法的结果是浮点数, 9/3 = 3.0
#结果是整数 10//3 = 3
Null value is none (equivalent to null)
Built-in type Numbers (integers and floating-point numbers), strings,lists
Print (")
Print ("percent") #% string prints single and double quotes
Print (R ' C:\Users\local ') #C: \user\local raw means that the equivalent of C # is not escaped
String spans multiple lines
Print ("' Content
Content ")
"Content"
2. String
Character encoding
ASCII uses a byte containing 127 encodings (case English, numerals, special symbols)
Unicode uses two bytes to represent one character (remote use of 4 bytes)
Utf-8 (8-bit Unicode transformation Format) variable-length encoding for Unicode
ASCII characters with one byte, Chinese characters using two byte encoding fully compatible with ASCII
Unicode encoding is used uniformly in computer memory and is converted to UTF-8 encoding when it needs to be saved or transmitted
Ord () #取字符的整数表示
Chr () #把编码转换为字符
Print (' \u4e2d\u6587 ') #中文
x = B ' ABC ' #x为bytes one character occupies one byte
x = ' ABC ' #x为str
String encoding (bytes str cross-turn)
Str->bytes ' STR '. Encode (' ASCII ') # Utf-8
Bytes->str b ' str '. Decode (' ASCII ')
String formatting
'%d,%f,%s,%x '% (2333,3.14, ' Python str ', 0x23)
'%2d-%02d '% (3,1) #3-01
'%.2f '% 3.1415 #3.14
Strings can be connected with +
Use * to repeat string 3 * ' A ' #aaa
Print (' a ' B ') #ab so that A and B will automatically connect and write less than A +
Word = ' Python '
Print (word[0]) #p
Print (Word[-1]) #n to the bottom of the first
Word[0:2] #Py
Word[2:5] #tho
Word[:2] #Py
Word[4:] #on
Word[-2:] #word [-2] + word[-1] On
String has immutability word[0] = ' A ' will error
3.list
x = [1, 2, 3, 4]
As a string, index is more convenient to use.
X[0] #1
X[1:2] #[2, 3]
X[-2:] #[3, 4]
y = [4, 5, 6, 7]
x + y #[1, 2, 3, 4, 5, 6, 7] set and operate
Tuple
With the list type, but where the elements are not mutable
4. sharding
A bit of Matlab feeling
L = [n/A]
L[0:2] l[0] l[1]
L[1:] #L [1] to the last element or written l[1:-1]
L[-1] Last element
L[:10:2] #前10个数, take one of each two [0,2,4,6,8]
5.map () & Reduce ()
Map (func, [+])
Performs a func once for each element in [all-in-one]
Reduce (func, [+)]
Func (func), 3)
Practice Answers
Input: [‘adam‘, ‘LISA‘, ‘barT‘] , output: [‘Adam‘, ‘Lisa‘, ‘Bart‘] :
def Normalize (name): = Name[0].upper () for in name[1:]: = first + i.lower () return = ['Adam'LISA ' BarT'= List (map (normalize, L1))print(L2)
6. Filter ()
Filter (func,[1,2,3,4])
Similar to map, call Func with each element as a parameter, return the hold for ture, or discard
Sorted ([2,3,4,5,6,7],key = comparer, reverse = false)
A comparison method allows you to quickly sort
Reference links
Strings and encodings-Liaoche's official website
Https://docs.python.org/3/tutorial/introduction.html
[Python] Python Learning Record