当前位置:网站首页>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 :
边栏推荐
- IP address is such an important knowledge that it's useless to listen to a younger student?
- Chapitre 1: le roi de shehan a mal calculé
- Day11 ---- 我的页面, 用户信息获取修改与频道接口
- Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
- IPv6 experiment
- 02 -- QT OpenGL drawing triangle
- unittest框架基本使用
- 47. Process lock & process pool & Collaboration
- 10 smart contract developer tools that miss and lose
- Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
猜你喜欢
Xctf attack and defense world crypto master advanced area olddriver
第一章:简化同码小数和s(d, n)
Virtual machine installation deepin system
2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
Wechat applet quick start (including NPM package use and mobx status management)
Chapter 2: find the box array, complete number in the specified interval, and improve the complete number in the specified interval
Teach you how to quickly recover data by deleting recycle bin files by mistake
Derivation of decision tree theory
BOC protected tryptophan zinc porphyrin (Zn · TAPP Trp BOC) / copper porphyrin (Cu · TAPP Trp BOC) / cobalt porphyrin (cobalt · TAPP Trp BOC) / iron porphyrin (Fe · TAPP Trp BOC) / Qiyue supply
随机推荐
01 - QT OpenGL display OpenGL window
Nerfplusplus parameter format sorting
Sword finger offer 30 Stack containing min function
Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
Use of aggregate functions
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
PR 2021 quick start tutorial, material import and management
2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
Chapitre 1: le roi de shehan a mal calculé
第一章:三位阶乘和数,图形点扫描
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
Unittest framework is basically used
Micro service knowledge sorting - asynchronous communication technology
Day10 -- forced login, token refresh and JWT disable
2022-06-28 网工进阶(十三)IS-IS-路由过滤、路由汇总、认证、影响ISIS邻居关系建立的因素、其他命令和特性
Microservice knowledge sorting - search technology and automatic deployment technology
5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes