Размер шрифта
-
+

Python Handbook For Beginners - стр. 3

"Once upon a time…!" () print

If you put it right, you should get a program that prints a sentence. Here it is below.



Are you done with the task? Great! In both programs that we have just created, we used a print function. We will use it a lot in this book. But first, let's dive a little deeper into it.

3 Print Function

Print is the function that does what it's called: it "prints" text on a screen. Programmers use this function to show messages to users. Such as "Your session is expired, please log in" or "Your password is too weak, please use a stronger password," etc.

Not only simple text messages, but the print function can also display different calculation results, presented in numeric format. We will learn all this stuff in the coming chapters. For now, we will focus purely on the print function and code some more with it.

Here is the plain text for you. Please use it with the print function, so when you run the code, it shows you the following message: Hey! I keep coding! Here is what you should get:



Have some fun. Since now you have learned how to write a program that displays messages, experiment with different messages you want your program to print. Use numbers too. Try adding or removing anything from your code. And then watch what happens; This is the best way to learn how the code operates.

4 How Does Python Read The Code?

As soon as you run the code, the computer starts reading it line by line, from top to button. Just like you're reading this book or anything. It may not sound too important, but it's essential to consider it when building and arranging our code; This is why some elements like modules (we will learn them in the following chapters) belong at the top of the code. We import them first to use them further down.

5 Counting Program

Let's build a program that counts from 1 to 3. Here is the code we need to do that:

print("1")

print("2")

print("3")

Pretty simple, right? Now extend the code so that it could count to 10. Here is the result you should get when you run your extended program:



6 Python Challenge

Let's celebrate the completion of the first chapter with a small challenge. You need to complete the below line of code so that it could output the message: "I nailed the first chapter!"

Here is what the output should look like when you fix and run the program:



7 Wrapping Up Chapter One

In the first chapter, we have accomplished the following:

1) Learned what Python is;

2) Understood what a program is;

3) Found out how does Python read the code;

4) Wrote our first line of code;

Страница 3