0 Basic do n’t be afraid, from 0 to 1 easily teach you how to get started with Python

Produced by CDA data analyst

Hello everyone, I am writing a BUG line. You can call me a line. Today I will lead you through the learning journey from entry to improvement of Python. First of all, we have a basic understanding of Python and a clear concept, and then step by step from the basics to improve, and then fully open your way to Python training.

The content of this article is divided into four parts:

  • Background knowledge of Python language
  • How to install Python packages
  • The first Python program written
  • Good programming rules

Background knowledge of Python language

As we all know, in the 21st century, computers have become very popular. People can use it to shop, chat, video call, play games and even in professional fields. People can use it to do professional work, such as image recognition and sound signal judgment. , Voice assistant, simulated human playing chess. The reason why the computer can do this is mainly because of various software on the computer, such as WeChat, QQ that can chat, Weibo to share things around, knowing about sharing various knowledge, and recommending products Taobao and Jingdong. These are artificial intelligence software with unique thinking ability and learning ability.

These softwares are completed through programming predictions. People use programming languages ​​to write diamantes that can be recognized by computers, and then realize various functions of the software, which generates great value, and we call these programming people programmers.

Next, I will popularize some definitions in the computer field.

Computer Software : Software for short, is a collection of computer data and instructions organized in a specific order. Such as office software, Windows operating system, WeChat, QQ, these are software.

Programming Language (Programming Language) : is a form of language that specifies a set of instructions that can be used to produce various outputs. Programming languages ​​are usually composed of computer instructions and can be used to create programs that implement specific algorithms.

Although you may not have studied a programming language before, you must have heard the names of the following programming languages: C, Java, C ++, C #, R, JavaScript, PHP, and the focus of this article on Python.

** Computer program: ** Computer program is abbreviated as program, which is a collection of instructions executed by a computer to perform specific tasks

The relationship between the above definitions is: programmers write programs through programming languages, and compile and publish the software used by users.

What is the Python language

The origin of the Python language is because Python author Guido Van Rossum is a big fan of the British BBC program title "Monty Python's Flying Vircus", so the word Python is used as the official name of the Python language. In addition, the meaning of Python in English also includes the meaning of python and python.

Python is a very general-purpose high-level programming language. A script interpreter developed by Gudio during Christmas time in 1989 and released publicly in 1991, originally as an inheritance of the ABC language. Python is an interpreted language, with a strong emphasis on the design concept of code readability, allowing programmers to use more concise codes than C ++ and Java to implement language-related functions. In addition, the underlying Python is written in C language. , Running fast.

As of February 2020, Python ranked first in the PYPL programming language

Ranked third in TIOBE, while the first and second names are Java and C

Advantages of Python

The advantages of Python can be divided into five points:

1. Simple and easy to use. Compared with other languages, it is simpler and easier to use than C and C ++. For beginners, Python is easy to use.

2. Python provides a large number of function libraries. In addition to the standard library that comes with Python, it also has a third-party library support, especially in scientific computing, data analysis, human language processing, visual processing, machine learning, and medical image processing libraries. Python programming is easier to learn and powerful.

3. Python has language compatibility and is called glue language. It can combine various modules of other languages, especially (C and C ++).

4. With the ability to transplant across systems. The operating systems supported by the Python system include: Windows, Unix, MacOS X, OS \ 2, Android, etc.

5. The code is free and open source. Follow the GPL usage agreement. Users can freely publish a backup of Python, read its source code, and make changes to it.

It is precisely because of these advantages that Python is used as a programming course by the top universities in the world of computer science, such as Carnegie Mellon University and MIT. These advantages are the reasons for beginners to learn Python. Of course, the popularity of Python is also related to the recent development of big data and artificial intelligence worldwide.

Install Anaconda

No matter if your computer is Windows, Linux or MAC system, Anaconda has corresponding installation software. If your computer is a 64-bit system, it is recommended to download the 64-bit version. (PS: Right-click on My Computer in Windows system and click Properties to view the number of bits in your computer. For MAC systems, click the Apple logo in the upper left corner of the toolbar, and then click About This Machine> More Information> System Report> (Left) Software to view the right The 64-bit kernel and function extensions in the window, if it shows yes, it is 64-bit, otherwise it is 32-bit. If you do n’t know the Linux system, you can use Baidu.)

The Python version is recommended to choose the Python 3 version. Although Python 2 is currently used more, the future mainstream is definitely Python 3. After all, the official maintenance of Python 2 has been stopped since January 1, 20.

After confirming the software version to be downloaded, we will proceed with the installation of Anaconda.

1. Select the corresponding version to download and close the anti-virus software on the computer ! !

