📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Уроки Python с нуля / #3 – Базовые операции в языке Python

Школа itProger / Программирование19:34

Transcription

Hello! In this lesson, we will familiarize ourselves with the basic concepts and terms in the Python language.

Before we begin, I would like to recommend the website itprager.com. On this site, you will find their homework assignments and a lot of other useful information. The link to this lesson on the site will be in the description of this video.

So, first, I suggest we talk about comments. Comments are an important part of the code as they allow you to describe a specific section of code. Later, you or other developers will quickly understand what is happening in a certain part of your project.

To create a comment, you use the hash symbol (#). After this symbol, you can write anything you want, as all the text you write here will be considered a comment. You can describe what exactly will happen in your project below.

If you need to create a multi-line comment, you can place a new hash symbol at the beginning of each line. This will also be a comment. Comments are convenient because they allow us to describe a specific part of the code. Even if you forget about your project and return to it in 2-3 years, if you have comments, you will quickly understand what is in a certain part of the project.

So, don’t forget about comments. Sometimes it’s worth writing them, but you shouldn’t write them for every line of code, as that would only worsen the readability of the code. However, for larger sections of code, comments can be useful for both you and other developers.

Now, let’s talk about the print function and how to output various information to the console or terminal of our project. In the previous lesson, we touched on this function and learned how to output something to the console. Here, let’s work with it a bit more.

Let’s say I want to see not just a specific text, but I would like to output a number. For example, I want to output the number 5. I simply write this number here, and if I run the program now, the number 5 will be displayed.

If I want to output not just the number 5 but also some text along with it, I can write double quotes here and include some text. For example, I can write "result" and then add the number 5. I could write it directly here, and it will be output as a regular string. If I run it like this, everything will be processed correctly.

Alternatively, if I want to output them separately, I can write a comma here and then write another number, for example, 5. If I run it again, everything processes correctly.

This way, you can either output everything in one common line or, if you have multiple outputs, you can combine them using commas. For example, here I want to output the word "result," then the number 7, and then, say, the number 15. If I write all this with commas, everything will process correctly.

We notice that the output includes a string, a second number, and a period. Everything processed correctly. When outputting values, we notice that a space is always placed between them. We can actually remove this space.

To do this, we need to set an additional property at the very end. It’s called "sep" or "separator." Here, we simply specify a certain string that will separate each element. If I specify a vertical bar here, it means that each element will be separated by this vertical bar.

If I specify an empty string here, then no spaces or anything additional will be added. We get what looks like one common string. Here, the word "result" is followed by the number 715, which combines the output of 7 and 15, plus a period at the end.

So, with "sep," we indicate a symbol or several symbols that will separate each element when displayed on the screen.

Additionally, if we try to output another result, let’s say "second line," we will notice that our first print and the second print are displayed on different lines. The first print outputs in the first row, and the second in the second row.

If you want to output everything on one line, it’s easiest to write it all in one print statement. You can add another comma here and write "second line." This will allow you to write everything in one row.

Besides that, you can also use an additional property called "end," which sets the end of the string. By default, each print adds a newline character at the end. This newline character is represented as "\n," which indicates that a new line is needed.

If we write this value now, nothing will change. The first print outputs its data, and at the end, it adds a newline character, so the next print outputs on the next line.

However, if I specify an empty string as the value for "end," then everything will be displayed on one line. This happens because now the first print does not perform a newline, so the second print outputs its information in the same line.

Thus, with "sep," we can specify separators between these values, and with "end," we can specify what will be at the end of the string. If you want, you can specify a certain symbol.

Let’s say I remove the period here and put an exclamation mark instead. In this case, we can see that the exclamation mark is placed at the end of the string. If I need a newline, I can simply add "\n," and I will have a newline. Everything processes correctly.

Since we have touched on special characters, let’s talk a bit more about them.

Suppose I need to output a double quote in this string. If I try to write it directly, I will get an error. The error occurs because we opened the string here and closed it there, and the compiler does not understand what is happening next.

To output a double quote inside double quotes, we need to indicate that it is a regular character. To do this, we use a backslash. When we write a backslash before the double quote, we tell the compiler that this quote is not a closing quote; output it as a regular character.

If I run this now, we see that it outputs correctly without any errors.

Another way to output it is to use single quotes around the string and double quotes inside. In this case, there are no conflicts. If we run this again, everything processes correctly.

One more nuance I forgot to mention is that you can output strings in double quotes or single quotes; it doesn’t affect anything. It’s entirely up to you whether you want to use double or single quotes.

Now, let’s consider a case where you need to output a single quote. You can solve this again using the backslash. You write a backslash followed by a single quote, and in this case, the single quote is not a closing quote and will be output as a regular character.

If you need a newline, you can always write "\n." This is a newline character. If I run this now, we see that each time there is a newline because I wrote "\n" everywhere.

If we talk about special characters, there is a character that creates a tabulation. It is written as "\t." If we run this, we see that there is a large gap between the single quote and the letter "l." This gap acts like a tab character, as if you pressed the Tab key, creating a large indentation.

You can place these characters anywhere, and they will create additional indentations.

If you need to output a backslash, you can write it directly, but you might notice that the editor suggests something is wrong. To output a regular character, you need to place another backslash before or after it. This way, you indicate that this backslash is a regular character and not a special character.

If I run this, it outputs just one backslash. Everything processes correctly.

Now, let’s consider what other actions we can perform with the print method. Besides outputting something, we can also perform mathematical operations.

For example, I want to add 5 to 5. I use the addition symbol (+). If I run this, it outputs the number 10. Everything processes correctly.

Let’s comment out these lines of code to avoid too much output and confusion. By the way, to quickly comment out lines, you can press Control + Slash on Windows or Command + Slash on Mac.

Now, what else can we do with print? We can perform various mathematical operations. I just performed addition, but we can also do subtraction, multiplication, division, or find the remainder.

Let’s perform a multiplication operation. If I run this, we see the correct result. If you want, you can combine this result with some output. For example, we write "result" and then the mathematical operation, and everything processes correctly.

Now, I notice there are two spaces, so I will remove one extra space to make it look better.

Besides standard mathematical operations, we can also perform more complex operations, such as exponentiation. We can say that 5 is raised to the power of 2. If we run this, we get the number 25.

If we raise it to the power of 3, we get 125. We can say that the number 5 is raised to the power of 3. The same can be done with any other numbers. For exponentiation, we use two asterisks (**).

You can also perform division. If we simply divide 5 by 3, we will get a number with many decimal places. If we want to round this number to a whole number, we can use two backslashes (//).

If we run this, we get the number rounded to a whole number, meaning all the decimal values are removed, and only the whole number is displayed.

These mathematical operations exist, and we will discuss them in more detail in the next lesson when we study variables and how to work with them.

In addition to standard mathematical operations, Python also has various functions like the print function that work with mathematical actions.

Let’s comment out this line again and copy it, changing only the second value.

We can use a function called "min." This built-in function exists in Python and allows us to find the minimum element among the given elements. I can pass multiple elements separated by commas, and this function will find the minimum element.

If I run this, we see that -3 is the minimum among all these elements. There is also the opposite function called "max," which finds the maximum element among the given elements.

Besides these functions, there are other functions for mathematical operations. For example, the "abs" function finds the absolute value. If I pass a negative number, it will always return its positive value.

If I pass -5, I will get 5. There are also functions like "pow," which allows us to raise a number to a certain power.

For example, if I want to raise 5 to the power of 3, I can write it like this, and we will get 125.

We can also consider the "round" function, which rounds numbers. If I divide 5 by 3, I should get a number with a decimal point, but we see that we get a rounded whole number.

The round function rounds to the nearest whole number. For example, if we have 1.6, it rounds to 2.

In Python, there are many other functions that allow us to perform various mathematical operations. We won’t study all these functions in detail, as you may never need to work with many of them.

In the course of the video series, if we need a mathematical function, I will explain it. But these are the main functions we just reviewed: the "max" and "min" functions find the maximum or minimum element, the "abs" function gives the absolute value, the "pow" function is essentially equivalent to the two asterisks, and the "round" function rounds numbers.

Now, let’s learn how to get data from the user. At the moment, we cannot interact with this data because we haven’t studied the topic of variables yet.

But for now, let’s just try to learn how to get data from the user. The built-in function "input" is used for this. If print allows us to output something on the screen, input allows us to get values from the user.

If I run the program now, we see that the console displays a message, and the program does not finish because it is waiting for me to input something. I can enter any text or number here, and everything I input will be considered a string. The program will only finish when I press Enter.

After that, we see that the program has finished. We have received data from the user, but we cannot interact with it yet because I haven’t stored it anywhere. This will be covered in the next lesson when we learn to use variables.

However, at least we have learned how to get these values using the input function. Additionally, the input function can take a parameter, where you can write a string that will be displayed to the user.

For example, I can write "Enter your age." If I run this now, it will first display "Enter your age." It would also be convenient to add a colon.

Let’s restart the program. To restart the program, you can click this button. It will ask if I want to do this, and I will check the box so it doesn’t ask again and click to restart.

Now everything works. If I enter any values here, I can later save them in a variable. For now, I can’t do that, but the main point is that when I press Enter, the program will finish.

At this point, we are waiting for the user to input an age, which they can enter, and then we can interact with this age and perform interesting manipulations based on the language.

So, in this lesson, we have become more familiar with the print method for outputting information and the input method for receiving information from the user.

In future lessons, we will often work with print and input. So if something is unclear now, don’t be discouraged, as we will revisit this many times in the future. Even if you don’t remember something now, don’t worry, as we will return to it in the next lessons, and you will definitely remember it.

Well, that concludes our lesson. I hope you enjoyed it. If so, don’t forget to subscribe to the channel and join our social media. All the links will be in the description of this video.

That’s all from me. See you soon! Goodbye!