<<Python Foundation & gt; & gt;
pythonThe birth of
Founder: Guido van Rossum (Dutch)
System operation and maintenance
Network programming (search engine, crawler, server programming)
Artificial Intelligence, Robot
Big data and database programming
Game, Image Processing
Advantages and disadvantages:
Object Oriented (Java, C++, Python, C#, Swift)
Portability (Windows, Linux, Unix, IOS, Android)
Mixed Programming (C/C++/Java/net)
Easy to learn and easy to use
Wide range of applications
Compared with C/C++, the execution speed is not fast enough.
Can’t close source code
python V2.7(2020Year-end maintenance)
python V3.5(Current teaching)
pythonExplain the type of executor
CPython (CLanguage development)
JPython (JavaLanguage development)
PyPy (PythonLanguage development)
The first Python program
Writing: hello. PY
Execute Python program
$ python3 File path name.py
Commonly used file suffixes:
.c cLinguistic suffix
.cpp/.cc/.cxx C++Linguistic suffix
.java JavaLinguistic suffix
Write and execute code in Python interactive mode
Enter Interactive Mode
$ python3 <Carriage return >
Exit interactive mode:
>>> quit() <Carriage return >
>>> exit() <Carriage return >
Or enter: Ctrl + D (input file terminator)
Start with # until the end of the line
Leave the content of the comment out of execution
sublime Note shortcut key Ctrl +/
pythonComposition of the procedure:
The program consists of modules.
Modules are composed of data, functions, classes, etc.
Functions consist of statements
Statements consist of expressions
python Core data types
Numbers (integer int, float, complex, bool)
String str, list, etc.
Integers are numbers without decimal parts
The literal representation of integers
Floating point type float
Floating-point numbers are numbers with decimal parts (decimal parts can be 0)
The literal representation of floating-point numbers:
3.14 3.1 3.0 0.14 .14
Scientific counting method
Decimal e/E (Positive and Negative) Index
6.18E-1 (Equivalent to 0.618)
2.9979E8 (Equivalent to 299790000.0)
Complex type complex
It is divided into two parts:
Real part (real)
Imaginary part (image)
The imaginary part is the number ending with J or J.
Boolean type number bool
Numbers used to represent truth and falsehood
True Represent truth (condition satisfied or established)
False Express false (condition is not satisfied or not valid)
True The value is 1.
False The value is 0.
Null value None
None Is a special object that represents nonexistence
Used for occupying
Expressions and Operators
Consisting of a number or number, an operator, etc.
Usually let the computer do something and return the result.
1 + 2
1 +2 * 3 + 4 / 5
// Floordiv removal from floor
% Seek surplus
Dividing results in floating-point numbers that do not discard decimal parts.
1/3 # 0.33333333
8/2 # Get 4
// Floor removal
The result of division is to remove the decimal part and take it downward.
7/3 # 2.33333333
7//3 # Get 2
3.25//1.5 # Get 2
% Seek surplus
7%3 # Get 1
3.25%1.5 # Get 0.25
Y th power which means x
4**2 # Get 16
3**4.0 #Get 81
1. A shop sells watermelons for 7 yuan each. How many watermelons can you buy with 20 yuan? How much is left?
2.A student’s graduation salary is 10,000 yuan, which is increased by 20% every year. What’s the salary of this student ten years later?
1. 1) 20//7 # Buy two watermelons
2) 20%7 # 6 yuan left.
* / // %
Parentheses grouped subexpressions
()The internal expression is computed first
Hybrid type automatic upgrade
1+ 2.14 The return type is floating-point 3.14
3+4.0 Return to 7
Basic output function print
print(‘Things to be printed’)
Multiple data are separated by commas
What is a variable?
A variable is an identifier associated with an object
Naming rules for identifiers:
Must start with letters or underscores followed by letters or underscores or numbers
Unable to use Python keywords
python Case-sensitive variable names
Examples of legitimate identifiers (variable names)
abc a1b2 _abc _Abcd var
Illegal identifier (variable name):
1a2b $abc +a #sde @sdfh
True, False, None, is, del, if, elif…etc.
Variable name = expression
Variable name 1 = variable name 2 = expression
Variable name 1, variable name 2 = sequence
1.Create a variable that binds data objects
2.Modify the binding relationship of one variable to bind another data object
When a variable does not exist, create it and bind it to the object
When a variable exists, change the quota object bound by the variable
One variable can bind only one object
Two variables can bind the same object at the same time
pi2=pi #piVariables and Pi2 variables are bound to 3.1415936 at the same time
x=y=100 # xVariables and Y variables are bound to 100 at the same time
x,y=100,200 # Sequential assignment
python Automated memory management in
a=a+b # Release 10000 and rebind 30000
c=a # cAt the same time bound to 30000
1.Specify a garden radius r = 9cm
1) Calculate the circumference of a circle
２）Calculate the area of a circle
2.Variable Exchange Exercise
Two variables are known:
Question how to exchange bound objects between a and B without creating any new data objects
Used to delete variables and to de-associate objects if possible
del Variable name
View all variables in the current scope in interactive mode
Quit key: Q
a=10000 # Create a variable and bind 10000
print(a) # 10000
del a # Delete a variable and release 10,000 data objects
print(a) # error
Automated memory management and reference counting:
Each object records several variable bindings (references) itself. When the number of references is zero, the object is destroyed (releasing memory resources). This automatic memory management method is called reference counting.
b=a # Two variables bind 10,000 data objects at the same time
del b # At this point, 10,000 data objects will be released.
is/ is not operator
Determine whether two objects are the same object and return True when they are the same object.
Otherwise return to Flase
is not The return value is opposite to is
x is y
x is not y
Returns the address of an object in memory
Small integer object pool
CPython, Numbers of integers – 5 to 256 always exist in the pool of small integers and are not released and can be reused.
a is b # True
Compliance with assignment arithmetic operators
y+=x Equivalent to y = y + X
y-=x Equivalent to y = Y-X
y*=x Equivalent to y = y*x
y/=x Equivalent to y = Y / X
y//=x Equivalent to y = y//x
y%=x Equivalent to y = y%x
y**=x Equivalent to y = y** x
x=x*2 # x=200
1.In ancient China, it was called 162.1 kilograms. In ancient China, 216 kilograms were several kilograms and several kilograms, which were printed out by program.
2.It has passed 63320 seconds since the time of 0:00 in the morning. What time, minutes, seconds is it? Write a program and print it out.
Celsius = 5.0/9.0* (Fahrenheit – 32)
What’s the temperature of 100 degrees Fahrenheit? Write a program to calculate it.
If you know that the current temperature is Celsius, can you figure out what Fahrenheit temperature is?