2. Open the installation software system prompting security warning and click Allow (Ignore this step if there is no pop-up)

3. The interface of the installation program pops up, click Next

4. Some user notices pop up. If you do n’t, please click I agree. (If you do n’t agree, you can use it)

5. At this time, pay attention! ! ! Don't choose the default Just Me, change to All Users or some problems may occur

6. At this time, you must choose the installation path. You need to have 3 G of memory space. It is recommended to install it on the C drive. If you want to install it on another drive, it will be more troublesome to deal with when there is a problem. After selecting the installation path, click Next to enter an important step.

7. After selecting the installation path, you need to tick both of the pictures. The function above is to add Anaconda to the system path environment variable. If you do not configure it, you need to manually add the environment variable. The function below is to register Anaconda as the system Python 3.7 . Here you must remember to tick it. After ticking, click install to install it.

8. Wait for Anaconda's automatic installation to complete. After that, click next and finish in the pop-up window. If the anti-virus software pops up during the installation process, remember to click Allow.

Write the first Python code program

After completing Anaconda, we can enter Jupyter Notebook.

Press the Windows key + R key on the keyboard, enter CMD in the pop-up running window and click OK

Then a CMD command window will pop up, enter Jupyter NoteBook in the window, and then hit enter to enter the browser window, remember that this CMD window will enter the local server mode of Jupyter NoteBook after entering, remember not to close This window, otherwise there is no way to run Python.

In the pop-up browser window, create a new Python3 file

Common shortcut keys in Jupyter Notebook

When using Jupyter Notebook and Python, if you know that some shortcut key functions are very helpful for us to speed up writing code, the following line lists some commonly used shortcut keys to help us save some time when writing code. Commonly used shortcut keys are mainly divided into command mode and edit mode, we introduce one by one

Shortcut key in command mode (press ESC to enter)

· A- Insert new unit above

· B- Insert the unit below

· C-Copy selected unit

· DD-Delete selected unit

Shortcut key in edit mode (press Enter to enter)

· Tab- code completion or code indentation

· Shift + Enter-Run this unit and jump to the next unit

· Ctrl + enter- run this unit only

· Alt + Enter-Run this unit and insert jump to a new unit

There are many shortcut keys in Jupyter notebook, we don't have to understand all of them, just remember a few commonly used ones.

Our first program on Jupyter Notebook

Next is the first program we entered in Jupyter Notebook

# Our first program print ( 'hello the best world' ) print ( ' You okay, the best in the world') print ( 'It's nice to meet you') print ( ' This is great - ')

The first line of code is #our first program, press Ctrl + enter after the input is completed, you can see that there is no reaction after running, because the symbol of the #line comment in Python, the code with # is not Will be executed to explain the relevant information of the code, such as the function of the program, the time of writing the code, and the author of the code, which is used to facilitate the reader to understand the code.

The code on the second line is print ('hello the best world'), enter and press Ctrl + Enter, it will display hello the best world.

The third line of code is hello, the best world, run after the input is completed, the following will display: hello, the best world

After the fourth and fifth lines of code are entered, execution will return a sentence in the print function.

We can put these codes in the same code block, and then run after the input is completed, we will get the following results:

After entering the code above, we successfully contacted the Python language to generate the words we want to output on the computer. This way we have generated our first program, after which we can continue to call it.

It should be noted that the above code cannot be entered at will. You need to pay attention to capitalization issues, full-width and half-width issues, and brackets. Otherwise, errors may be reported in the Jupyter notebook. Here we introduce how to solve the code error.

Code error debugging

In order to experience the problem of code errors, we modify the code and then run it.

When there is an error in the code, a prompt appears at the bottom: SyntaxError: invalid syntax, meaning: syntax error: invalid syntax. Looking closely at the code, you can see that the third line of code is missing a left parenthesis after the print, so here is a syntax error.

Looking closely at the code, you can actually see that the P of the last line of code is uppercase rather than lowercase, but the Python running code is executed line by line. If there is an error above, then the code below will not be prompted, then Now let's fix the first error and then run the code to see what will be displayed.

Now after running the code, a second error pops up NameError: name 'Print' is not defined meaning: name error: the name "Print" is not defined, which means that Print cannot be recognized in Python and needs to be named correctly . What can be understood here is that the error prompt below indicates that there is an error in the fifth line of code. We can use this to quickly find the wrong code, especially in the case of more codes. If we cannot directly see multiple errors, we can only modify the code based on the error message after modifying the code.

For English prompts that are wrong, if you have a good English level, you can quickly get familiar with and understand the meaning of the error. If the level is not good, you may find the error more slowly

Good coding rules

