当前位置:网站首页>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 :
边栏推荐
- Ruby replaces gem Alibaba image
- Strict data sheet of new features of SQLite 3.37.0
- 2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
- Chapter 1: simplify the same code decimal sum s (D, n)
- 第一章:三位阶乘和数,图形点扫描
- PR notes:
- Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla
- 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
- 2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]
- Global and Chinese market of charity software 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Promethus
03 -- QT OpenGL EBO draw triangle
AcWing 1460. Where am i?
Phpstudy set LAN access
Acquisition and transmission of parameters in automatic testing of JMeter interface
Point cloud data denoising
FPGA 学习笔记:Vivado 2019.1 工程创建
5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
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
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
随机推荐
3. Data binding
Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
Implementation of stack
第一章:递归求n的阶乘n!
[raid] [simple DP] mine excavation
Chapter 1: find the factorial n of n!
How to improve data security by renting servers in Hong Kong
Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
Chapter 2: find the box array, complete number in the specified interval, and improve the complete number in the specified interval
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
Popularize the basics of IP routing
2022-06-28 网工进阶(十三)IS-IS-路由过滤、路由汇总、认证、影响ISIS邻居关系建立的因素、其他命令和特性
44. Concurrent programming theory
QT tutorial: signal and slot mechanism
Acquisition and transmission of parameters in automatic testing of JMeter interface
01 - QT OpenGL display OpenGL window
Microservice knowledge sorting - search technology and automatic deployment technology
CMD implements the language conversion of locale non Unicode programs
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
第一章:求n的阶乘n!