day02: Python variables and comments

Use Python to calculate

Assuming that the hourly wage of going out to work is 50 yuan, 8 hours a day, how much is the wage? (calculated using idle python shell)

50*8
400

Assuming we work 22 days a month, how much can we earn in a month?

50*8*22
8800

Assuming 11 months of classes a year, how much can you earn in a year?

50*8*22*12
105600

If you spend 2,000 yuan per month, how much money can you have left in a year?

50*8*22*12-2000*12
81600

First variable

A variable is a place to store data. The calculation method of Python is mentioned above. Then the hourly wage is 50 yuan when you go out to work, and the hourly wage is adjusted to 60 yuan. To calculate how much money you can leave in a year, you need to start a new calculation. , in order to avoid recalculation, we set the hourly salary as a variable, if the hourly salary changes in the future, directly adjust the variable content

In Python, you can use "=" to set the content of the variable, and create a variable x to define the hourly salary

x = 50
print(x)
50

Now that the salary is adjusted to 60, then we can directly modify the variable

x = 60
print(x)
60

A program can use multiple variables. Assuming an hourly salary of 60, working 8 hours a day, and working 22 days a month, how much can you earn in a year?

x = 60                       #时薪
y = x * 8 * 22 * 12          #年薪计算(时薪x小时x每月工作天数x12个月)
print(y)
126720

Assuming that we spend 2500 per month, how much do we spend in a year

z = 2500         #定义每月花费z
o = z * 12       #定义一年花费o
print(o)
30000

Then continue to calculate how much can be left each year. Above we have defined the annual salary with y and the annual cost with o

t = y - o        #定义一年剩多少钱(一年工资-一年花费)
print(t)
96720

Then reset the salary variable

Hourly salary: hour_salary replace x with this variable

Annual salary: annual_salary replace y with this variable

Monthly Expenditure: month_expenditure replace z with this variable

Annual Expenditure: replace o with this variable for year_expenditure

Year storage: year_storage replace t with this variable

Then we recalculate the annual storage (annual salary)

hour_salary = 60
annual_salary = hour_salary * 8 * 22 * 12
month_expenditure = 2500
year_expenditure = month_expenditure * 12
year_storage = annual_salary - year_expenditure
print(year_storage)
96720

Python variable naming rules

It must start with an English letter _ (underline) or a Chinese character, it is recommended to use English letters

Variable names can only be composed of English letters, numbers, (underlined) or Chinese characters.

English letters are case sensitive, for example, Name and name are treated as different variable names.

Note: Python system reserved words (or keywords) or Python built-in function names cannot be interpreted as variable names.

The following are system reserved keywords that cannot be used as variable names

 The following are Python system built-in functions that cannot be used as variable names. If you accidentally use the system built-in function name as a variable, the program itself will not error, but the original function function will be lost.

 

note

Comments are explanations and descriptions of the code, and their purpose is to make it easier for people to understand the code. When writing a program, the comment is an explanation or prompt for a statement, program segment, function, etc., which can improve the readability of the program code. Comments are only to improve readability and will not be compiled by the computer. Maybe you still remember the meaning of the code when you just wrote the program. After a long time, you may forget it. This reflects the importance of comments.

We added comments for the above calculation year storage as an example

hour_salary = 60                                    #时薪                                       
annual_salary = hour_salary * 8 * 22 * 12           #年薪
month_expenditure = 2500                            #月花费
year_expenditure = month_expenditure * 12           #年花费              
year_storage = annual_salary - year_expenditure     #年存储 
print(year_storage)                                 #打印年存储

I believe that comments have been added, and you will be able to think of it when you look at the code in ten years

Guess you like

Origin blog.csdn.net/weixin_50877409/article/details/129518829