Python, as one of the most popular programming languages, has penetrated into every industry. It is easy to learn and easy to understand, has a rich library, complete functions. Life is short. Use Python.

The reading time is not long, we had better personally code again code, so that you can have more harvest.

The profile

1. Understand and install VsCode for writing and running Python code;

2. Master Python’s comments and learn to code from the start;

3. Master basic Python operators and have a preliminary feeling of programming language features.

Install and use VsCode

Compiling and running Python is a big job, and we need a great editor, preferably simple and stylish, useful and cool. As it happens, there is a Python editor here: Visual Studio Code, or VsCode for short, which was developed by Microsoft. Powerful yet simple, it can compile C++, C#, PHP and other languages besides Python.

 

Visual studio Code interface display

There are two ways to install VsCode:

  • Start by installing Anaconda. During the installation process, there is an option to install the Microsoft VsCode editor directly.
  • Go to the official website to download and install: code.visualstudio.com/.

After the installation, please install a artifact, can automatically complete the code, very convenient. Open VsCode and type “Ctrl + P “. In the dialog box that pops up, enter ext Install Python and download the first one.

 

 

So with everything in place, let’s try to do what every student program learns, print “Hello World” in two ways:

  • Create your own document, change the suffix to “.py “, let’s call it “hello.py”, then Open the File you created using the “Open File” menu of VsCode, and type the code: “print(‘ Hello World ‘)”.
  • To create a File directly with VSCode, you just need to open the menu “New File “, enter the code and run it before selecting the location you want to save it.

 

The next step is to run the program, in two ways:

  • Right click “Run Python file on terminal”.
  • Use the command “TERMINAL “at the bottom of the VSCode interface: We can either use the “CD” command to go to the hello. py file location, or use the “Open Folder “in the VSCode menu to put the terminal directly in the” hello. py” file location. Then type “python hello. py” to run the program.

 

 

annotation

Comments are an essential feature of every programming language, and their benefits are numerous:

  • Write your code ideas and plans in your program as notes.
  • Comment code feature, you can let others quickly read your code.
  • Temporarily disable a line of code so you can debug it.

In Python we use “#” for annotation. Here we give you a list of common annotations of several scenarios, you learn to do the exercise after the basic pass.

Code:

print("How old are you") # print a sentence
print(1+1) # print the result of one plus one
Copy the code

Explanation:

The green part of the text is the comment, and this example shows the ability to explain the line. For example, the code itself is very simple. If you encounter a very complex line of code, such as a complex calculation formula, then the importance of comments comes out.

Code:

Def add(a,b): # function of add operation return a+b print (add(2,3))Copy the code

Explanation:

We define a super simple function that implements a+b. A and B are the variables to be entered. We comment a paragraph at the end of the function to tell you that this is an addition function, so that we can call it later. We call the addition function directly in line 3 with add(2,3).

Code:

C = add(3,3) print (c)Copy the code

Explanation:

We wrote two lines of code to call add(3+ 3) and 3+4 (3+4), but we only want to assign 3+3 to c. We can temporarily mask c= add(3+4) with a comment to achieve this effect.

It is worth noting that the print function must be used with parentheses () in Python3.

Running results:

How old are you
2
5
6
Copy the code

Exercises:

Define a subtraction function and comment out its function.

2, Print How old # are you? And look at the results.

3. Write two lines of code that simultaneously call the subtraction function, comment out one line, and print the result.

The operator

Arithmetic operator

In the example noted above, we used addition + and subtraction -, both of which are actually operators, specifically called arithmetic operators. In addition, the arithmetic operators include: *, /, %, **, //, which are multiplication, division, mod, power (power), and divisor rounded downward.

Code:

print (8%3/2)
print (11//3**2)
Copy the code

Explanation:

The first line of code uses two arithmetic operators, % and /, to compute 8%3, mod the dipper to 2, and divide by 2 to 1

The second line uses two arithmetic operators, // and **, to calculate 3 ** 2, which equals 9, and then 11//9, which is rounded down to equal 1. The priority order of computation needs to be noted here.

Comparison operator

Is used to compare two numbers: ==,! =,>,<,>=,<=, respectively means equal to, not equal to, greater than, less than, greater than or equal to, less than or equal to. Notice that the output is True(True) or False (False).

Code:

print (9*8 ! = 8*9) print (9%2 == 4-3) print (3**2 >= 3*3)Copy the code

Explanation:

Line 1 is used! Lambda is equal to lambda to see if 9 times 8 is 8 times 9.

Line 2 uses == to see if 9% of 2 is equal to 4-3.

Line 3 uses >= to determine whether 3**2 is greater than or equal to 3*3.

Logical operator

Those of you who are familiar with digital circuits are probably familiar with the word and or not, and yes, we have three operators in Python, and, or, and not. And requires both sides of the logic to be correct, the result will be considered correct; Or requires that at least one of the two logics be true, then the result is true. Functionally, they are used in conditional statements to connect different conditions logically, returning the same result as the comparison operator, True(True) or False (False).

Code:

print (5 > 3 and 4 < 3)
print (5 > 3 or 4 < 3)
print (not 4 < 3)
Copy the code

Explanation:

The first line of code uses and, because 4<3 on both sides is wrong, the result is wrong.

Line 2 uses OR, and since 5>3 is correct on both sides, the result is correct.

Line 3 uses not, and since 4<3 on both sides is incorrect, the result is correct.

Running results:

1.0
1
False
True
True
False
True
True
Copy the code