Python Tutorial - Part 4 / By P4RS

Dolyetyus

Özel Üye
21 Nis 2020
1,207
676
Delft
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;
Kod:
>>> "Welcome THT Family"
'Welcome THT Family'
>>>

IL69dV.png


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.
>>>
As the output we get when we write as here.

QG6Ad5.png


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
>>>
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".

yS5dV8.png


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
>>>

RyfVfP.png


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
>>>
we got an error

cBScIV.png


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
>>>

3I9Wb1.png


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
>>>

Q5R8Pa.png


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
>>>
You will get an error when you try to skip lines with double quotes.

3xG3OK.png


Kod:
>>> print("""Hello
THT
Family""")
Hello
THT
Family
>>>
But as you can see in the three quotes, we were able to continue our series by skipping lines.

VzHLKJ.png


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
>>>

cxSUO2.png


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
>>>

1B2cIR.png


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
>>>

GAPBCf.png


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.
>>>

Wb0274.png


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
>>>

S8RMd9.png


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

H0z43a.png


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()

cJdQSP.png


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.


KcK588.png


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:
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.