"Learning Python with You Hand in Hand" 4-Hello World!

After sharing the first three articles, I believe you have installed Python (Anaconda) and the integrated development environment PyCharm, and now you must be impatient to start writing Python code. For those who have not finished, please refer to the following three articles, configure the running environment, and then you can play Python with everyone.

"Learning Python with You Hand in Hand" 1-Why learn Python?

"Learning Python with you hand in hand" 2-Python installation

"Learning Python with You Hand in Hand" 3-PyCharm installation and configuration

If there is any program that all programmers will inevitably run, and it will run at the very beginning, it is undoubtedly printing out this sentence:

Hello World!

So, today we also start our Python journey with this sentence, and at the same time introduce to you one of the most commonly used functions of Python-printing:

print()

Not much else to say, let's complete such a task with everyone-print Hello World!

Open the installed PyCharm, click on the Python Console in the lower left corner, you will see the following interface.

Enter after In[2]:

print('Hello World!')

Press Enter and you will see the output result:

Hello World!

This completes the first task of the Python journey! congratulations!

Because I am just a rookie who has just been exposed to Python for less than half a year, the content I share below is what I have learned through contact in the past half year.

After all, the learning time is very short and the understanding is very limited, so the introduction may be relatively simple, but it should be the content that just needs to be used when you are just getting in touch with Python, so I will try to use the most popular content based on my own learning experience. Speak out the language. I will try my best to help everyone level out the pits I encountered in the process of learning.

Okay, let's start the official sharing.

The print you used just now is actually a function, more accurately a built-in function of Python, which means it can be used without additional installation.

A function can be understood as a command first, which is what you command the computer to do, for example: print Hello World! , Which is the print('Hello World!') we just typed in.

Print is a function. The things in parentheses are called parameters, which can be understood as the specific requirements of what we order the computer to do, such as what to print.

Although we just wrote'Hello World!' in parentheses, in fact, his parameters are not only that, but the complete function and parameter list should look like this.

print(objects, sep=' ', end='\n', file=sys.stdout)

Below we will introduce each parameter of the print function one by one, and use examples to show its different printing effects.

1. Object parameters

The objects parameter is the content to be printed, which can be one content or multiple content. In fact, the content here should be called objects, there are many kinds of objects in Python, because we haven't learned there yet, we won't expand it first, and we will naturally use it then.

The previously printed "Hello World!" is also an object, called a string, which needs to be enclosed in two single quotation marks "Hello World!" or two double quotation marks "Hello World!" in English to be called a string. The quotation marks can neither be omitted nor mixed, otherwise an error will be reported and not printed.

Because you can print multiple objects, you can also print multiple strings, all enclosed in quotation marks, but separated by commas in English. For example, print('Hello World!','Hello World!','Hello World!')

You can try it. The output at this time is:

Hello World! Hello World! Hello World!

2. Sep parameter

If you look closely, you can see that there is a space between every two sentences of the printed result above.

This is because the default value of the sep parameter in print is a space, which is'', so there will be a space between the two printed objects. If we change to other symbols, the computer will also add the symbols we require after each object is printed according to our requirements. such as:

print('Hello World!', 'Hello World!', 'Hello World!', sep='yes!')

The output is:

Hello World!yes!Hello World!yes!Hello World!

This is because sep represents the interval symbol, which will only appear in the middle of multiple print objects, and will not appear at the end of the entire print content. So in the above example, the third yes! is not printed at the end of the sentence.

So what will appear at the end of the printed content? This is the purpose of the end parameter.

3. The end parameter

The function of the end parameter is to control what ends the entire print task.

The end here is not what we just entered!, but the end automatically added by the print function, the default is a newline character \n, just like our enter key.

So, when we enter two commands in a row:

print('Hello World!')

print('Hello World!')

The result is:

Hello World!

Hello World!

Did you have a problem when you typed two lines of print('Hello World!') just now? Just after typing the first line, press Enter to run. (In[5] as shown below)

In PyCharm's Console, you can press Shift+Enter at the same time to achieve non-running line breaks, and you can enter multiple lines of commands. (In[6] as shown below)

If we change the end parameter, it will also become yes!

The command becomes:

print('Hello World!', end='yes!')

print('Hello World!', end='yes!')

The result becomes:

Hello World!yes!Hello World!yes!

Because there is no \n at the end, there will be no line breaks.

4. File parameter

The last file parameter is the file object to be written, because it has not been studied and will not be used temporarily, so I won't introduce it first.

Finally, I will share a little experience with you.

In the process of learning, you must predict the result of each sentence by yourself before running, and see if the running result of the program is consistent with your prediction. If they are consistent, it means that we have a good grasp of the function of the sentence. If they are inconsistent, you must consider yourself where the problem occurred, whether it was an input problem, or a function or parameter problem, which resulted in inconsistent results.

In this article, I have introduced the usage of the print function and its parameters. I believe that you can achieve free output according to your own wishes, which is the first step for us to control and talk to the computer. At the same time, PyCharm's Console function is also used to realize the input and output of the program.

In fact, the advantage of PyCharm lies in the compilation and running of large projects. It is not particularly friendly to the running and testing of our single statement, and it is not very convenient to input.

So in order to better demonstrate to you, in the next article, I will introduce a new software to you. Although it is the Web version, it can also execute Python commands, and in terms of operational convenience and visualization, it is better than the PyCharm Console function we use today.

 

 


Thanks for reading this article! If you have any questions, please leave a message and discuss together ^_^

Welcome to scan the QR code below, follow the "Yesu Python" public account, read other articles in the "Learning Python with You Hand in Hand" series, or click the link below to go directly.

"Learning Python with You Hand in Hand" 1-Why learn Python?

"Learning Python with you hand in hand" 2-Python installation

"Learning Python with You Hand in Hand" 3-PyCharm installation and configuration

For Fans: Follow the "also said Python" public account, reply "Hand 4", you can download the sample sentences used in this article for free.

Also talk about Python-a learning and sharing area for Python lovers

 

Guess you like

Origin blog.csdn.net/mnpy2019/article/details/98749374