Want to understand Python? Buy watermelon once

What is code?

Code is a language that a computer can read. The computer is a fool, he can not understand the default two things. For example, if you let your husband buy a watermelon, your husband will decide where to buy, buy a few, and find a discounted purchase. In short, if you give him money, he will buy it for you to eat. Watermelon. But do you want the computer to buy a watermelon? You want to tell him: go to the front of the fresh fruit store to buy, buy sand meal without child watermelon, if the next shop discount, go to the next store to buy. In short, you can't make the computer make any decisions. You must clearly tell him all the behavior in all situations. The code is the language you communicate with the computer, or the command to the computer.

We understand the computer as a person. This person is very rigid and can only work in accordance with the detailed orders you gave him. This person works particularly fast and guarantees that the work results are correct.

The programming language is not really important. It is important to understand how to communicate with the computer and understand this code to understand the code.

Programming = Algorithm + Data Structure

Python is a language that communicates with computers. There are few words (keywords) in this language, and there are few rules. All of the following is based on python.

1, judge

When you order the computer to do one thing, this matter may be handled in some cases.

The keywords usually involved in the judgment are: if, else, elseif, and, or,<, >,=,<=,>=,==

For example, you let the computer buy watermelons, hoping to buy discounted watermelons. If you don't discount them, you will not eat them.

If fruit shop discount:

Buy watermelon

Else:

Don't buy it

With this command, the computer becomes more intelligent, and he can execute your order according to the situation.

You can't eat watermelon without a discount at the fruit shop. You are not happy. Well, let's make computers more intelligent. If watermelon is discounted, buy watermelon; if not, let the computer bargain with the boss, the owner agrees to discount, and buy the watermelon; disagree, do not eat;

If fruit shop discount:

Buy watermelon

Else:

Bargain with the boss

If bargaining success:

Buy watermelon

Else:

Do not eat.

Although the bargaining price with the boss can generally be successful, some employers give 50% discount, and some bosses only give 10% discount. You think, if the boss hits 20 percent, I'll buy two. Eat one today and one tomorrow. If the boss doesn't give his power, if the discount is less than 50%, buy one, eat it today, and say it tomorrow.

If fruit shop discount:

Buy watermelon

Else:

Bargain with the boss

If discounted successfully and discounted <=5:

Buy two watermelons

Elseif discount success and discount strength> 5:

Buy a watermelon

Else:

Do not eat.

In addition, has it been found that this code of success can also be written as:

If the discount is successful:

If discount strength <=5:

Buy two watermelons

Else:

Buy a watermelon

Else:

Do not eat.

This shows that the computer is not completely static, as long as you give him the command he can understand and will not default to both, he can complete the work.

The meaning of and stands for and, or the meaning of or stands for or. The order of and and or is different, and the result is also different. You can use parentheses to indicate the priority of the judgment condition.

a==b is a comparison of whether a and b are equal, and = represents an assignment. The meaning of assignments is described in the variable section below.

2, the cycle

Keywords related to loops in python include: for...in... , while, break, continue

The above example we have made the computer successful can go to the fruit shop to buy watermelon according to different situations, but if the boss does not discount, you can not eat watermelon. At this time, you think, we have three fruit stores at the entrance of the community, namely fresh fruit, fruits and roadside stalls, a discount does not mean that the three are not discounted, so you hope that the computer can go to all three to see Look, pick a discount store to buy watermelon.

For fruit shop in (fresh fruits, seasonal fruits and street stalls):

If fruit shop discount:

Buy watermelon

Break (end of loop)

Else:

Do not do anything

Bring watermelon home

The significance of looping is that you can use the computer to execute the same command on different objects.

Introduce break, break means to end the loop. For example, the break in this code indicates that once we have bought a watermelon, we take the watermelon home.

3, variable

It is time to introduce the important thing of 'variables'.

We still understand the computer as a person who helps us to work. Then as a person, he needs to have memory, and we can control and use the memory of the computer through variables.

Variables can be understood as a memory unit of a computer. The computer has excellent memory. Therefore, it can be used as you wish. If you run out, throw it away, you need it. (Of course, it is not rigorous to say what you like, but you can understand it as you like)

That's why it's called a variable because it can be changed. Why change? For example, we all have our own age. I am 18 this year and 19 next year. Therefore, 'age' is changed every year in my memory.

One of the variables corresponds to a constant, and in my memory, my wife is Jiang Jiang. And this 'wife' memory unit will not change in my mind. She will always be Jiang Jiang. Constant you can not understand now, I write mainly to show love.

Now, the computer is going to buy watermelon again. But this time, you hope that the computer can find a fruit shop with the greatest discount in three fruit stores to buy fruit.

Previous discounts for all fruit shops = None

Fruit Shop previously given the biggest discount = None

For fruit shop in (fresh fruits, seasonal fruits and street stalls):

Current fruit store discount = discount given by the owner

If Discount at current fruit store < previous discount at all fruit stores: #4 fold off 50% off

