Current location - Recipe Complete Network - Complete cookbook - Introduction to Xiaobai: What is used to write Python?
Introduction to Xiaobai: What is used to write Python?
How to learn python

As the saying goes, "skyscrapers start from the ground." Learning any programming language requires a solid language foundation, but how to lay a solid foundation? There is only one secret: knock more code, knock more code, knock more code. In the early stage of learning, it is recommended to find a book or blog about python basics and follow the examples in it. After laying a good foundation, you can go to the websites of some famous competitions, such as kaggle, to consolidate your knowledge by doing projects.

Recommended book: Python Basic Course by Magnus Lee Hetland (3rd Edition).

Recommended reason: Python's basic knowledge, basic concepts, advanced themes, Python program testing, packaging, publishing and other knowledge, as well as the practical development process of 10 Python project, covering a wide range, which can not only lay a solid foundation for beginners, but also help programmers improve their skills, and is suitable for Python developers at all levels to read and refer to.

basic knowledge

Code specification

1. Indent

Compared with other languages that use curly braces and end to identify code blocks, python is "unique". It identifies code blocks by indenting the code. Usually four spaces are an indentation, which can be realized by tab key. Indentation is an important part of python code. If your code indentation format is incorrect, such as code block statements with inconsistent indentation, and the first sentence is not capitalized, it will run an error.

# The first sentence of a complete sentence should be capitalized.

i=0

# Statements in the same code block should be indented consistently.

For I in the range (5):

Printing (1)

i+= 1

Step 2 take notes

Annotation of programming language, that is, explanation and explanation of code. Adding comments to code can improve the readability of code. When you read a piece of code written by others, you can quickly grasp the general meaning of the code through comments, which will make reading the code more convenient.

Python language comments are divided into single-line comments and multi-line comments, and the content after comments will be automatically skipped by the computer.

Single-line comment: add "#" before the statement to be commented, which can be used after the code or on a new line.

I= 1 # Use comments after the code.

# Use comments on new lines

Multi-line comments: add three single quotes or three double quotes at the beginning and end of the statement (consistent)

'''

Multi-line comments with single quotation marks

'''

"""

Multi-line comments with double quotes

"""

The use of comments not only plays a role in reading the meaning of the text and quickly understanding the meaning of the code, but also has a small beauty, which can hide a certain unfinished or modified code and temporarily prevent it from being executed by the computer.

3. Multi-line statement

Python marks the end of a line by default, but sometimes we need to express a complete code across lines for ease of reading. At this point, we can use the continuation character: backslash "\" to display one-line statement as multiple lines:

score = eng_score + \

Math score+\

His score

Note: If you use braces {}, brackets [] or parentheses () to enclose the data in the statement, you don't need to use a continuation character, as shown below:

