Python Tutorial - Part 2 / By P4RS

Dolyetyus

Özel Üye
21 Nis 2020
1,207
676
Delft
Hi again TurkHackTeam family, In the previous topic, we showed what Python is and how to install a code editor in GNU / Linux and Windows environments. Let's enter the code structure in this part.



Strings

Last month, we had written "Hello World". Last month, we had written "Hello World". This article is actually a string. You may not have heard of the string, but it is very important for programming.
By definition, strings are made up of one or more characters (alphabets, numbers, special signs).
Now I can hear you say "What does it matter if it's a string or something else?". However, it is of great importance that we know the type of code we write in programming languages. If we learn the type of data, we will understand what we can and cannot do with that data. There are many types of data in Python except strings, and they all have different functionality and functions.
We wrote "Hello World" in our topic last month. The important thing about strings is quotation marks. Quotes are an essential part of strings. Let's write it without quotation marks;
Kod:
>>>Merhaba Dünya

7yyJ35.png


As you can see, we got an error. This is not a string. Character strings are indicated with quotation marks. Let's give a single character array as an example;
Kod:
>>>"x"
this is a single character string. It can also be an empty string

NWHe8G.png


Kod:
>>>""
It gave us a blank character as output.

MxdQ5f.png


However, in Python, there is a huge difference between an empty string and a string consisting of a space. One has no element, but in a string consisting of a space, each of the spaces is a character. This is an important consideration because if you leave a space while writing code, you may get an error where you never expected.
For example, you can do exercises all by yourself;
Kod:
>>>"Ali"
'Ali'
"Türkiye"
'Türkiye'
"Türk Dil Kurumu"
'Türk Dil Kurumu'
these examples can be increased further.

CI77Va.png


Well, we've done some strings, yet we call them strings but could it be something different? We will use the type() function to find out. The type function determines the type of a data.
Kod:
>>>type("Deneme")
<class 'str'>
we printed it out. Here str means string.

3R90xG.png


We learned what strings are and how to look at the type of data. Now let's do some interesting examples of strings. Let's combine strings as an example.
Kod:
>>>"python" + "öğreniyorum"
'pythonöğreniyorum'

GK7UTV.png


It wrote contiguous here because there is no space character. Pay special attention to this part mates, because we will need it in the future when sending requests to sites or combining a data array. There are three ways to do this:


1. Adding " " Between Words

Kod:
"python" + " " + "öğreniyorum"
'python öğreniyorum'

decBVa.png


As you can see it left a character blank in between and separated words.


2. Putting A Space At The End Of The First Word

Kod:
"python " + "öğreniyorum"
'python öğreniyorum'

PdA1eA.png


As you can see, it wrote the same in this one character with a space.

3.Putting A Space Before The Second Word

Kod:
"python" + " öğreniyorum"
'python öğreniyorum'

NyTG28.png


I have shown you three ways you can do it this way, it will be very useful in the future.

If you want to write a word more than once, we will use the "*" (multiplication) sign. If you want to make more than one word or a sign, but don't want to deal with one by one, you can use this easy way.

To the Python command line
Kod:
"deneme" *5
'denemedenemedenemedenemedeneme'

Qd8beB.png


As a printout, it gave 5 adjacent 'deneme'.

If you want to write separately, put a space at the beginning or at the end, as we learned above.

Kod:
"deneme " * 5
'deneme deneme deneme deneme deneme '
Let's write and check out.

f6ydQI.png


As you can see, there are many ways to improve yourself. In strings, "+" (concatenation) is used as, "*" (multiplication). It combines the two characters you choose with addition, and in the multiplication feature, it writes the character you specify as many times as you want. The"-" (subtract) and "/" (divide) properties are not available in strings. They are available in integers, I'll tell you about them now.



Number Arrays

When I was in the strings section, I mentioned that there are many data types. Number arrays are one of them. There are also many properties for number arrays. Let's take a look at them.
Let's open the Python command line and type any number.
Kod:
>>>10
10

AWIAUG.png


As you can see, it gave our number directly as output. There are many types of numbers when we say numbers; integers, decimals, complex numbers ... Their representations are also different.
For example,
Kod:
>>>2.3
2.3
>>>type(2.3)
<class 'float'>

>>>(12+3j)
(12+3j)
>>>type(12+3j)
<class 'complex'>
We got the printouts. Here the number 2.3 is a float When we looked at the type, we already understood that this is a decimal number. (12+3j) is a complex number. When we look at the type, we can see that it is a complex number.

My52HL.png


Let's continue with examples of arrays of numbers. Let's try to do four operations, but first let's look at which operators mean what.

+ : Addition
- : Subtraction
* : Multiplication
/ : Division
// : Full Division
% : Finding the Remainder
** : Exponent

Let's make examples with these;
Kod:
>>>17+12
29
>>>15-5
10
>>>160/8
20.0
>>> 160//8
20
>>> 23%4
3
>>> 2**7
128
You can understand the operators with examples such as.

zx1z4K.png


So are numbers really numbers? So when we look at the type of the number 100;
Kod:
>>> type(100)
<class 'int'>
which means integer.

GC4LPA.png


But is "100" a number or a character?
Kod:
>>> type("100")
<class 'str'>
as you can see it is a string.

dH3dGy.png


You should pay attention to this distinction, otherwise you will encounter many errors when you start writing code. What if we enclose the numbers in double quotes as in strings and try to add them?

For excample
Kod:
>>> "10" + "23332"
'1023332'
We got a printout like this. It turned into a string because we enclose it in double quotes. Since the + sign in strings means concatenation, it also served as a concatenation here.

bOxSST.png


Can we multiple strings and strings of numbers?
E.g.:
Kod:
>>> "90" + 90
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    "90" + 90
TypeError: can only concatenate str (not "int") to str
We got an error. So you cannot do anything with an int (integer value) and str (string).

JVTJae.png


This part ends here. We'll learn variables in the next part so it will get more difficult :))





Source: https://www.turkhackteam.org/python/1884943-python-egitimi-part-2-p4rs.html
Translator: Dolyetyus

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