Python hard to understand? Once you buy a watermelon, you'll understand! __python

Source: Internet
Author: User

Source: Code Bay


What is code?


Code is a language, a language that computers can read. The computer is a fool, and he cannot understand anything that is ambiguous by default. For example, you let your husband to buy a watermelon, your old guild decided where to buy, buy a few, find a discount to do activities to buy, in short, you give him money, he will buy you back to let you eat cool watermelon. But you want the computer to buy a watermelon. You have to tell him: go to the door of the fresh fruit shop, buy Sha watermelon, if the next store discount, go to the store next to buy. In short, you can't let the computer make any decisions, you have to clearly tell him all the circumstances of all the behavior. and code, is the language you communicate with the computer, or the command of the computer.


We will understand the computer as a person, this person is very rigid, can only follow the detailed orders you give him to work. This person works particularly fast and ensures that the results of the work are correct.


Programming language is not important, it is important to understand how to communicate with the computer, understand this will be able to read the code.



programming = algorithm + data structure


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


1, Judge


When you order the computer to do one thing, it may be handled in some way.

The key words usually involved are: if, else, ElseIf, and, Or,<, >,=,<=,>=,==


For example, you let the computer buy watermelon, hope to buy a discount watermelon, do not discount if you do not eat.


If Fruit shop discounts:

Buy Watermelon

Else

I'm not buying it.


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


You are not happy if you can't eat watermelon without a discount at the fruit store. So let's make the computer smarter. If watermelon discounts, buy watermelon, not discounted words to let the computer and the boss bargain, the boss agreed to discount, buy watermelon, do not agree, do not eat;


If Fruit shop discounts:

Buy Watermelon

Else

Bargain with the Boss

If bargaining success:

Buy Watermelon

Else

Don't eat.


Although with the boss bargain generally can speak success, but some boss give force to hit 50 percent, some bosses only to hit 90 percent. You think, if the boss dozen 50 percent, I will buy two, today eat one, tomorrow eat one. If the boss does not give force, discount less than 50 percent, buy one, eat today, tomorrow.


If Fruit shop discounts:

Buy Watermelon

Else

Bargain with the Boss

If discount success and discount strength <=5:

Buy two watermelons

ElseIf discounts and Discounts strength > 5:

Buy a watermelon

Else

Don't eat.


In addition, there is no discovery, the success of this code can actually be written:


If discounts are successful:

If discount strength <=5:

Buy two watermelons

Else

Buy a watermelon

Else

Don't 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 ambiguous, he can complete the work.


The meaning of and, or the meaning of OR. The order of and and or is different, and the results are different, and you can specify the precedence of the criteria by parentheses.


A==b is the meaning of comparing A and b equality, while = represents assignment. The meaning of the assignment, which I introduced in the following variable chapters.


2. Cycle


The keywords in python that involve loops include: for...in ..., while,break,continue


The above example we have let the computer successfully can according to different conditions to buy watermelon, but if the boss does not discount, you can not eat watermelon. At this time, you think, we have three fruit shop at the door, are fresh fruits, Four seasons fruit and roadside stalls, a discount does not represent three do not discount ah, so you hope that the computer can go to three to see, pick a discount shop to buy watermelon.


For Fruit shop in (fresh fruits, Four seasons fruits and roadside stalls):

If Fruit shop discounts:

Buy Watermelon

Break (Loop End)

Else

Don't do anything.

Take the Watermelon Home


The meaning of loops is that you can use the computer to execute the same commands on different objects.


The introduction of the break,break means to end the loop. For example, the break in this code means that once we buy a watermelon, we take the watermelon home.


3, variable


Now is the time to introduce the ' variable ' this important thing.


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


Variable can be understood as a computer memory unit, the computer has a very good memory, so, can be arbitrary use, used up to throw, need to. (Of course, it's not rigorous to say what you want to do, but you can understand it.)


Why is it called a variable because it can be changed. Why to change it. For example, we all have our own age, I am 18 this year, next year will be 19. So, ' age ' is changed every year in my memory.


Variable corresponding to a thing called constant, in my memory, my wife is Jiangjiang classmate. And this ' wife ' this memory unit in my mind is not change, she is always jiangjiang classmate. Constants you can not understand now, I write mainly to show love.


Now, the computer is going to buy watermelons again. But this time, you want the computer to find a fruit store with the biggest discounts in three fruit stores.


Maximum discount for all previous fruit stores = None

The fruit store that gave the maximum discount before = None

For Fruit shop in (fresh fruits, Four seasons fruits and roadside stalls):

Current fruit Shop Discount = Boss's Discount

If current fruit shop discount < maximum discount for all fruit stores before: #4折比5折力度大

Top discount for all fruit stores before = current Fruit store Discounts

Before giving the biggest discount to the fruit store = Fruit Store


Buy a watermelon for the biggest discount fruit store before you go.


There are several memory units above:


Maximum discount for all previous fruit stores

Before giving the biggest discount to the fruit store

Current Fruit Shop Discounts

Fruit Store note that for a in B,a is also a variable, which is an element of the current execution in B at every execution.


Here are some ideas to introduce:


None represents null in Python, which means that the memory unit is now nothing. For example, in my first love, my mind has already had a lover this memory unit, I have been looking for, but encountered you have not been found, so that period of time this memory unit has been empty.

Assignment symbol: =. Note that in the computer language, a ' = ' number denotes an assignment, with two equal sign ' = = ' to compare elements before and after. The meaning of the assignment is to put the following elements in the previous variable.


Scope of variables


Scope this thing is very important, but really do not want to say, because there is no way to combine the actual examples, but still generally explained.


Or just the example, I added the line number, and I use-represents a space, representing the indentation of the code.


Maximum discount for all fruit stores before 1 = None

Fruit Shop with maximum discounts prior to 2 = None

3 for Fruit store in (fresh fruit, Four seasons fruit and roadside stand):

4---current fruit shop discount = Boss Discount

5---If current fruit shop discount < maximum discount for all fruit shops before: #4折比5折力度大

6------Before the maximum discount for all fruit stores = current Fruit store Discounts

7---Before giving the maximum discount to the fruit store = Fruit Store

8 to buy a watermelon for the biggest discount fruit store before you go.


You see this example may wonder why we should first give ' the maximum discount for all previous fruit stores ', ' before the maximum discount of the fruit store ' These two variables are assigned to none. is because of the scope problem.


You see this program, each line of code indentation is not the same. The scope of a variable can be understood to be: A variable that works only if it is indented at the first occurrence of the place and indented more code. Our 1th, 2-line variables can be used for 3-8 lines, because the indentation of the code is at the back of the 1, 2 lines. The four-line variable ' current Fruit store discount ' is valid only in lines 5, 6, and 7


1 A = None

2 ———— B = None

3 ————..

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

5 ————————..

6 ———— D = None

7 ————..

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.