Basic Grammar Primer Learning Recommendation: Concise Python Tutorial
The following is only the supplement and focus of the introductory book
One, variable marker two, data type
- Number
- String
- Listing (list)
- Tuples (turple)
- Dictionary (dictionnary)
可变数据类型 * 整型 * 浮点型 * 字符串型 * 元组不可变数据类型 * 列表 * 字典
For more information about mutable and immutable data types, click here for more information.
Type conversions:
Chr (i) to turn an ASCII value into a character
Ord (i) turns a character or Unicode character into an ASCII value
Oct (x) turns integer x into a string represented by octal
Hex (x) turns integer x into a hexadecimal-represented string
STR (obj) gets the string description of obj
List (seq) converts a sequence into a list
Tuple (seq) converts a sequence into a tuple
Dict (), Dict (list) converted to a dictionary
int (x) is converted to an integer
Long (x) converts to a long interger
Float (x) converted to a floating-point number
Complex (x) converted to plural
Max (...) to find the maximum value
Min (...) to find the minimum value
Third, operator explanation
- Arithmetic operators
- Compare (relational) operators
- Assignment operators
- logical operators
- Bitwise operators
- Member operators
- Identity operator
- Operator Precedence
Iv. conditional statements
if 判断条件: 执行语句……else: 执行语句……
V. Circular statements
while 判断条件: 执行语句......(迭代)for iterating_var in sequence: 执行语句......
Vi. functions
Form: def function (parameters): statement return [expression]
Parameters:
- Required parameters (default parameters)
- Keyword parameter (when calling a function, the parameter position is: formal parameter = actual parameter)
- Default parameter (when defining a function, the parameter is assigned value)
- Indeterminate length parameter (*args: All the parameters except the prerequisites exist in a tuple, **kwargs: All parameters in the form of key values except the required parameters exist in one dictionary)
Anonymous functions:
variable = lambda [arg1 [, Arg2,..... argn]]: expression
Return statement:
When more than one parameter is returned: return [ARG1,ARG2,...] Returns a list, a tuple, a dictionary.
Global variables are used within functions, and global variables need to be declared by global.
Vii. objects
Eight, abnormal
一些特性:
Nine, module
import moudlefrom moudle import tool
Package:
部分函数
System-related Information module: Import sys
SYS.ARGV is a list that contains all the command-line arguments.
Sys.stdout Sys.stdin Sys.stderr respectively represents the standard input output, the error output of the file object.
Sys.stdin.readline () reads a line from the standard input sys.stdout.write ("a") screen output a
Sys.exit (Exit_code) Exit program
Sys.modules is a dictionary that represents all the available module in the system
Sys.platform getting running operating system environment
Sys.path is a list that indicates all paths to find Module,package.
Operating system-related calls and actions: Import OS
Os.environ A dictionary contains the mapping relationships of environment variables
os.environ["Home"] can get the value of the environment variable home
Os.chdir (dir) changes the current directory Os.chdir (' D:\outlook ')
Note windows is used to escape
OS.GETCWD () Get current directory
Os.getegid () Gets the valid group ID Os.getgid () gets the group ID
Os.getuid () Get user ID Os.geteuid () get a valid user ID
Os.setegid Os.setegid () os.seteuid () Os.setuid ()
Os.getgruops () Get a list of user group names
Os.getlogin () get user login name
OS.GETENV Get Environment variables
OS.PUTENV Setting Environment variables
Os.umask setting Umask
Os.system (CMD) runs the cmd command with system calls
Built-in modules (can be used directly without import) commonly used built-in functions:
Help (obj) online, obj is any type
Callable (obj) to see if an obj can be called like a function
Repr (obj) obtains a representation string of obj that can be used to reconstruct a copy of the object using the string eval
Eval_r (str) represents a valid Python expression that returns the expression
Dir (obj) to view the name visible in the name space of obj
Hasattr (obj,name) See if there is a name in the name space of obj
GetAttr (obj,name) gets a name in the name space of obj
SetAttr (Obj,name,value) is the name of an obj
A name in space points to Vale, the object
Delattr (obj,name) removes a name from the name space of obj
VARs (obj) returns the name space of an object. expressed in Dictionary
Locals () returns a local name space, denoted by dictionary
Globals () returns a global name space, denoted by dictionary
Type (obj) to view the types of an obj
Isinstance (OBJ,CLS) to see if obj is a CLS instance
Issubclass (subcls,supcls) View Subcls is not a subclass of Supcls
X. Documents
Python file (file) method:
The file object is created using the Open function, and the following table lists the functions commonly used by file objects:
| Method Name |
Description |
| File.close () |
Close the file. The file can no longer read and write after closing. |
| File.flush () |
Refreshes the file internal buffer, directly writes the internal buffer data immediately to the file, rather than passively waits for the output buffer to write. |
| File.fileno () |
Returns an integer that is a file descriptor (Descriptor FD Integer) that can be used in some of the underlying operations, such as the Read method of the OS module. |
| File.isatty () |
Returns True if the file is connected to an end device, otherwise False is returned. |
| File.next () |
Returns the next line of the file. |
| File.read ([size]) |
Reads the specified number of bytes from the file, if none is given or is negative. |
| File.readline ([size]) |
Reads the entire line, including the "\ n" character. |
| File.readlines ([Sizehint]) |
Reads all the rows and returns the list, and, given sizeint>0, sets how many bytes are read at a time, in order to alleviate the read pressure. |
| File.seek (offset[, whence]) |
Set the current location of the file |
| File.tell () |
Returns the current location of the file. |
| File.truncate ([Szie]) |
Intercepts the file, the truncated byte is specified by size, and the default is the current file location. |
| File.write (str) |
Writes a string to the file without a return value. |
| File.writelines (Sequence) |
Writes a list of sequence strings to the file and adds a newline character to each line if a line break is required. |
2.Python OS File/directory method:
See here
os 模块提供了非常丰富的方法用来处理文件和目录。
Attached: Python built-in functions in a detailed
Getting Started with Python--02 basic syntax