[Python is not a python] Explain HelloWorld in simple terms | Comments | Input and output

Ⅰ. Explain HelloWorld in simple terms

0x00 HelloWorld

We use vim as an example when writing code. If you are using a windows system, you can use vim in gitbash.

VSCode is the number one IDE in the universe, you should use VSCode!

PyCharm is the best IDE for Python, what vim should I use?

Jupyterlab is the best!

。。。。。

VSCode and PyCharm have excellent code completion, highlighting and automatic error finding, which are particularly friendly to novices.

But after using it for a long time, you will find that you are dependent on the IDE. Without the IDE, it will be useless!

Therefore, I recommend everyone to use vim as an IDE, also to become familiar with the terminal.

 Okay, no longer poor!

 We open the terminal and use gitbash under Windows (you don’t have to install it yourself, it’s almost the same on Linux).

Enter the command to create the folder:

mkdir PythonLearn

 Then enter the command to move to the folder

cd PythonLearn

After executing these two lines of commands, you will find that the working area is now in the folder you just created.

 

 Next, we create a new HelloWorld file and enter it on the command line

touch HelloWorld.py

When we use the dir command, we will find that there is an additional HelloWorld.py file in the folder. We enter the vim command to edit the file.

input the command

vim HelloWorld.py

 

When this interface appears, press the i key to enter the editing interface (if there is any problem, please refer to Baidu, we pay attention to programming)

 This will successfully enter the editing interface, let’s start writing code!

After a moment of concentration, I splashed ink and solemnly wrote two lines of code:

# 宇宙第一经典程序
print("Hello World")

 

 Then we press the ESC key to enter command mode

Enter two ZZ (note the capital letters), save the file and exit

Next we enter the command and run the code

python HelloWorld.py

 HelloWorld is output on the screen, we are successful, we have entered the code of the programming world! ! !

 

(Let’s liven up the atmosphere)

0x01 What exactly did we write? 

Let’s review the code just now

We enter in the terminal and display the file content

cat HelloWorld.py

 

# 宇宙第一经典程序
print("Hello World")

Let's look at the first line first

# 宇宙第一经典程序

This is just a note, we won’t talk about it now, we will talk about it later.

look at line 2

print("Hello World")

This is a statement that calls a built-in function. You may not understand what a built-in function is.

Calling a function means there is an encapsulated function, and we take out this function and use it

one of them

"Hello World"

This is a string, and this string is also used as a parameter for the function print

Passing parameters can be understood as this function needs something, you just give it to it and let it run

I’ve been talking about it for a long time but haven’t printed anything yet. We won’t talk about it here for now, but we’ll talk about it later.

We mainly need to understand these concepts.

Ⅱ. Notes

We said in the previous chapter that comments should be left for later, so let’s talk about them now.

Comments are used to prompt or explain the role and function of certain codes to the user. They can appear anywhere in the code. The Python  interpreter ignores the comment and does nothing with it when executing the code, as if it does not exist.

The biggest role of comments is to improve the readability of the program. A program without comments is simply a bible that makes people vomit blood!

Never think that you can write code specifications without comments. Giving others a piece of code without comments is disrespectful to others and a very selfish act; you can like to abuse yourself, but please don't abuse others.

Many programmers would rather develop an application themselves than modify other people's code. The lack of reasonable comments is an important reason. Although good code can speak for itself, we never know who will read this code in the future and whether he has the same idea as you; or after a period of time, you yourself will not know the purpose of writing this code at that time. .

Generally speaking, reasonable code comments should account for about 1/3 of the source code.

Python supports two types of comments, single-line comments and multi-line comments.

0x00 single line comment

Python3 uses # as a single-line comment symbol, that is, the content after the # symbol (until the newline) will be ignored by the interpreter. The format is:

# 我是一个单行注释

However, the content before the # sign will not be affected.

When describing the function of multiple lines of code, comments are generally placed on the previous line of code, for example

# 输出字符串
print("Hello")
print("Python")


When describing the function of a single line of code, comments are generally placed on the right side of the code, for example

print( 36.7 * 14.5 ) #输出乘积

print( 100 % 7 ) #输出余数

0x01 multi-line comment

Multi-line comments refer to commenting on multiple lines of content (including one line) in the program at once.

Python uses three consecutive single quotes ''' or three consecutive double quotes """ to comment multi-line content. The specific format is as follows:

"""
我是一个多行注释
"""
Precautions

1) Python multi-line comments do not support nesting

2) Whether it is a multi-line comment or a single-line comment, when the comment characters appear as part of the string, they can no longer be regarded as comment marks, but should be regarded as part of the normal code

0x02 Special file encoding comment

When interpreting files in Python, utf-8 is generally used to decrypt the file. If we edit the file with other encodings, decryption will fail, so we can write a line of comments at the beginning to indicate the file encoding format.

For example, the file is encoded with gbk

Just add the following comment

# coding=gbk

Decryption will be successful

Other encoding formats are expressed in this format:

# coding=编码格式

Another little trick

Ⅲ.Input and output

A user-friendly program must be indispensable for input and output, and Python’s support for this is also very complete.

0x00 print function

We have already seen the print function in HelloWorld. Its function is to output data to the screen.

We can get help for him through the help function

Reference translation:

关于模块内置的内置函数打印的帮助:



print(…)

print(value,…,sep='',end='n',file=sys.stdout,flush=False)



将值打印到流,或者默认情况下打印到sys.stdout。

可选关键字参数:

文件:类似文件的对象(流);默认为当前sys.stdout。

sep:插入值之间的字符串,默认为空格。

end:附加在最后一个值后面的字符串,默认为换行符。

flush:是否强制冲洗流。

If you want to output multiple strings, you can do this: If separated by commas, each string will be separated by a space.

print('hello', 'world', 'python')
#输出:hello world python

end will add the specified character at the end of the output content. In fact, if the value of end is not specified, the default is the newline character, which is \n. So if you print twice, it will be displayed as two lines instead of one line.

print('hello', end='#')

# 输出: hello#

0x01 input function

In python, use input to prompt the user to enter content

 Reference translation:

关于模块内置中内置函数输入的帮助:



input(提示=无,/)

从标准输入中读取字符串。尾部换行符被剥去。



提示字符串(如果给定)将打印到标准输出,而不带

在读取输入之前尾随换行。



如果用户点击EOF(*nix:Ctrl-D,Windows:Ctrl-Z+Return),则引发EOFError。

在*nix系统上,如果可用,则使用readline。
input('请输入你的姓名:')

# 输出: 请输入你的姓名:

After entering your name, press Enter, that is, press Enter to confirm, and then the function will return the entered string.

Guess you like

Origin blog.csdn.net/m0_73552311/article/details/132744477
Recommended