Easy Python Tutorial

Original link: https://hsiaofeng.com/archives/190.html

This is a tutorial for coping with the middle school exam. It aims to quickly learn some basic Python operations and cope with the exam.

Take a simple math problem as an example:

A bottle of Coke costs 3 yuan, and Xiao Ming bought 2 bottles. How much does he have to pay?

Introduce a concept, variable . A variable is a name for data or something more complex. In this problem, we can get two variables – the unit price of Coke and the quantity of Coke. We name it price and number . Of course, you can also use Chinese单价and数量if you like.

In Python, use the equals sign for assignment operations. Assignment means assigning what is on the right of the equal sign to the left.

 price = 3 number = 2

We can easily know that the price payable is price multiplied by number .

In Python, there are six commonly used operations.

  • Add, represented by the symbol + ;
  • Subtract, represented by the symbol - ;
  • Multiplication, represented by the symbol * ;
  • Division, represented by the symbol / ;
  • Divide, represented by two division symbols // ;
  • The remainder is represented by the symbol % .

Below are some examples. >>> represents user input, and lines without >>> represent feedback from the system.

 >>> 1+1 2 >>> 1-1 0 >>> 2*1 2 >>> 15/4 2.5 >>> 10//4 2 >>> 21%4 1

For the last two inputs, the quotient of 10 divided by 4 is 2, and the remainder of 21 divided by 4 is 1:

$$
10 ÷ 4 = 2 \cdot\cdot\cdot 2
$$

$$
21 ÷ 4 = 5 \cdot\cdot\cdot 1
$$

So, to calculate the price to pay, we can set one more variable, with:

 money = price * number

Finally, we can output the result to the screen:

 print(money)

print() is used to output text. Fill in the parentheses with what you want to output, and it will be displayed on the screen.

Finally, the program runs and outputs a line:

 6

We can make it look more human, like:

 print("应付" + money + "元。")

In Python, the plus operator can be used to add strings . The part enclosed in English quotation marks " , we call it a string. If it is not enclosed in quotation marks, it will be regarded as a variable by Python.

In this way, the screen will output

应付6元。

We can prompt the user to enter some content, using the input() function. For example, if we want to know how much the user paid, we can write:

 pay = input("请输入收取金额:")

The screen will show:

请输入收取金额:

At this point the keyboard can enter, press the Enter key to complete the input.

Suppose we entered 10 , then the current pay is equal to "10" . The content entered through the input() function is of string type by default.

We can turn it into a number, otherwise Python has no way to subtract it.

 pay = float(pay)

float() is used to store variables as decimals.

At this point, we can calculate how much money should be paid to Xiao Ming.

 print(pay - money)

But there is a problem at this time. If Xiao Ming doesn’t pay enough, the print will become a negative number, which is wrong. We should be prompted not to pay enough.

Here, a grand introduction – if . As its name suggests, it is used for “if” judgments.

 if 判断条件:如果为真,执行这里else:如果为假,执行这里如果为假,执行这里其他语句...

In Python, the sizes of numbers have these relationships:

  • greater than > , for example a>b
  • less than < , e.g. a<b
  • Equal to == , for example a==b
  • Not equal to != , e.g. a!=b

Why is equal not = but == ? Because it is = , it is confused with the previous assignment operation.

if can be used here like this:

 if pay-money<0: print("付款不足。") else: print("找零" + pay - money + "元")

In this way, a simple change calculation system is constructed.

This article is reproduced from: https://hsiaofeng.com/archives/190.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment