Python entry basics
02.Getting started with python
Chain assignment and series unpacking assignment
- Chain assignment is used to assign the same object to multiple variables x=y=123
- The series unpacking assignment is assigned to the variables corresponding to the same number (the number must be the same) a,b,c=4,5,6
[Operation] The series unpacking assignment realizes variable exchange a,b=1,2 a,b= b,a - Constants, python does not support constants
Built-in data types
Every object has a type. Python’s most basic built-in data types:
1. Integer: integer
2. Floating point: decimal
3. Boolean: true or false, only containing true/false
4. String type: composed of characters Sequence "abc", "little star"
Numerical operators: ±*/(floating point division 8/2=4.0) //(integer division 7///2=3)% (take the remainder 7% 4=3) ** power divmod obtains the quotient and remainder at the same time, 0 cannot be a divisor!
Integer
Integer, 0b secondary system, 0o octal, 0x hexadecimal
- Int realizes type conversion and directly discards the decimal part
- Automatic conversion, integer and floating point operations, automatic conversion to floating point numbers
- Python3 integers have no boundaries and will not overflow, especially suitable for scientific operations
Floating point
Floating point, float, 3.14=314E-2
- float can convert others into floating point numbers
- Mixed operations are automatically converted to floating point numbers
- round(value) can return the rounded value
Enhanced assignment operator
- a=a+1,a+=1
Representation of time
- Base time point: 00:00 on January 1, 1970, calculated in milliseconds (1/1000 second)
- python time.time() gets the current time, first import import time time.time()
Exercise: Define multi-point coordinates _ draw a polyline _ and calculate the distance between the starting point and the ending point.
Answer: day_2:test_1
Error summary
1. Turtle is a module. Turtle is initialized before it is used. It is referenced by import.
2. A=3. A is a variable
. Variables and modules must be initialized before use. 3. Variables and modules are all identifiers. Identifiers: variables, functions, Classes, modules, etc., pay attention to the identifier naming rule
4. Turtle.done() can always retain the running results
Boolean value
Comparison operator
- == is equal to, used for comparison; = used for assignment
- ! = Not equal to
Logical Operators
- And and, x and y, if x is true, y is returned; if x is false, then returns false, a false is false
- Or or, x or y, if x is true, then return true; if x is false, then return y -if true is true
- Not not
Same operator
- Is compares whether the object referenced by the variable is the same address id, compare id
- == Whether the value of the comparison object is equal, compare value
- 例子:a=1000,b=1000,a==b true;a is b false,id(a)id(b)
- Note: integer cache problem (-5, 256)
- is more efficient than ==, it is recommended to use is as much as possible
String
Basic characteristics of strings
- Writing programs pay more attention to logical thinking rather than mathematical ability
- The essence of string: character sequence, string is immutable in python
- String function: Corresponding characters to numbers
String encoding
- Python3 directly supports unicode and can represent characters in any written language in the world
- ASCII code is a subset of Unicode and only supports letters and numbers
- ord() built-in function, which can convert characters into Unicode codes
- chr() can convert decimal to corresponding characters
Quotation marks create string
- You can use single or double quotes to create strings
- Note : pay attention to the difference between inner and outer single and double quotes
- Three consecutive single or double quotes can create a multi-line string
Empty string
- c=“”
- len (c) 0
Escape character
String splicing
- If both sides of + are strings, concatenate
- If there are numbers on both sides of +, add
- Multiple "aa" "bb" == ""aabb"
- “Sct” * 3 “sctsctsct”
- Print without line break end="any string"
》》》print("aa", end="#")
》》》print("bb", end="#")
aabb - Read the string from the console
"" "myname = input (" please enter the name ")
Please enter the name: green onion
" "" myname
"green onion"
str() extract characters _replace() to replace
- str() converts other characters into strings
- [] Extract characters
- replace()
String slice
- Slice slice[], the standard format is: [start offset start: end offset end: step size]
- Note: Baotou does not end
- step: how long to take
- Common operations
- Other operations
- The start and end offsets are not in the string range, no error is reported, and the start or end is taken to the end
Assignment:
1. Output the string "to be or not to be" in reverse order
a="to be or not to be"
Error summary
- Only need to adjust the step, no need to think about complexity
- The output in pycharm needs to be called print()