Current location - Quotes Website - Signature design - Summary of Python basics
Summary of Python basics

input("Information")

For example:

input("Please enter a number")

Because Python has no special The data type is artificially specified, and the data type is determined by the computer, so the data input by our input() is processed as a string by default. If you want to enter some numbers, you need the eval() evaluation function to evaluate the string and turn it into statement(number).

print(...)

Default is a blank line. If you want no blank line, then

print(...., end = "")

Features:

Base:

Features:

There is an uncertain mantissa in the operation between floating point numbers, it is not a bug

For example: 0.1+0.3 → 0.4

0.1+0.2 → 0.30000000000000004

This is because all data in the computer are stored in binary, and some floating Points cannot be completely converted into equal binary numbers, they can only be infinitely close to binary numbers.

For example: 0.1 →

Solution:

Rounding:

For example: z = 1.23e-4 + 5.6e+89j

z.real gets the real part, z.imag gets the imaginary part

There is a gradually "expanding" or "broadening" relationship among the three types:

< p> Integer→ Floating point number→ Complex number

Features:

There are 2 types of strings*** 4 representation methods:

Extension:

Use [] to get one or more characters in a string

Use [M:N:K] to slice the string according to the step size

{:}

> Right-aligned

^ Center-aligned | Output width of slot setting | Numeric Thousands separator | Floating point decimal precision or maximum string output length | Integer type

b , c , d , o , x , X

Floating point type

e , E , f , % |

A group of three padding, alignment and width, for example:

"{0:=^20}".format( "PYTHON")

→ '=======PYTHON======='

"{0:*>20}".format("BIT ")

→ '****************BIT'

"{:10}".format("BIT")

'BIT '

The remaining three are in groups, for example:

"{0:,.2f}".format(12345.6789)

p>

→ '12,345.68'

"{0:b},{0:c},{0:d},{0:o},{0:x},{0: X}x".format(425)

→ '110101001,Σ,425,651,1a9,1A9'

"{0:e},{0:E},{0 :f},{0:%}".format(3.14)

'3.140000e+00,3.140000E+00,3.140000,314.000000%'

↓CloseCode↓

Use the raise statement to throw a specified exception.

raise [Exception [, args [, traceback]]]

Compact form: two-branch structure suitable for simple expressions

if else

For example:

↓CloseCode↓

↓CloseCode↓

↓CloseCode↓ < /p>

↓CloseCode↓

For example:

↓CloseCode↓

Running result:

↓CloseCode↓

p>

↓CloseCode↓

For example:

↓CloseCode↓

Running result:

↓CloseCode↓

↓CloseCode↓

For example:

↓CloseCode↓

Running result:

↓CloseCode↓

↓CloseCode↓

For example:

↓CloseCode↓

Running result:

↓CloseCode↓

< p> ↓CloseCode↓

For example:

↓CloseCode↓

Running result:

↓CloseCode↓

Loop operation mode controlled by conditions

↓CloseCode↓

For example:

↓CloseCode↓

Running result:

↓CloseCode↓

↓CloseCode↓

↓CloseCode↓

For example:

↓CloseCode↓

Running results:

↓CloseCode↓

For example:

↓CloseCode↓

Running results:

< p> ↓CloseCode↓

↓CloseCode↓

Optional parameters such as:

↓CloseCode↓

Running result:

↓CloseCode↓

Variable parameters such as:

↓CloseCode↓

Running result:

↓CloseCode↓ < /p>

In function definitions, *args (arguments) and **kwargs (keyword arguments) are often encountered as parameters.

(In fact, in functions, and are necessary, args and kwargs can be replaced by other names)

*args refers to an indefinite number of non-key-value pair parameters.

**kwargs refers to an indefinite number of key-value pair parameters.

*args as a tuple matches arguments without a specified argument name. And **kwargs serves as a dictionary, matching parameters with specified parameter names.

*args must precede **kwargs.

args (usually followed by an identifier, you will see a or args are both identifiers) is Python's syntax for receiving or passing arbitrary position-based arguments. When you receive an argument described using this syntax (for example, if you use the asterisk syntax for a function signature in a function def statement), Python will bind the identifier to a tuple that contains all position-based The parameters received by Hermit. When you pass parameters using this syntax, the identifier can be bound to any iterable object (in fact, it can also be a person and an expression, and does not have to be an identifier), as long as the result of the expression is An iterable object will do.

**kwds (the identifier can be arbitrary, usually represented by k or kwds) is python's syntax for receiving or passing arbitrary position-based parameters.

(Python sometimes refers to named parameters as keyword parameters. They are not actually keywords - they are just used to name keywords, such as pass, for, or yield, and there are many more. Unfortunately, this is confusing. The confusing terminology is still an ingrained part of the language's culture) when you receive an argument described with this syntax (for example, if you use the double asterisk syntax for the function signature in the function's def statement). Python will bind the identifier to a dictionary containing the named arguments for all received hermits. When you pass arguments using this syntax, identifiers can only be bound to dictionaries (i.e. it can also be an expression, not necessarily an identifier, as long as the result of the expression is a dictionary) .

When you define or call a function, you must make sure that a and k come after all other parameters. If both appear at the same time, place k after a.

The lambda function returns the function name as the result

↓CloseCode↓

For example:

↓CloseCode↓

Running results:

↓CloseCode↓

Use lambda functions with caution