List of articles
- perform python Two ways to code
- python Interpreter path
- Specify encoding method
- python notes
- python Interpreter and pyc file
- python Variable
- The assignment of a variable
- Writing format
- while loop
- Exercises
stay Windows Next , We can go through D:\python3\python 1.py To specify which version of python Interpreter . stay Linux Next, we can directly put one py Add executable permissions to the file , adopt ./1.py The way to carry out 1.py file ( It's like Linux Run any executable in the same way ), In this case, we did not specify which python Interpreter , So we need to 1.py The first line of is written
#!/usr/bin/env python
This sentence means to tell the operating system which to use python Interpreter (python Interpreter path ), The way is Linux Endemic . Other ways Windows and Linux You can do it all the time . Be careful , This sentence must be placed throughout python The first line of the script file .
coding=utf-8
stay python3 Next , Add this sentence or not . stay python2 Next , If you have Chinese, you need to add this sentence , Otherwise, there will be confusion . because python2 By default ASCII Encoding and decoding , stay python3 There is actually no string in , All contents are bytes ( Essentially, it's all Unicode). For example Windows Next ,cmd The terminal uses GBK code , If used python2 To compile a program with Chinese strings , There will be chaos , and python3 All of them Unicode, So it won't be garbled . There is such a conversion relationship between codes , What you use in the program is utf8, If the terminal uses GBK If the mode is displayed , The display is garbled , because utf8 Chinese accounts for 3 byte , and GBK Chinese takes up two bytes , The two of them cannot be directly converted . It can only be converted into Unicode, Then it is converted to another coding method .
stay C Used in language /**/ To comment , stay C++ Use in // Annotate , And ours python and shell Scripts use # Make a single line comment , stay python The way to use multi line comments in is three quotation marks
""" Annotated content """
We wrote python The code is created by python Explain to execute ,python The execution process of the code is as follows
perform Python Code , If you import other py file , In the process of execution, a with the same name will be automatically generated .pyc file , The document is python The bytecode generated after the interpreter compiles .
First of all, the meaning of variable existence is to record the change of a certain state , The essence of a variable is the name of a memory space , Variables can be understood as the number of a piece of memory , Access memory through this house number . We know , stay C/C++ When defining variables, you must specify the type of variables . stay python in , There is no need to specify the type when defining variables ,python It's a weak type programming language , Directly declare variables and assign values . But the definition of variable names should follow the following rules :
When we use input() When the function receives input , The received content is received as a string , Even if you enter numbers 1,var = input(), Variable var The final value of is ’1’ character string , Not Numbers 1.
First, let's look at two assignment methods
var1 = 'C/C++'
var1 = 'hello'
var2 = var1
How many memory blocks does the above occupy in memory in total ? No matter in python in , Or in other programming languages , When we modify a variable , In fact, it copies a memory space and reassigns it to the current variable . and var2=var1 The operation of , Not giving var2 Allocate memory , Rather let var2 and var1 Point to the same memory .
stay C/C++ in , Executable code must be placed in braces {} Inside , Braces are a scope , As long as {} Inside is a code block , There is no need to align between lines of code . however python The code structure is strictly controlled by indentation , Writing python When coding, you must strictly control indentation , The code must be aligned according to indentation .
if __name__ == __main__:
tag = input()
if tag == "hello":
print("hello")
elif tag == "world":
print("world")
while Conditions :
# The loop body
break # Exit all loops
continue # Exit this cycle
1、 Use while Loop input 1 2 3 4 5 6 8 9 10
1 #!/usr/bin/env python
2 # coding=utf-8
3
4 i = 0
5 while i < 10:
6 i += 1
7 if i == 7:
8 continue
9 print(i)
2、 seek 1-100 The sum of all the Numbers
1 #!/usr/bin/env python
2 # coding=utf-8
3
4 print(sum(range(1, 101))) # Print one line of code
5
6 i = 0
7 ret = 0
8 while i < 101:
9 ret = ret + i
10 i += 1
11 print(ret)
3、 Output 1-100 All odd numbers in
1 #!/usr/bin/env python
2 # coding=utf-8
3
4 i = 1
5 ret = 0
6 while i < 101:
7 ret = ret + i
8 i += 2
9 print(ret)
4、 Output 1-100 All the even numbers in
1 #!/usr/bin/env python
2 # coding=utf-8
3
4 i = 0
5 ret = 0
6 while i < 101:
7 ret = ret + i
8 i += 2
9 print(ret)
5、 seek 1-2+3-4+5 … 99 The sum of all the Numbers
1 #!/usr/bin/env python
2 # coding=utf-8
3
4 i = 1
5 ret = 0
6 while i < 100:
7 if i % 2 == 0:
8 ret = ret - i
9 else:
10 ret = ret + i
11 i += 1
12 print(ret)