Welcome again TurkHackTeam family. Today I will be giving you the 4th lesson of our series. I hope you will like it.
print() Function
In previous lessons, I introduced Python and explained its interactivity shell. We have reinforced them with a few examples. This time, we're going to talk about an essential function of Python, the print function.
What Is print(), What Does It Do?
In our previous lessons, we printed our data directly to the screen in the Python shell. So as follows;
We were getting a printout like above. These codes are working now, but we will have to print and change them in different files in our future projects. Then we cannot use it this way. When we write and save these codes in our file, we cannot get any value. We will use the "print()" function to av0id this.
The "print()" function is used to print the data we specified on the screen. For example;
As the output we get when we write as here.
We printed a string of characters on the screen (Hello THT Family). Well, when we do as we wrote before, we get a printout on the screen. Doesn't this happen? Actually, we do not print. You will see that nothing happens when we write straight in the applications I will show in the next lessons. Since we are in the interaction shell, we are provided with convenience, but you cannot do this in our future projects.
Its Usage
I have mentioned its usage above, but it is useful to go into a little more detail. In the print() function, we operate by writing parameters in parentheses. The print() function has many parameters.
Let's see its usage by assigning it to a variable as another use. For example;
As you can see in the code, we first assign our character data to a variable named "THT". Then we printed our variable named "THT".
Before moving on to the other parameters, I think it is useful to make a statement about the quotation marks. Python also has three different quotes.
('') Single Quote
("") Double Quote
(""" """) Three Quotes
As an example usage;
As you can see, there seems to be no difference, isn't it? Actually there are big differences. I want to make one more example to show the difference.
we got an error
The reason why we got the error here is the starting quotation mark in our sentence at first, but then the sentence ends with another quotation mark, exactly in the 'Hello' part. Then the word THT was left out and a new sentence started. Python cannot understand where the directory starts and ends and gives an error. So how do we use it?
We need to use both single and double quotes in our array. For example;
As you can see, we didn't get any errors. Because Python knows that the beginning of the array is double quotes and the ending will be double quotes. When there is a single quote in between, it perceives it as a character and continues.
Usage is the same for all three quotation marks.
We got the printout there was no problem. However, generally single or double quotes are used. The most important difference of three quotes from other quotes is that you can continue writing by skipping three quotes.
You will get an error when you try to skip lines with double quotes.
But as you can see in the three quotes, we were able to continue our series by skipping lines.
In the print() function, we can also write by separating the arrays. So we have more than one value, but these are different values and if you want to print them separately;
As you can see, you can print multiple series this way.
Parameters Used In The print() Function
You already understand how important the print() function is. Let's watch him bring his power to nirvana with parameters.
sep Parameter
Sep stands for "Seperator". With the print() function, we can add whatever we want to add between the arrays to be printed on the screen with this parameter. Above we have done this by putting spaces or adding different things ourselves, but we will do this automatically with sep.
As you can see, we didn't add any value between them when we used it, but it put "//" signs in between. The sep parameter did this. You can put it in space or put it in different characters if you want. I want you to fully understand the work by doing a few more examples.
Like here, at first Python automatically gave us output by leaving a space. On the other hand, we made it print on the screen by leaving more than one space with the sep parameter.
end Parameter
The sep parameter specifies which character will come between arrays. The end parameter allows us to add a value at the end of the array.
As you can see here, there was no sign at the end of the array, but through the end parameter, we put the "." (dot) mark. Let's finish this parameter with a few examples.
It can be used this way.
File
We can print data to the python shell with the print() function. With the file parameter, we can write data to a file or get data and write it to the python shell. As an example, I will use a print() function with a file parameter and also I'll print() as a normally use.
We were able to print this directly on the python shell. Let's try to print it to a file using the file parameter. For print this;
Let's explain the purpose of the codes here. First, we created a file named "tht.txt" and assigned it to a variable named our file. I will explain the Open() function in the next lessons, but you just need to know that it is used in creating files. "w" is the writing mode. So we specified that the file will be opened with the write-in feature. In this way, we created our file. The file we created was created in the directory where we are located. If you are operating in the python shell like me, you can find your file in this extension "C:\Users\user_name\AppData\Local\Programs\Python\Python37".
Then we printed data with the print() function. However, we did this to be in our file. Then we closed the file with the "ourfile.close()" command. The purpose of this is to save the data we write. When we open the "tht.txt" file, you will see the text "Hello THT" written in it.
This part ends here. See you in the next part...
Source: https://www.turkhackteam.org/python/1895626-python-egitimi-part-4-p4rs.html
Translator: Dolyetyus
print() Function
In previous lessons, I introduced Python and explained its interactivity shell. We have reinforced them with a few examples. This time, we're going to talk about an essential function of Python, the print function.
What Is print(), What Does It Do?
In our previous lessons, we printed our data directly to the screen in the Python shell. So as follows;
Kod:
>>> "Welcome THT Family"
'Welcome THT Family'
>>>
We were getting a printout like above. These codes are working now, but we will have to print and change them in different files in our future projects. Then we cannot use it this way. When we write and save these codes in our file, we cannot get any value. We will use the "print()" function to av0id this.
The "print()" function is used to print the data we specified on the screen. For example;
Kod:
>>> print("HelloTHT Family.")
HelloTHT Family.
>>>
We printed a string of characters on the screen (Hello THT Family). Well, when we do as we wrote before, we get a printout on the screen. Doesn't this happen? Actually, we do not print. You will see that nothing happens when we write straight in the applications I will show in the next lessons. Since we are in the interaction shell, we are provided with convenience, but you cannot do this in our future projects.
Its Usage
I have mentioned its usage above, but it is useful to go into a little more detail. In the print() function, we operate by writing parameters in parentheses. The print() function has many parameters.
Let's see its usage by assigning it to a variable as another use. For example;
Kod:
>>> THT = "Hello THT Family"
>>> print(THT)
Hello THT Family
>>>
Before moving on to the other parameters, I think it is useful to make a statement about the quotation marks. Python also has three different quotes.
('') Single Quote
("") Double Quote
(""" """) Three Quotes
As an example usage;
Kod:
>>> print("Hello THT Family")
Hello THT Family
>>> print('Hello THT Family')
Hello THT Family
>>> print("""Hello THT Family""")
Hello THT Family
>>>
As you can see, there seems to be no difference, isn't it? Actually there are big differences. I want to make one more example to show the difference.
Kod:
>>> print('Hello 'THT' Family')
SyntaxError: invalid syntax
>>>
The reason why we got the error here is the starting quotation mark in our sentence at first, but then the sentence ends with another quotation mark, exactly in the 'Hello' part. Then the word THT was left out and a new sentence started. Python cannot understand where the directory starts and ends and gives an error. So how do we use it?
We need to use both single and double quotes in our array. For example;
Kod:
>>> print("Hello 'THT' Family")
Hello 'THT' Family
>>>
As you can see, we didn't get any errors. Because Python knows that the beginning of the array is double quotes and the ending will be double quotes. When there is a single quote in between, it perceives it as a character and continues.
Usage is the same for all three quotation marks.
Kod:
>>> print("""Hello 'THT' Family""")
Hello 'THT' Family
>>>
We got the printout there was no problem. However, generally single or double quotes are used. The most important difference of three quotes from other quotes is that you can continue writing by skipping three quotes.
Kod:
>>> print("Hello
SyntaxError: EOL while scanning string literal
>>>
Kod:
>>> print("""Hello
THT
Family""")
Hello
THT
Family
>>>
In the print() function, we can also write by separating the arrays. So we have more than one value, but these are different values and if you want to print them separately;
Kod:
>>> print('THT', 'Turk', 'Hack', 'Team')
THT Turk Hack Team
>>>
As you can see, you can print multiple series this way.
Parameters Used In The print() Function
You already understand how important the print() function is. Let's watch him bring his power to nirvana with parameters.
sep Parameter
Sep stands for "Seperator". With the print() function, we can add whatever we want to add between the arrays to be printed on the screen with this parameter. Above we have done this by putting spaces or adding different things ourselves, but we will do this automatically with sep.
Kod:
>>> print("THT", "Turk", "Hack", "Team", sep='//')
THT//Türk//Hack//Team
>>>
As you can see, we didn't add any value between them when we used it, but it put "//" signs in between. The sep parameter did this. You can put it in space or put it in different characters if you want. I want you to fully understand the work by doing a few more examples.
Kod:
>>> print("Today","it's","too","hot")
Todat it's too hot
>>> print("Today","it's","too","hot", sep=" ")
Today it's too hot
>>>
Like here, at first Python automatically gave us output by leaving a space. On the other hand, we made it print on the screen by leaving more than one space with the sep parameter.
end Parameter
The sep parameter specifies which character will come between arrays. The end parameter allows us to add a value at the end of the array.
Kod:
>>> print("Today","it's","too","hot", end=".")
Todat it's too hot.
>>>
As you can see here, there was no sign at the end of the array, but through the end parameter, we put the "." (dot) mark. Let's finish this parameter with a few examples.
Kod:
>>> print("Hello THT Family", end=" P4RS")
Hello THT Family P4RS
>>>
It can be used this way.
File
We can print data to the python shell with the print() function. With the file parameter, we can write data to a file or get data and write it to the python shell. As an example, I will use a print() function with a file parameter and also I'll print() as a normally use.
Kod:
>>> print("HelloTHT")
HelloTHT
We were able to print this directly on the python shell. Let's try to print it to a file using the file parameter. For print this;
Kod:
>>> ourfile= open("tht.txt", "w")
>>> print("Hello THT", file =ourfile)
>>>ourfile.close()
Let's explain the purpose of the codes here. First, we created a file named "tht.txt" and assigned it to a variable named our file. I will explain the Open() function in the next lessons, but you just need to know that it is used in creating files. "w" is the writing mode. So we specified that the file will be opened with the write-in feature. In this way, we created our file. The file we created was created in the directory where we are located. If you are operating in the python shell like me, you can find your file in this extension "C:\Users\user_name\AppData\Local\Programs\Python\Python37".
Then we printed data with the print() function. However, we did this to be in our file. Then we closed the file with the "ourfile.close()" command. The purpose of this is to save the data we write. When we open the "tht.txt" file, you will see the text "Hello THT" written in it.
This part ends here. See you in the next part...
Source: https://www.turkhackteam.org/python/1895626-python-egitimi-part-4-p4rs.html
Translator: Dolyetyus
Moderatör tarafında düzenlendi: