程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python Programming] II. Basic syntax

編輯:Python


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


perform python Two ways to code
  • python filename: hold filename Documents to python Interpreter , It is equivalent to giving the file address to python Interpreter ,python The interpreter will find filename file , Read the file into memory and execute . stay Windows Next , No .py Suffixed files can also be python function , By python The file that the interpreter runs can be any suffix , But when we program formally , Should take py For the suffix .
  • cmd Mode input python: Type... Directly on the command line python, Will enter python The interactive interface of the interpreter , It can interpret the instructions we input in real time and output the execution results .
python Interpreter path

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 .

Specify encoding method
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 .

python notes

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 """
python Interpreter and pyc file

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 .

python Variable

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 :

  • Variable names can only be Letter 、 Any combination of Numbers or underscores
  • The first character of a variable name cannot be a number
  • The following keywords cannot be declared as variable names
    [‘and’, ‘as’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘exec’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘not’, ‘or’, ‘pass’, ‘print’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]

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.

The assignment of a variable

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 .

Writing format

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 loop
while Conditions :
# The loop body 
break # Exit all loops 
continue # Exit this cycle 
Exercises

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)



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved