On my blog, I write a lot about Python, and a lot of people think that eraser should be a Python developer or a technical developer, but I’m sorry to say that eraser is the opposite of a developer in many companies. But I’m a product manager who knows a lot about technology.

This series of articles will be completed before the Spring Festival of 2021, welcome to follow, like, comment -- dream eraser

First introduction to Python, Python variables and basic operations

1.1 Snowballing To Learn Python

Starting with this blog, I will write you a Python tutorial, literally from zero to one, showing you how to quickly learn Python skills.

By the way, although this set of courses is the text class column course, but its value is the training course of 2W yuan of training institutions, no more than the omission of the organization teacher to give you blow its past NB experience of those nonsense occupied time (estimated to offend many training institutions teacher).

The blog is called Snowball Learning Python, and the final goal is to write 100W words. The final goal is to form a basic script for a video course, and the “shameless” eraser will be the voice actor for the course.

The purpose of this course is to bring the cost of Learning Python from 2W-3W in training institutions and from 6K-8K in Internet videos to less than 300 yuan.

Snowball To Learn Python, and strive to complete the knowledge of the program, with a relaxed style of writing for everyone to present the feast of Python learning. Since this series of articles is not academic, please save your time for the theorist technologists who are interested in knowledge accuracy. You should not waste your time on our little white feast. Read the official manual instead.

1.2 Introduction to Python

Python is a programming language, not much different from JAVA, C#, C++, C, etc. Every programming language has its advantages and disadvantages. No language is superior to any other, and it is the best language in the right context.

Python is an interpreted, or literal, language, which is characterized by the execution of programs line by line through a translator, currently CPython, written in C, which is fairly efficient.

An extension of this knowledge is the existence of compiled languages, such as JAVA and C#, which convert the language into machine code and then execute it, but for you at this stage, this is not important, just get started.

Python is open source, and anyone in the world can contribute code to it and extend modules to it, all for free.

1.2.1 Python author

The author of Python, Guido van Rossum, devised the Language in 1989 to pass the time. Python was named after Monty Python’s Flying Circus, which Guido loved. So the name Python has nothing to do with the popular Python language.

Information about Python can be found at www.python.org.

1.2.2 Introduction to Python Language development

  • Python was invented by Guido van Rossum in 1989
  • It was released in 1991
  • Python2.0 was released in 2000
  • Python3.0 was released in 2008

It’s 2020 and you should learn python3.x.

1.2.3 Prospects for Python

It must be said that Python has been very popular in the last two years. The hype in the industry is more than the actual application. As a language, there is no field in which it cannot be used, but it is difficult to use. You don’t have any problem getting a job when you learn Python, and more often than not, you learn one programming language and all the other languages are pretty much the same. Languages are like a family, so you copy each other and everyone ends up using the same language.

You can’t just brag about Python’s future. Everything is good. In China, it is certainly not as big as the JAVA market, nor as big as the front-end market, and the PHP market in the last two years seems to be able to arm each other, after learning can find a job, the use of many scenarios, with the help of big data, artificial intelligence boast, get a high salary, after all, no one has said that JAVA can not do artificial intelligence.

Python is cross-platform and comfortable to develop on Linux and Mac, as well as other languages. There are no barriers to developing on Windows, which is where the rest of this series will be based, because Windows is where the most games can be played.

1.3 Installing Python

1.3.1 Python3.7. X installation

A language is the most difficult installation environment and development tools, the first step will block many people, in the beginning of learning simple and direct best. Also, when learning, do not choose the latest version of Python, you will have trouble finding the answer to the question, so we will choose 3.6 or 3.7 related version.

Open the official download address, www.python.org/downloads/, see English don’t panic, you can guess roughly.

Download don’t hurry, first find the right download link again, find the area shown below, see Windows is downloading, if you don’t pay attention, download the wrong will be very annoying ~

As a beginner who wants to learn Python, installing Python locally is a basic hurdle. Keep everything default during the installation process. After the installation is complete, go to the start menu and find the directory shown in the picture below.

