This should be the most detailed summary of basic Python syntax ever!
Let’s cut to the chase!
Define variables and use variables
Input Indicates the value entered by the user
Print Print values can output multiple variables
% d | % s placeholder for output variables
Strings (required for later crawlers)
Int () converts a string to a number
STR () converts numbers to strings
Len () gets the number of bytes taken up by a character
Strings can be concatenated with +
Character subscripts can be negative
STR [start position: end position: step size] slice, taking the string from the start to the end position, excluding the end position
Reverse the STR [: : – 1]
STR. The find () | rfind string search
STR. The index () | rindex () string search, and find different is the result of the return
Str.count (STR,start=0,end=len(STR)) Number of occurrences of the string
Str.replace (” STR “, “STR”, number of replacements) String replacement, which does not affect the original string
Str.split (” “) String split, parameters can be automatically split without writing
Str.capitalize () capitalizes the first character in a string
Str.title () capitalizes each word in the string
STR. Startswith (obj) | endswith (obj) to determine whether a string starting with obj | end, returns a bool
Str.lower () converts all uppercase to lowercase
Str.upper () converts all lowercase to uppercase
Str.ljust () returns a left-aligned string and fills it with Spaces to a new string of length width
Str.rjust (width) returns a right-aligned string and fills it with Spaces to a new string of length width
Str.center () returns a centered alignment of the original string and fills it with Spaces to a new string of length width
Str.lstrip () removes whitespace at the beginning of the string
Str.rstrip () removes whitespace at the end of the string
Str.strip () removes whitespace characters at both ends of the string
Str.partition () divides STR into three parts, before STR, STR, and after STR
Str.rpartition () divides STR into three parts, starting from the right, before STR, STR, and after STR
Str.splitlines () splits by row, returning a list of rows as elements
Str.isalpha () checks if the string is all letters
Str.isdigit () checks if all strings are numbers
Str.isalnum () checks whether the string is all numbers or letters
Str.isspace () checks if all strings are Spaces
Str.join (mystr) Adds a mystr after each character to build a new string
The operator
Add (+)
(-)
(*) Multiplication also makes the string repeat n times
In addition to (/)
(//) take quotient
(%) mod
(**) Pluto 2 to the tenth power 1024,2 to the sixteenth power 65536
If statement
Statement: (if condition: else condition:) Condition must be TAB key, otherwise it will be executed as normal code
Comparison operators: > | = | < = |! =
Logical operators or | and | not
if.. Elif elif… The else… statements
Looping statements
While loop — print(” “) newline end= “” no newline
For loop — You can loop strings directly
Break and continue break the loop
The list of
Similar to arrays in PHP
List =[data]
Add, delete, change and check list:
List. Append added
List. Insert added
List. extend merges lists, or the + operator can be used
List.pop () removes the last element of the list
List. Remove Can remove specified content only once
Del list [subscript] Deletes the specified subscript content
List [subscript]= modifies elements
Elements in | not in the list whether elements in the list
The dictionary
Dictionary ={key-value pair}
Add, delete, change and check dictionary:
Add: dictionary [key]=value
Delete: del dictionary [key]
[key]=value
Look up: dictionary.
Dictionary. keys: view the key of the dictionary
Dictionary values: View the values of the dictionary
Dictionary.items () : Tuples dictionaries
tuples
Tuple =(value)
Tuples can assign values to multiple variables: a,b= tuple
Values defined by tuples cannot be modified
If a tuple has only one argument, it must write an extra comma, otherwise it is not a tuple
Tuples can be empty
function
Function name (): content
Lists, dictionaries, can be used as global variables
Variable argument (holds data as a tuple): def function name (*args): content
Undefined arguments (which hold key-value pairs in dictionary form): def function name (**kargs): contents
The argument can be preceded by or * to indicate unpacking
.sort() sorts from smallest to largest
.sort(reverse=true) sort from large to small
The reverse () order
Use of lambda anonymous functions
Eval () converts to an expression
Swap two variables: a and B, for example,a=a +b,b=a-b,a=a-b,python can also write: A,b=b,a
Modifications can be made using +=, restricted to mutable types, lists, and dictionaries
Num +=num is not equivalent to num=num+num
File operations
Open () opens the file
Close () Closes the file
Read () reads the file
Write () to write documents
Readlines () reads data by line
Readline () Reads only one line of data
Seek (offset,from) locates the read/write,offset,from =0 at the beginning of the file,from =1 at the current position,from =2 at the end of the file, seek actually controls the file pointer
Tell () gets the current pointer position of the file
Rename the os.rename() file
Os.remove () deletes files
Os.mkdir () creates a folder
Os.getcwd () gets the absolute directory of the current operation
Os.chdir () changes the default operation directory
Os.listdir () gets a list of directories
Os.rmdir () deletes the folder
If you want to learn Python, I have prepared some Python materials for you. How to get them?