The biggest discount for all fruit stores before = current fruit store discount

The fruit store that gave the biggest discount before = Fruit Shop

Go to a fruit store that gave the biggest discount before buying a watermelon

Above are the following memory units:

The biggest discount for all previous fruit shops

Fruit shop before giving the biggest discount

Current fruit shop discount

The fruit shop notes that for a in b, a is also a variable that is an element of the current execution in b at each execution.

Here are some concepts to introduce:

None is empty in python, indicating that this memory unit is not present at all. For example, when my love sinus was opened, I already had a memory unit in my mind. I was looking for it, but I never found it before I met you, so the memory unit was empty all the time.

Assignment symbol: =. Note that in the computer language, a '=' sign indicates an assignment, and two equal signs '==' indicate the comparison of the preceding and following elements. The meaning of assignment is to put the following elements into the previous variables.

The scope of the variable

The scope of this thing is very important, but sincerely do not want to talk, because there is no way to combine real-life examples, but it is still a general description.

Still just an example, I added a line number, and I use - to represent the space, which represents the indentation of the code.

1 maximum discount for all fruit stores before 1 = None

2 Fruit Shops Before Maximum Discounts = None

3 for Fruit Shop in (fresh fruits, seasonal fruits and street stalls):

4 --- Current fruit store discount = discount given by the owner

5 ---if Discount at current fruit store< Previous discount at all fruit stores: #4 fold over 50%

6 ------ largest discount for all fruit stores before = current fruit store discount

7 ---Fruits previously given the biggest discount = Fruit Shop

8 Buy a watermelon before going to the biggest discounted fruit store

You see this example may be wondering why you should assign None to the two variables, which are the 'big discounts for all previous fruit stores' and the 'fruit shop' that previously gave the biggest discount. It is because of the scope problem.

You see this program, the indentation of each line of code is not the same. Variable scope can be understood as: a variable, only in the indentation where it first appeared, and the indentation of more code is only valid. Our first and second line of variables can be used for lines 3-8, because the indentation of these codes is behind lines 1 and 2. The fourth line variable 'Current Fruit Shop Discount' is only valid on lines 5, 6, and 7.

1 A = None

2 ————B = None

3 ---..

4 ————————C = None

5 ————————..

6 ————D = None

7 ————..

8 --------..

9 --------..

10 ..

11 ..

As another example, ABCD is a variable and .. represents some code. The scope of A is 1-11, which means that A can be used on lines 1-11. B's scope is 2-9 lines, C's scope is only 4-5 lines, note that 8-9 lines can no longer use C, although his indentation and 4 and 5 lines, scope encountered One cannot end with code. The scope of D is 6-9 lines.

4, function

To understand the above sections, you should probably understand how the computer executes code.

Functions and modules are not the same as the previous ones. Before it was how to get the computer working, it was computer-oriented. This section is about people. How do we make the code we write to the computer look more concise and clear. Functions and modules are the optimization of the code structure, otherwise a slightly larger project, a large amount of code, the computer can not understand human understanding, is not conducive to management and communication.

Say the function first. A function is a collection of code. There are several reasons to use a function:

1. Give a piece of code a name.

2. This code has little to do with the code that uses it, so it can be extracted and the code is more concise. The meaning of not much is: the function code can be executed by passing a few arguments to the function.

3. The code can be assembled and used in different places of the code.

Still we go to buy watermelon, but we hope that the computer can buy watermelon tomorrow and buy one again tomorrow. As before, this logic should be written like this:

For fruit shop in (fresh fruits, seasonal fruits and street stalls):

If fruit shop discount:

Buy watermelon

Else:

Bargain with the boss

If discounted successfully and discounted <=5:

Buy two watermelons

Elseif discount success and discount strength> 5:

Buy a watermelon

Else:

Do not eat.

rest for a day.

For fruit shop in (fresh fruits, seasonal fruits and street stalls):

If fruit shop discount:

Buy watermelon

Else:

Bargain with the boss

If discounted successfully and discounted <=5:

Buy two watermelons

Elseif discount success and discount strength> 5:

Buy a watermelon

Else:

Do not eat.

The above code can meet our requirements, but the code to buy watermelon is actually a duplicate. It is more difficult to manage the duplicated code. For example, if you want to change it, you have to change it everywhere.

So, let's make the code to buy watermelon into a function, and the code can be organized into:

Def buy watermelon_function():

For fruit shop in (fresh fruits, seasonal fruits and street stalls):

If fruit shop discount:

Buy watermelon

Else:

Bargain with the boss

If discounted successfully and discounted <=5:

Buy two watermelons

Elseif discount success and discount strength> 5:

Buy a watermelon

Else:

Do not eat.

Buy watermelon_function()

rest for a day.

Buy watermelon_function()

This time, the code is more tidy, and, every time we want to modify the code to buy watermelon, we only need to change this function to a code.