If you see the above picture, it means that your Python environment has been installed and the basic operation of the computer is ok. There is no need to do the basic operation of creating folders and files. If you encounter difficulties in this area, it is recommended that you learn the basic operation of the computer.

In the above picture, clicking IDLE opens the terminal to start writing Python code. However, if you start writing Python code directly from the normal textbook, chances are that you will be stuck at the entrance of Python. Learning Python as they say is too boring. Who says you have to start with the Python Shell?

1.3.2 PyCharm installation

PyCharm is a fully functional code editor for Python development. PyCharm is available for free, but you can use the Community version.

Download address: www.jetbrains.com/pycharm/dow… , software less than 300M, very easy to download, the installation of the software is still given to you to complete, the reason is too simple. Please refer to the screenshot below for important steps.

Important points Screenshot

When you first activate PyCharm, choose a color scheme,Black editorIt doesn’t look easy to get tired

When started, PyCharm will ask you to create a new project, as shown in the following image. By default, PyCharm will match the Python environment you just installed, and it will automatically create a virtual environment.

LocationRepresents the directory, select a local arbitrary folder can be.

The picture below appears just wait.

After loading, the final result is shown in the following figure. If all directory structures are the same as the following figure, it is a great success

Generate another code for us on the right side, which is the file main.py at the bottom of the image. Yes, python files are usually named.py files.

Note that Hi PyCharm appears at the bottom. Congratulations, you have completed all installation tasks.

Installing any environment or development tool is simple but not common, and getting it done is a ticket. The rest is technical learning. To create a File, right-click on the directory on the left, select New and select Python File.

1.4 Language first learn notes

What programmers are most afraid of, is to write comments to the program, more afraid than comments is other people’s code does not write comments, how interesting logic. If you want to make the program more readable, it is inseparable from the program comments, in the company, a program, a project, a product contains thousands of code blocks, if you do not write comments, teammates will be sprayed.

1.4.1 Comment symbols

In Python, the use of a # sign to indicate a single line comment, that is, the content after the # sign, is ignored by the Python interpreter and does not run. For example, the code generated automatically after creating a Python project above contains many single-line comments.

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name) :
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
Copy the code

Comment symbols can be placed above or to the right of the code, for example:

# I'm a line comment
print("hello world")  # I'm also a line comment
Copy the code

1.4.2 False multiple lines comment three quotation marks

If you want to comment multiple lines, you can either use a # on each line or use three quotation marks. For example:

