当前位置:网站首页>1.4 learn more about functions
1.4 learn more about functions
2022-07-03 19:59:00 【leeshuqing】
Let's continue with the simplest one last time Python On the basis of the program , Let's take a look at the usage of functions .
We changed the code , Can you understand what this code means ?
str = input() # Such as input bye,str by ‘bye’
str = str.upper() # here str by ‘BYE’
print(str) # Output ‘BYE’It means the same thing , Just in the middle step, first convert the characters to uppercase , And replaced the original str Variable . You can also see from it , So-called Python Programming is like this , Write line by line , One function per line , It's like piling up wood , Put together a complete function . Of course , Although the function of this code is consistent with the previous , But there are some differences . This is the old code :
str = input() # Such as input bye,str by ‘bye’
print(str.upper()) # Output ‘BYE’,str by ‘bye’
In the previous code ,str Always input in lowercase bye, But in this new code , because str Has been replaced , Therefore, the original input content will no longer be available .
You may also notice , There is a text beginning with a pound sign after each code sentence , This is called annotation , Generally, it is some textual annotation and description , For people to understand the meaning of the code .
meanwhile , Even paste this code directly into PyCharm in , You'll find out , The statement content of these comments does not affect the operation of the program , And it is displayed in light gray .

This function is very useful , Sometimes we want to temporarily cancel the execution of a piece of code , Deleting it directly is not a good idea , It's not easy to find the code again , therefore , It can be handled by temporarily commenting into code . such as , We want to temporarily mask the second line of code of this uppercase conversion function , You can click on this line , Press down Ctrl+/, You will find that it automatically becomes a comment :

Press again and it will change back , This is a toggle key . If you select multiple lines , You can comment on multiple lines .
Maybe some readers will think whether it is possible to write like this :
print(input())The effect is completely OK , It takes the input directly as print The parameters of the function , That is, direct output .
thus , We have written three versions of the same function :

Then why do we define variables str To save the input ? There is a key problem involved here : When do I need to define variables ?
The answer is simple , If you need to continue to use the previous data content later , Then you should consider saving it to a specific variable . If direct output is adopted :
print(input())The input here is not saved , Although it can be output , But if the input content needs to be processed again later , You will find that you will not be able to get the previous input . conversely , If you have saved the input to str variable :
str = input()
print(str.upper())
Then you can pass at any time str Variables get the input again . It's like using money , If you need money tomorrow , Then you need to store the required amount in advance . It's the same thing !

The problems that beginners often encounter here are the random addition of indentation and unnecessary spaces in the code , It is highly recommended that you do not add unnecessary spaces at will , It can even lead to errors . For example, unnecessary spaces have been added at the beginning of the second and third lines , Led to a mistake ( The meaning of the error message is unexpected code indentation , There are spaces at the beginning of the line. Of course, this line of code is indented ):

The specific reasons will be explained later . Here you can add a very useful function , Often after writing too much code , There may be many careless spaces , The format will be messy . It is suggested that you often press Ctrl+Alt+L, You'll find out PyCharm Auto layout code , Remove unnecessary spaces , Align code, etc , Very convenient . For example, the above code will be immediately typeset as :

Only this and nothing more , We give you a brief explanation of the basic Python Program , You can find that it seems to be composed of various variables and functions . From the cognitive level, the meaning of variables and functions , We can make a conclusion ,Python The code of is composed of various variables and functions :

The most obvious surface difference between variables and functions is that there are parentheses after functions , Even if there is no content . In terms of function , The difference between the two can be understood as , Variables express some static characteristics , Functions express some dynamic functions , Therefore, the combination of the two can completely express the things in the objective world and their related relationships , So as to reproduce these things and relationships through code logic , It realizes the specific logic functions we need ( A kind of thinking similar to programming philosophy ).
among , Functions seem more strange , Variables can be defined by themselves , But functions often need ready-made functions . occasionally , We're going to have a problem , We don't know which functions ? This is about to learn , Meet one, know one , in the course of time , You can learn some of the most common functions . Most of the data analysis functions of our course are provided by these functions , We don't need to know the details , Just know how to use them , It is enough to complete the data analysis tasks we need .
Supporting learning resources 、 MOOC video :
边栏推荐
- 10 smart contract developer tools that miss and lose
- Global and Chinese market of charity software 2022-2028: Research Report on technology, participants, trends, market size and share
- Leetcode 1189. Maximum number of balloons (special character count)
- 2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
- BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
- Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- How to check the permission to write to a directory or file- How do you check for permissions to write to a directory or file?
- Explore the internal mechanism of modern browsers (I) (original translation)
- Xctf attack and defense world crypto advanced area best_ rsa
- [Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
猜你喜欢

Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!

Part 28 supplement (XXVIII) busyindicator (waiting for elements)

Xctf attack and defense world crypto advanced area best_ rsa

BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply

第二十章:y= sin(x)/x,漫步坐标系计算,y= sin(x)/x 带廓幅图形,奥运五环,小球滚动与弹跳,流水显示,矩形优化裁剪,r个皇后全控nxn棋盘

01 - QT OpenGL display OpenGL window

Chapitre 1: le roi de shehan a mal calculé

Chapter 1: simplify the same code decimal sum s (D, n)

Explore the internal mechanism of modern browsers (I) (original translation)
![2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]](/img/b6/5d6b946d8001e2d73c2cadbdce72fc.png)
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
随机推荐
2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration
Xctf attack and defense world crypto advanced area best_ rsa
Nacos usage of micro services
Difference between surface go1 and surface GO2 (non professional comparison)
04 -- QT OpenGL two sets of shaders draw two triangles
unittest框架基本使用
Day11 ---- 我的页面, 用户信息获取修改与频道接口
Derivation of decision tree theory
Professional interpretation | how to become an SQL developer
Promethus
Chapter 1: simplify the same code decimal sum s (D, n)
2022-06-28 网工进阶(十三)IS-IS-路由过滤、路由汇总、认证、影响ISIS邻居关系建立的因素、其他命令和特性
第一章:三位阶乘和数,图形点扫描
Phpstudy set LAN access
Chapter 1: drinking soft drinks, step tariff calculation, step tariff calculation function, personal income tax, solving square root inequality, simplifying solving square root inequality, solving dem
第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[x,y]内的守形数,探求n位守形数,递推探索n位逐位整除数
44. Concurrent programming theory
第二章:求a,b的最大公约与最小公倍数经典求解,求a,b的最大公约与最小公倍数常规求解,求n个正整数的的最大公约与最小公倍数
Popularize the basics of IP routing
Chapter 2: find the classical solution of the maximum Convention and the least common multiple of a and B, find the conventional solution of the maximum Convention and the least common multiple of a a