In Python, use def function_name(): to define a function. All the indentations below the function belong to this function, called the function body.

Use function_name() to call the function.

4.1 function parameters

Now that you are not satisfied with the computer, you will only buy watermelon, and you want to eat grapefruit again. How to do it, then is it because we have to write a function to buy grapefruit?

Of course, you can write another buy grapefruit function, but the logic to buy grapefruit function and buy watermelon function logic are the same, the order is the same, the only difference is that before selling watermelon, it is to buy grapefruit. If you write a similar purchase of grapefruit function, then there is a problem just mentioned, each time you modify the logic to buy fruit, you need to modify the buy watermelon, buy grapefruit function, if grapefruit tired, but also buy bananas, apples, Pear, isn't it going to write a lot of functions?

To solve this problem, there is actually a better way to use function parameters.

Since we can buy fruits (whether it's watermelon or grapefruit) the function is the same, so we can write a buy fruit function and buy a fruit function to define the process of buying a fruit. When we use this function, we only need to tell The function we want to buy fruit, the function can be based on the fruit name you tell him to buy the corresponding fruit.

The code to buy the fruit function is as follows:

Def buy fruit function (fruit name):

For fruit shop in (fresh fruits, seasonal fruits and street stalls):

If fruit shop discount:

Buy [fruit name]

Else:

Bargain with the boss

If discounted successfully and discounted <=5:

Buy two [fruit names]

Elseif discount success and discount strength> 5:

Buy a [fruit name]

Else:

Do not eat.

Buy fruit function (watermelon) # Let the computer to buy watermelon

Buy fruit function (grapefruit) # Let the computer buy grapefruit

To use function parameters in python, you only need to add parameters inside the parentheses when the function is defined. The parameter here is 'fruit name'. Code can use function parameters in the function body.

When calling a function, a variable or constant is written in parentheses after the function name and passed to the function.

Note that if there are multiple parameters, the parameter order of function definition and calling function must be the same.

In Python, there are still a little more advanced ways to use default parameters, keyword parameters, etc. For example, don't explain in detail.

Def sum(a, b = 0,c = 1): #b/c is the default parameter

Return a+b+c

Func(1,0,1)

Func(1)

Func(1,c=1)

The result of these three func calls is the same.

5, module

Just now we have already talked about functions. Functions are the integration of code. The module is to integrate more code.

The introduction of modules and functions is to solve similar problems: manage code segments, and give them a name.

In general, a module is a py file, which may include a lot of functions, but also can include some of the class definition (we will talk about later).

The introduction of modules is to better manage the code.

We put the code in a py file, this py file is a module, in other py files, we can use the import keyword to introduce the module.

For example, we now have an a.py file with some functions, such as buy_xigua, buy_youzi, etc. If we want to use these functions in the b file, we must import the a module before use.

There are many ways to import a module and use the functions inside the module. I list the common methods below.

method one:

Import a

A.buy_xigua()

A.buy_youzi()

This method imports module a and then calls the function via the module.function.

Method Two:

From a import *

Buy_xigua()

Buy_youzi()

This method imports all the contents of module a and can be used directly.

Method 3:

From a import buy_xigua,buy_youzi

Buy_xigua()

Buy_youzi()

This method imports the specified content in module a and can be used directly. Generally, this method is used more often in the software development process. The reason is that we only import the necessary content and use it, but we do not need to import it. (Don’t waste it if you need it, it's a good habit to use it, and the deep reason is beyond the scope of the tutorial)

6, classes and objects

The concept of classes and objects may be slightly unintelligible to students who have not been exposed to programming. I remember being exposed to this concept for the first time in the year and I was confused for a while.

Previously, we introduced 'variables'. The introduction of variables is to describe the memory unit within the program. A variable can be either a number or a string (eg setting a variable name = 'water style').

However, some things need to be described together using many variables. For example, someone who buys watermelons. People who buy watermelon have many characteristics, such as name, deposit, and the number of watermelons. In addition, people who buy watermelon also have some behaviors, such as buying watermelon. In order to realize the function of people who buy watermelon, we introduce the concept of classes and objects. Note that the problem that the class/object introduces to solve is not the same as the problem that the function or module solves. The introduction of functions and modules is more about managing code, and the introduction of classes/objects is to encapsulate state and behavior together.

What does the class and object mean? Classes are the states and behaviors of what we define. For example, people who buy watermelons have state and behavior. We define them in advance and the concept of class is a kind of virtual. The object is an entity that is dynamically created and generated during the execution of the code.

For example, for people who bought watermelons, we wrote a process to buy watermelons. Now we want to create two people who buy watermelons, each responsible for one day, so that those who buy watermelons can also rest.

Self Test GFCI With TR Outlet

Self Test Gfci With Tr Outlet,20 Amp Gfci Plug,20 Amp Gfci Receptacle,Double Gfci Outlet

Lishui Trimone Electrical Technology Co., Ltd , https://www.3gracegfci.com

Posted on