""" I'm a big comment and I can have multiple lines of comments and notice the quotes in English
print("Dream Eraser")
Copy the code

Because of the Python language, you can use three single quotes to get the same effect:

"" I'm a big comment and I can have multiple lines of comments and notice I'm single quotes" "
print("Dream Eraser")
Copy the code

The use of triple quotation marks will come up later when we talk about strings, but you can interpret this as a multi-line comment.

1.5 Understanding variables

Any language needs to learn from variables. Variables were first exposed to algebra in junior and middle school. They are simply understood as a thing to store data in programs. Create a new file with an arbitrary name (start up, cognitive, get the code up and running, see the results is a big win).

For example, I’m 18 years old. In Python, you can declare a variable age equal to 18.

age = 18
Copy the code

If xiaohong is 20 years old, add a new variable xiaohong_age in the program and make it equal to 20.

age = 18
xiaohong_age = 20
Copy the code

You can use comments to make your code clearer.

# My age
age = 18
# Xiao Hong's age
xiaohong_age = 20
Copy the code

This way, when the code is long written, we can quickly know what each variable meant when it was written by commenting it, saving the following paragraphs.

When I wrote this code, only God and I knew what it meant. Now, only God knows.

Python variables are a little different from other languages, which require the variable’s data type to be declared in advance. Python does not need to specify the variable’s data type, and it automatically sets the variable’s type based on the value to the right of the equal sign. This can greatly improve coding speed, as you will see later.

1.5.1 Variables Naming Precautions

Variable naming is important in any language, and Python is no exception. The naming principle is as follows:

  1. Must be written in English letters,_(underscore) or Chinese (usually not), try to use English letters
  2. Variable names can contain only letters, numbers,_(underline) or Chinese characters
  3. English letters are case-sensitive, and Age and Age are different variable names
  4. Python’s built-in reserved words and built-in functions cannot be used as variable names

It is important to note that although there is nothing wrong with using Chinese characters as variables, try not to use them.

Name =123
print(name)Copy the code

For system reserved words and Python built-in functions, as long as they are easily found through a search engine, you can copy the code below into any Python file and run it.

import keyword
print(keyword.kwlist)
Copy the code

The running results are as follows:

['False'.'None'.'True'.'and'.'as'.'assert'.'async'.'await'.'break'.'class'.'continue'.'def'.'del'.'elif'.'else'.'except'.'finally'.'for'.'from'.'global'.'if'.'import'.'in'.'is'.'lambda'.'nonlocal'.'not'.'or'.'pass'.'raise'.'return'.'try'.'while'.'with'.'yield']
Copy the code

Python built-in functions are not listed here. Using built-in functions as variable names does not generate errors, but will cause the function of the original built-in functions to be lost. Therefore, it is not recommended to use them.

1.5.2 Examples of names of non-compliant variables

Non-compliant variable names:

  • A,1 contains special symbols that are not allowed
  • Start with a number of 1A
  • False reserved words
  • Hex Specifies the name of the built-in function
  • I am a variable and try not to use Chinese

After naming variables for a long time, there is no need to memorize too much, just follow a rule, the name should try to have a real meaning, such as age, by age, rather than directly declare an X to represent the age, so that you can see the name.

1.6 Print function print()

The first function you’ll encounter in Python is print(), which prints content on the console, such as the following:

print("hello world") Print a string
print(1+1)  # output 1+1

name = eraser
print(name) # output variable values
Copy the code

We will learn more about print() in the future, so we can output the content in this chapter.

1.7 Mathematical Operations

Python supports four basic operations, remainder and power operations. The Python symbols for the four operations are +, -, *, and /. Mod, also known as mod, corresponds to the symbol %, and the symbol for the power operation is ** (two asterisks). The specific code and comments are as follows:

# addition
a = 1+1
# subtraction
b = 1-1
# multiplication
c = 2*5
# division
d = 4/2

# modulo
e = 5/2

# power operation
f = 2**3

# print() can print multiple variables separated by commas
print(a,b,c,d,e,f)

Copy the code

The final result is as follows, we get the correct result of each operation, for division and mod in the subsequent coding process is a very important part, you can try several groups of numbers, first find a feeling.

2 0 10 2.0 2.5 8

1.7.1 The priority of mathematical symbols

When it comes to mathematics, you have to consider the precedence of symbols, but all precedence can be determined by parentheses (). If there are no parentheses, the order of powers, multiplication/division/remainder/exact division, and addition/subtraction is the same as in algebra.

1.8 Assignment operators

By now, you should have noticed that one symbol that shows up a lot is =, and notice that it’s called the assignment operator in Python (and in other languages, too), and it’s not an equal sign. In coding languages, two == equals, and a single = means assignment, For example, x=1 assigns the number 1 to the variable x.

Assignment operators can work with the arithmetic operators learned above to create a variety of assignment operators, such as +=, -=, *=. Examples are as follows. You can try a few on your own.

a = 1
a += 2

print(a)
Copy the code

The result is 3, a+=2 is equivalent to a=a+2, if the code is a/=2 then it is equivalent to a=a/2, and so on, and other assignment operators you can learn by analogy.

1.8.1 Assigning multiple variables at once

In Python code, it is possible to assign values to more than one variable at a time, for example:

x = y = z = 10086
Copy the code

X,y,z: 10086; x,y,z: 10086;

x,y,z = 1.2.3
Copy the code

Note that the number of variables on the left is the same as the number of values on the right.

1.9 Summary of this blog post

This blog is the first chapter of snowballing Python. The eraser will draw you into the Python loop using a simple line of knowledge. Hopefully you can read this easily. This is the correct way to start learning Python.

The most dazzling light in the world, one is the sun, and another is to become a worker that you work hard