1. Chain assignment: used to assign the same object to multiple variables.
a=b=123 print(a) #123 print(b) #123
2. Series unpacking assignment: the series data is assigned to variables corresponding to the same number, and the number must be consistent.
a,b,c=1,2,3 print(a) #1 print(b) #2 print(c) #3
a,b,c=1,2,3 a,b=b,a print(a) #2 print(b) #1
3. Constant: Python does not support constant (meaning that it can be changed in fact, not logically.)
4. Introduction to the most basic built-in data types
(1) Integer
(2) Floating point: decimal
(3) Boolean: True, False
(4) String type: "abc", "sxt"
(5) Operators with special points:
/: Floating point division
//: Integer division
%: Surplus
**: power
Use the divmod() function to get the quotient and remainder at the same time
5. Integer: 0b binary, 0o octal, 0x hexadecimal
print(int(3.99)) #3 print(int(True)) #1 print(int(False)) #0
Type conversion with int():
a=3+3.5 print(a) #6.5
6. Floating point number: decimal float
Type conversion and rounding: float()
round(value) can return a rounded value: the original value will not be changed, but a new value will be generated.
Enhanced assignment operator:
+=: a+=2 is equivalent to a=a+2
Similar are: - =, * =/=
7. Representation of time
import time a=int(time.time()) print(a) totleminite=a//60 print(totleminite) totlehour=tl=totleminite//60 print(totlehour) totleday=totlehour//24 print(totleday) totleyear=totleday//365 print(totleyear) #51
8. Example: draw multi segment discount, connect the known points, and calculate the distance between the first and last points.
import turtle import math x1,y1=100,100 x2,y2=100,-100 x3,y3=-100,-100 x4,y4=-100,100 turtle.penup() turtle.goto(x1,y1) turtle.pendown() turtle.goto(x2,y2) turtle.goto(x3,y3) turtle.goto(x4,y4) distance=math.sqrt((x1-x4)**2+(y1-y4)**2) turtle.write(distance)
9. Boolean values: True(1),False(0)
Comparison operator:
== be equal to != Not equal to
Logical operators:
or, and, not
10. Same operator: used to compare the storage units of two objects. The actual comparison is the address of the object.
Is: judge whether the two variable reference objects are the same, that is, the address of the comparison object.
==: Used to judge whether the values of reference variables and reference objects are equal.
11. Escape character: use "\ + special character" to achieve some effects that are difficult to be represented by characters. Such as line feed.
Continuation:\
a='aaa\ bbb' print(a) #aaabbb
String splicing: + strings on both sides Or directly put together to achieve splicing
a='aaa'+'bbb' print(a) #aaabbb b='aaa''bbb''ccc' print(b) #aaabbbccc
String copy
a='aaa'*3 print(a) #aaaaaaaaa
Non wrapping printing
print("aaa") print("bbb") print("ccc") ''' aaa bbb ccc ''' print("aaa",end="*") #Ends with an asterisk and does not wrap print("bbb",end="\t") print("ccc") #aaa*bbb cccc
Read string from console: use input() to read keyboard input from console.
myname=input("Please enter your first name:")
12. (1) str() implements digital transformation string
print(str(1)) #1 print(str(34.5)) #34.5
(2) Extract characters using []
The essence of a string is a character sequence, which can be extracted by adding an offset specified in [] after the string Single character of the position. The positive order is numbered from 0 and 1, and the negative order is numbered from - 1 and - 2.
a="abcdefdhigklmnopqrstuvwxyz" print(a[1]) #b print(a[-1]) #z
(3) replace() implements string "change"
The string cannot be changed. Call replace() to generate a new string. The original string has not changed.
a="abcdefdhigklmnopqrstuvwxyz" print(a.replace('c','pay')) #ab pay defdhigklmnopqrstuvwxyz print(a) #abcdefdhigklmnopqrstuvwxyz the value of the original string does not change a=a.replace('c','pay') #a points to the new string print(a) #The value of ab defdhigklmnopqrstuvwxyz A is changed
13. (1) slicing string: intercepting string
The standard format is: [start offset: end offset: step size] header without footer
a='abcdefghijklmnopqrstuvwxyz' print(a[:]) #Entire output print(a[1:5]) #bcde print(a[1:5:2]) #bd print(a[-2::]) #yz step size is positive order 1 by default print(a[-10:-1:]) #qrstuvwxy uses negative numbers to slice, and arranges the order of starting coordinates according to the positive and negative of the step size print(a[-1:-10:-1]) #zyxwvutsr
Example 1 statement flashback output
a='to be or not to be' print(a[::-1])
Example 2 output all x's in the string
a='xstxstxstxstxstxst' print(a[::3])
14.split() split and join() merge
split() can separate the string into multiple substrings based on the specified separator. If the separator is not specified, the blank character is used by default.
a="to be or not to be" print(a.split()) #['to', 'be', 'or', 'not', 'to', 'be'] print(a.split("be")) #['to ', ' or not to ', ''] b="I\nlove\nu" print(b.split("\n")) #['I', 'love', 'u']
join() is used to splice a series of substrings. Better than using the form of string addition, string addition requires a newly generated variable. (note the format)
a=["this","is","not","love"] print("*".join(a)) #this*is*not*love
15. String resident mechanism and string comparison
String resident: a method to save only one copy of the same and immutable string, and different values are stored in the string resident pool.
string comparison
a="abc_d11" b="abc_d11" print(a is b) # True is compares objects (memory) print(a==b) #Is the content the same print("a" in a) #Does it contain True
16. Summary of common string methods
(1) Common search methods
a='''The birds are in the strong wind Turn quickly The boy went to pick it up A penny Grapevine fantasy And extended tentacles The waves recoiled And a towering back''' print(len(a)) #77 print(a.startswith('bird')) #True print(a.endswith('Back')) #True print(a.find('and')) #50 location of first appearance print(a.rfind('and')) #71 location of last appearance print(a.count('and')) #2 and how many times print("sacd12324".isalnum()) #Is True a string of numbers and letters print(a[78::])
17. String formatting
format(): format the string by directly mapping the parameter value through {index} / {parameter name}.
a="The name is:{0},Age is:{1}" print(a.format('Dream',20)) #The name is Dream and the age is 20 b='The name is{name},Age is{age}' print(b.format(name='April',age=18)) #His name is April and his age is 18
Use {0} in order. Use names without paying attention to the order.
Number formatting
a="The name is:{0},Revenue is:{1:.2f}" print(a.format('datou',15000.324325))