name=['Ada ',' Ailsa ',' Amy ',

Barbara, Betty, Blanche

Karina, Carly, Kelly.

Daisy',' Darcy',' Diana']

What is an algorithm?

What is the purpose of using python before learning to use it? As data analysts, we don't need to use python to design programs, but only python to implement algorithms. So what is the algorithm?

Algorithm refers to the process or steps of solving problems. We can understand it with an example that is very close to life. If you need to make "scrambled eggs with tomatoes", according to the recipe, prepare the ingredients first, then put the oil in the hot pot, pour in the tomatoes and stir fry, and then add the egg liquid to stir fry until cooked.

This is the "algorithm" we use to complete the dish "scrambled eggs with tomatoes", and all we need to complete this algorithm is raw materials and operating instructions. Let's see what the raw materials and instructions are.

changeable

The "raw material" in python is an object. In Python, everything is an object, and objects exist only by referencing variables. Referring to the above example, we can understand "egg" as "thing" and name it "egg laid by little red chicken" as "variable".

The concept of variables must be familiar to everyone, which is basically consistent with the variables in the middle school equation. Variables can be understood as codes or labels of things, and they are reusable quantities. Different from variables in equations, variables in programming languages are not only numeric, but also character, logic and other data types.

Properties of the 1. variable

Before using variables in python, you need to define them first, otherwise you will get an error. However, unlike other programming languages such as C, python does not need to define the variable type in advance, and the variable type is determined by the type of variable assignment. In python, variables can be assigned and used repeatedly, variables can also be assigned to each other, and variable # Variable can be manipulated and assigned repeatedly.

i=2

i=3

# You can manipulate variables.

i+ 1

# View the data type of the variable

Type (a)

2. Naming rules of variables

It can only contain numbers, letters and underscores. Cannot contain python reserved words, keywords and function names. Use lowercase letters l and uppercase letters o carefully to avoid confusion with the numbers 1 and 0. It is suggested to use hump naming, that is, add ""before words, such as: studentname# to see what reserved words python has.

Import keywords

Print (keyword.kwlist)

3. Assignment of variables

Basic assignment in python, we use "=" to assign values to variables, such as "x=3". It should be noted here that data and variables are stored separately, that is, data "3" is established in memory first, then a mark "X" is established, and then X points to the value in memory. If a new value "2" is assigned to the variable X, it is essentially a modification of the data reference, and the variable X points to the data "2" in the memory again.

Multiple assignments can refer to multiple variables of the same object. For example, a person can have multiple identities, and Mr. Li is both a father and a teacher. Different variables actually point to the same thing. Father = Teacher =' Mr. Li'

Multivariate assignment assigns values to multiple variables at the same time. Parentheses can be placed around the equal sign, but A, B, c= 1, 2, 3 cannot be added.

(a,b,c)=( 1,2,3)

sentence

After understanding the "raw materials" in the algorithm, let's look at the "operating instructions" in the algorithm. The "operating instructions" in the algorithm can be said to be the "soul" of the algorithm, just like how a dish is cooked, which largely depends on the chef's cooking skills (that is, cooking skills). The "operation steps" of the algorithm are statements, including python's basic statements and control flow statements. The syntax of control flow statements is more complicated, which will be introduced in later articles. Let's take a look at the basic sentences of python:

In fact, we have been exposed to the basic statement of python: assignment statement, which is relatively simple, so I won't introduce it. This paper mainly introduces another basic statement of python which is particularly widely used: output and input statements.

1. output statement

There are two main ways to output values in python: expression and print () function. The difference between them is that the result of expression output is a python object, but in practice, in order to facilitate reading, it is usually necessary to output the result in a certain format, and the print () function solves this problem well.

Print () syntax:

print(*objects,sep= ' ',end='\n ',file=sys.stdout,flush=False)

Print () parameter:

Objects-Complex number, indicating that multiple objects can be output at one time. When outputting multiple objects, you need to use the. Sep-used to separate multiple objects. The default value is a space. End-Used to set the ending content. The default value is the newline character "\n", which can be changed to other strings. File-The file object to write. Flush-whether the output is cached is usually determined by file, but if the flush keyword parameter is True, the stream will be forced to refresh. # Print multiple values and set the middle separator to "-"

Print ("Guangzhou", "Shanghai", "Shenzhen", sep="- ")

# First output the default ending character "\n", and then set the ending character to "-". Pay attention to the difference between them.

Printed matter ("Guangzhou")

Printed matter ("Shanghai")

Print ("Shenzhen")

Print ("Guangzhou", end="- ")

Print ("Shanghai", end="- ")

Print ("Shenzhen", end="- ")

The above code implementation results are as follows:

2. Enter the statement

The function of getting keyboard input data in python is the input () function, which automatically converts the input data into a string type, automatically ignores line breaks and gives a prompt string. If you need to obtain other types of data, you can perform forced type conversion.

Input () syntax:

Input ([prompt])

Input () parameter:

Prompt: Enter the prompt information, optional parameter age=input ("Please enter your age:").