first, the data type
1. Digital
int (integral type)
Long (integer)
Float (float type)
Complex (plural)
2. Boolean value
3. String
Second, the variable
Variable naming rules:
Variable names can only be any combination of letters, numbers, or underscores
The first character of a variable name cannot be a number
Variable names cannot be keywords (for example: And,or,continue,break,in,else,print, etc.)
three, string splicing
1. Use the plus sign (+)
Name = "Tom" age = 25print (name + "s age is" + str (age)) #输出: Toms 25
2. String formatting
Name = 25 (%
PS: In Python, using the + sign connection string, each occurrence of a + number, it is necessary to re-apply a piece of space in memory, how many + number, the number of space to apply. Generally do not build using the + sign connection string.
Iv. lists and tuples
1. Lists (list)
Str_list = [' Tom ', ' Lucy ', ' Mary '] or str_list = List ([' Tom ', ' Lucy ', ' Mary '])
STR_LIST[0]
Str_list.append (' Lilei ') print (str_list) #输出: [' Tom ', ' Lucy ', ' Mary ', ' Lilei ']
Str_list.insert (1, ' Lilei ') print (str_list) #输出: [' Tom ', ' lilei ', ' Lucy ', ' Mary ']
Str_list.remove (' Lucy ') print (str_list) #输出: [' Tom ', ' Mary ']
Str_list = [3,4,5,6,7,8,9]new_1 = Str_list[1:3] #从索引1开始取, fetch to index 3new_2 = str_list[0:6:2] #从索引0开始取, fetch every two bits, to 6th place new_3 = str_list[-2:] # take the back 2 numbers new_4 = Str_list[:3] # Take the first 3 numbers new_5 = Str_list[::3] #所有数, each 3 Take one print ( new_1,new_2,new_3,new_4,new_5) #输出: [4, 5] [3, 5, 7] [8, 9] [3, 4, 5] [3, 6, 9]
2, tuple (tuple)
Age = (18,25,33) or age = Tuple ((18,25,33))
In addition to the inability to modify, add, and delete elements, other operations tuples and lists are almost identical.
Five, dictionary
How to use Key-value storage
Phone = { ' Zhang San ': ' 13075632152 ', ' John Doe ': ' 15732015632 ', ' Harry ': ' 13420321523 ',}
Print (phone[' Zhang San ']) #如果key不存在, will error, key in brackets loaded print (phone.get (' Old Yellow ')) #如果key不存在, return none,key with parentheses loaded # Output: 13075632152# None
phone[] = phone[] =
Phone.pop (' Zhang San ') #第一种方法del phone[' John Doe ') #第二种方法phone. Popitem () #随机删除某一个
For key in Phone: print (Key,phone[key]) #输出: # Harry 13420321523# Zhang San 13075632152# lee 415,732,015,632
Phone = {' hr ': {' Lao Zhang ': ' 13700112233 ', ' Lao li ': ' 13432023152 '}, ' finance department ': {' Xiaoli ': ' 13230555666 ', ' Xiao Ying ': ' 13723688888 '}, ' Technical department ': {' Lao Luo ': ' 13866666333 '}}print (phone[' hr ' [' Lao Li ']) #输出: 13432023152
VI. If statement
1, If...else
Age = 16if <18: print (' You're underage ') Else: print (' You're grown up ')
2, If...elif....else
Score = 85if Score > 0 and score<: print (' Your grades fail ') Elif score >= and score <80: print (' Your grades passed ') Elif score>=80 and score<90: print (' Your score is good ') Else: print (' Your score is good ')
Seven, while loop
I=0num=0while i<=100: num+=i i+=1print (' 1-100 cumulative equals%d '%num)
Eight, for...in cycle
num = []for i in range: num.append (i) print (num) #输出: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Ix. user interaction (input)
Name = input (' Please enter your name: ') height = input (' Please enter your height: ') print ('%s ' height%s cm '% (name,height))
X. Basic operation of the file
Open file: F = open (' File path ', ' mode ') or with open (' File path ', ' mode ') as F:
Mode:
R: Open File as read-only
W: Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
A: Open a file for append. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
w+: Opens a file for read-write. (When the file is opened and emptied, can you read anything?) )
A +: Opens a file for read-write.
Read the file:
Read () ReadLines () ReadLine () usage
f = open (' D:/test.txt ', ' R ') #以只读方式打开文件print (F.read ()) #read () reads the entire contents of the file for line in F.readlines (): # ReadLines () reads the entire file and presses the row into the list print (Line.strip (' \ n ')) #去掉行尾的 ' \ n ' while 1: line = F.readline () # ReadLine () reads only one line of print at a time ( line.strip (' \ n ')) If not lines : breakf.close () #关闭文件
Write file:
F =open (' d:/test.txt ', ' a ') f.write (' hello,boy!\n ') f.close ()