Start your python data journey

Keywords: Python

print function

Print (variable name)
Print (variable)

print("hello world")    # hello world

myString = "hi";
print(myString);    # hi

Single-Line Comments

Single line notes begin with #

# Here are the notes
print("hi");    # Here are the notes

Continuation character

A continuation character is required because a line is too long

If you can judge the logical relationship, you do not need a newline character (two cases)

  1. The inside of parentheses, brackets and curly braces can be written more
  2. String under three quotation marks
color1 = "red";
color2 = "blue";
if color1=="blue" and\
     color2 == "red":
     print("yes");
else:
    print("no");

One line multiple statements

Write multiple statements in one line

x = 1; y = 2; print(x+y);

It's not usually written like this
Not easy to read

block

The same indentation is a block, no {} block

identifier

  1. Initials are underlined or letters
  2. The rest can be letters, underscores, numbers
  3. Case sensitive, i.e. PI and PI are different identifiers

established by the people through long social practice:

  1. All uppercase is considered a constant
  2. Try not to start with an underscore. It is considered a private variable
  3. Hump nomenclature
  4. See the name and know the meaning

keyword

Like if, or, else, etc., cannot be used as identifiers
Avoid conflicts with the language itself

expression

Operator to connect variables and constants
Attention priority

  1. Arithmetic operators, such as +, -, *, etc
  2. Bitwise operators, such as negation ~, and &, etc
  3. Comparison operators, such as >, <, = = and so on
  4. Logical operators, such as not, and, or

Note that the = sign is an assignment symbol

Assignment statement

Assignment by reference
Equivalent to a variable
No declaration type is required

Each variable has an id function that returns the unique identifier of the object

>>> a = 500
>>> b = a
>>> c = 500
>>> id(a)
2804352670064
>>> id(b)
2804352670064
>>> id(c)
2804352669904

// Here b and c refer to the same 500
// c refers to another 500
>>> a = 1000
>>> b = 1000
>>> id(a)
2804352670224
>>> id(b)
2804352670256

// It can be seen that b in example 1 is actually a reference to a 
>>> a = 10
>>> b = 10
>>> id(a)
2804351631888
>>> id(b)
2804351631888

>>> a  = 256
>>> b = 256
>>> id(a)
2804351639760
>>> id(b)
2804351639760

>>> a = 257
>>> b = 257
>>> id(a)
2804352670224
>>> id(b)
2804352670320

// In principle, the IDs of a and b should be different in the above three times
// But the first two are different, and the third is the same 
// Why is the same?

// Because some numbers are commonly used
// python is saved
// In the future, these numbers are directly referenced and saved, 
// This increases speed

// Therefore, at 10 and 256, the IDs of a and b are the same, 
// But it is different at 257, because python only saves - 5 ~ 256

assignment

Similar to c

Tuple assignment

x = 1
y = 2
x,y = y,x
print((x,y))    // (2,1)

Here (x,y) is a tuple
The assignment process here is similar to the deconstruction assignment of js
x,y = y,x
It is equivalent to packing y and X into a tuple (2,1) and assigning it to a temporary variable temp,
Then, after the structure is 2 and 1, it is assigned to X and Y respectively

sentence

A line of logic code that completely performs a task

For example, an assignment statement

integer

Similar to C
However, long integers and integers are combined
The specific digits are related to the operating system
One digit represents positive and negative, and the remaining digits represent size
=/-(2^(n-1)), n is the number of bits of the operating system, such as 64 bit operating system

Boolean

There are only two values: True, False
The essence is that True is stored with 1 and False with 0, so it is a subclass of integer

float

It's called float in python
It can be expressed by similar scientific counting method

Plural form

i^2 = -1
Then i is an imaginary number
A real number plus an imaginary number is a complex number

For example, a + bi, where a and B are real numbers, and a and B can be 9
In python, real is used to obtain a, and imag returns b
Use combine() to obtain its conjugate complex number

From the above three sentences, it can be concluded that real and imag should be attributes
And combine is the function name

// Note that in python, j is used to represent the imaginary number unit, and the above i only explains what is an imaginary number
>>> a = 1+10j
>>> a.imag
10.0
>>> a.real
1.0
>>> a.conjugate()
(1-10j)
>>>

complex in python

String representation

Single quote ''
Double quotation mark ""
Three quotation marks (three single quotation marks) ''
Similar to js ` ` ` ` ` ` ` (three backquotes)

Mapping type (Dictionary)

The only mapping type in python is dictionary
Similar to JSON (key value pair)
d = {key_string : value_any,...}
d[key_string];

Note the key above_ String must be a string
value_any is of any type

arithmetic operation

special
Power:**
Division://

Multiple divisions
The remainder is the remainder after division
The result of division is the result of / / operation

109 divided by 10 equals 10 remainder 9
That is 109 / / 10 = 10
109%10 = 9

Note - 32 = - (32) = - 9
(-3)**2=0

Pay attention to the operation priority

Comparison operation

Values are compared by value size
Strings are compared by ASCII value size

be careful
3<4<5
This form
Equivalent to: 3 < 4 and 4 < 5

Character operator

Raw string operator (r/R)
Can be used where you don't want escape characters to work
r'something'
Because there may be such as \ n in the string
And \ n is understood as line feed. If the path "\ note\math_note" exists,
It may not be resolved correctly,
In this case, you can use r"\note\math_note", so that the escape character will not work
Or use two \ (two \ will be escaped as \)

function

Built in function

Can be used directly

Standard library functions

The python standard specifies the functions to be supported
Import the module to use

Third party Library

Install a third-party library and then import it for use

User defined function

User defined functions

//Unfinished to be changed

Posted by coder4Ever on Fri, 19 Nov 2021 08:31:45 -0800