In the coding process of Python, we must be very careful. We must program in accordance with the provisions of the Python language, otherwise we will make mistakes in the programming process. Below we understand some Python-related regulations.

Identifier

Identifiers are used in the Python language to standardize the names of executable code objects that a naming interpreter can recognize. The print function is the function name that the interpreter can recognize. In Python, variables, keywords, functions, operators, and class names are all identifiers. We will explain after these.

1. Composition of variables, keywords, functions, classes

In Python, these names can only consist of letters, numbers, and underscores. That is, lowercase letters az, uppercase letters AZ, underscore _ and numbers 0-9 can be used, it should be noted that the number can not be placed in the first character of the name.

Here we introduce the correct naming and wrong naming

1. Correct naming:

Variables: i = 0, price = 10.0

Functions: print (), sum (), my_definition (). It should be noted that the parentheses themselves are not function names

Keywords: if, break

2. Incorrect naming

Variables: 9i = 0, h = 11, Note: 9 cannot be used to name the first character, and cannot be used to make up variable names

Function: Print (), my.definition () Description: The built-in function composition letters cannot be capitalized, and cannot be used as the composition of the function name

Keywords: if = 1, IF. Note: The if keyword cannot be used as a variable name, and the keyword cannot be expressed in uppercase

2. Case sensitivity in Python

Case is a very sensitive language in Python. For example, a = 0 and A = 0 are the two variables defined. We can run the following code.

a = 0 #define a variable print (a) #output variable a # get output 0print (A) #output variable A # report error

It can be seen from the error message here that A is an undefined variable. So we can tell that Python is case sensitive.

3. The naming must be concise and easy to read

In Python, there is no requirement for the length of the naming. As long as the computer allows, any length can be used, but too long naming will affect reading, and it will also occupy memory and hard disk space resources.

So we need to set an invisible rule that our naming must be concise and easy to read. For example, we want to set a variable iamagoodboyforchina = 'Zhang Yida', so obviously the variable name is too long. In addition, the naming can not be too simple, for example, i = Zhang Cannon, here we can not see what the meaning of "i" represents. A reasonable name is like this my_name = 'snake', so that we can understand that this is a variable name of "my name"

4. Naming at the beginning of the underscore

Identifiers that begin with an underscore have special meaning in Python. A single underscore (such as food) represents a class attribute that cannot be directly accessed, and needs to be accessed through the interface provided by the class; a double underscore (such as: food) represents a private member of the class; a double underscore begins and ends (Such as _food ) represents a special logo for special methods in Python. We will introduce it in detail later, as long as you understand it here.

Data

In Python, in addition to the identifier, the remaining objects can be called data, such as variable values, the contents in quotation marks, the records stored in the database, the called files, pictures, audio, and video. The information inside the quotes of the print function is a type of data.

Program code is basically composed of identifier (executable command) and data

Basic code format

1. Multi-line statement

Python generally writes one statement per line, but when the code is too long, we can use a slash (\) to divide a line of statements into multiple lines, such as the following code:

one = 9two = 11three = 13sumall = one+\two+\three

Check the operation and the output of the four variables

In addition, if the statement contains [], {}, (), there is no need to use multi-line connectors

Multiline indent format

When writing multiple lines, Python uses a strict indentation system for better code execution and reading. As shown below

Requirements for indent format:

1. Jupyter notebook has an automatic indentation format positioning function. When you press enter after completing a line of code, the next line will be automatically indented to the specified position, then continue to enter the code. When there is indentation of four bytes, multi-line programming needs to be performed in strict accordance with the format, otherwise logic errors may occur, making the code less readable.

2.Python distinguishes different subcode blocks in an indented format. As shown in the figure above, the content enclosed with] belongs to the same subcode block, and they will be executed together, while the two lines of code below else belong to another The code block will not be executed here.

Python reserved keywords (Keywords)

We can view the current reserved keywords in Python through the code below

import keywordkeyword.kwlist

The keywords in Python are listed in the following table

These reserved words cannot use variable names, function names, and class names. They are all Python-specific identifiers, so they are called reserved keywords.

Comment

When programming in Python, we need to add comments to the code in a reasonable place, which can play a role in explaining the code, which is helpful for us to watch for a long time or others. The comment statement starts with a pound sign #, and the content after the pound sign will not be executed.

There are two ways to comment

#This is a single line comment print ('hello word')

#This is the end of line comment

In the current epidemic situation, the hurried pace of the past has finally slowed down, and it is also time to think about your career plan and life plan. Prepare in advance, plan ahead, and store energy for the future-ready to go!

Published 2739 original articles · Like 276 · Visits 560,000+

Guess you like

Origin blog.csdn.net/yoggieCDA/article/details/105635167