This blog is only for my spare time to record articles , Publish to , Only for users to read , If there is any infringement , Please let me know , I'll delete it .
This article is pure and wild , There is no reference to other people's articles or plagiarism . Insist on originality !!
Hello . Here is Python Reptiles from getting started to giving up series of articles . I am a SunriseCai.
This article is my usual study Python Some knowledge points commonly used in , Don't be Python System learning .
This is simpler , It is recommended to click the link directly to check the usage .
data structure | features |
---|---|
list list | variable , Orderly |
Tuples tuple | immutable , Don't add, delete, or alter |
Dictionaries dict | Exists as a key value pair |
aggregate set | variable , disorder |
Definition :
Place holder | Indicates the type |
---|---|
%s | character string |
%d | Integers |
%f | Floating point numbers |
format() | hold % Replace with {} To achieve formatted output |
Place holder % Example :
name = input(' Type in your name :') # SunriseCai
age = input(' Enter your age :') # 20
weight= input(' Enter your weight :') # 65.432
print(' Your name is %s, Age is %d, Height is %.2f kg ' % (name, int(age), float(weight)))
# Your name is SunriseCai, Age is 20, The weight is 65.43 kg
# %.2f It means taking 2 Decimal place
format() Format example :
name = input(' Type in your name :') # SunriseCai
age = input(' Enter your age :') # 20
weight = input(' Enter your salary :') # 65.432
print(' Your name is {}, Age is {}, The weight is {} kg '.format(name, age, weight))
# Your name is SunriseCai, Age is 20, The weight is 65.432 kg
Python Programming if Statement is used to control the execution of a program , The basic form is :
if Judge the condition 1:
Execute statement A
elif Judge the condition 2:
Execute statement B
else:
Execute statement C
To understand is : If meet Conditions 1 execute A, dissatisfaction Conditions 1 But satisfied Conditions 2 execute B, If neither of them is satisfied, they will execute C.
Example :
num = 10
if num > 10: # Judge num
print(' I am bigger than 10')
elif num < 10: # num Less than 10 It outputs
print(' I am smaller than 10')
else: # If the above is not satisfied, output
print(' I am equal to 10')
# Final output I am equal to 10
Here's a quote from the rookie tutorial Python Loop statement .
Type of cycle | describe |
---|---|
while loop | The given judgment condition is true When the loop is executed , Otherwise, push out the circulatory body |
for loop | Repeat the statement |
while Loop example :
while True:
string = input(' Input string :')
if string == 'end':
break # Exit loop
print(string)
for Loop example :
for i in range(100):
print('Hello World')
Method | describe |
---|---|
Python join() | Used to connect elements in a sequence with specified characters to generate a new string . |
Python strip() | Used to remove characters specified at the beginning and end of a string ( The default is space or newline ) Or character sequence . |
Python split() | Specify the separator to slice the string , If parameters num There is a specified value , Then separate num+1 Substring |
Example :
string = ['SunriseCai', 'SunriseCai', 'SunriseCai']
print('--'.join(string)) # SunriseCai--SunriseCai--SunriseCai
string = '%#SunriseCai%#'
print(string.strip('%#')) # SunriseCai
string = '%#SunriseCai%#'
print(string.split('%#')) # ['', 'SunriseCai', '']
Python Commonly used try/except Statement fetch catch exception .
Common use :
try:
Execute statement A
except Exception as e:
Execute statement B
# raise NameError # once raise perform ,raise None of the following statements will be executed
finally:
Execute statement C
Example :
try:
print(1 / 0) # 1 Divide 0 Will report a mistake
except Exception as e:
print(e)
finally:
print('SunriseCai')
# division by zero
# SunriseCai
The method called by the module :
Method | effect |
---|---|
import module | The import module |
import module as xx | Rename the import module to xx |
from module import xx | Import the xx function |
from module import xx as bb | Module functions to be imported xx Renamed bb |
import time
time.sleep(10) # here Python The program will sleep 10 second
import time
print(time.time()) # 1578838874.4708762
import time
stamp = time.time()
print(time.strftime('%Y-%m-%d', time.localtime(stamp))) # 2020-01-12
import time
stamp = time.time()
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(stamp))) # 2020-01-12 22:20:20
Method | describe |
---|---|
os.listdir(path) | return path The specified folder contains a list of files or folder names . |
os.path.exists(path) | Judge the document ( Folder ) Existence or not |
os.makedirs(path) | Recursive folder creation function |
os.removedirs(path) | Recursively delete directories . |
os.rmdir(path) | Delete path Empty directory specified , If the directory is not empty , Throws an error |
os.remove(path) | The deletion path is path The file of . If path It's a folder , An error will be thrown |
It's usually os These two methods of os.path.exists(path) and os.makedirs(path) Most used . Judge whether the document exists or not , Then judge if the next file needs to be created .
function | describe |
---|---|
json.dumps | take Python The object is encoded as JSON character string |
json.loads | To encode JSON The string is decoded to Python object |
above , Is in Python Reptiles Very few knowledge points commonly used in .
There are many knowledge points and modules that will be used , Here, just remember some of the most used .
Finally, I will summarize the content of this chapter :
Next article , be known as 《Python Reptiles from getting started to giving up 03 | Python Advanced usage in reptile 》.