Lesson-5: Python Lists, Tuple and Dictionary Data Types

Python Lists, Tuple and Dictionary Data Types

In this lesson I wanted to devote a place to the data types of lists, tuple, dictionaries. These data types are also data types such as string, integer, float, boolean. But inside, as their name suggests, they contain multiple data. Now let’s examine these data types in sequence. Before starting the lesson, you can watch the video below…

Lists:

It’s one of the data types in Python. It can store multiple data in the form of a list. To create a list, first:

a = [“memet”,”ayşe”,”fatma”, “mert”, “ahmet”, “veysel”, 78, 99, 101]

in the form, we define the variable a as a list. the type of variable a is [list]. Lists can contain string and integer values, and as you can see, it is very easy to create.

with print(len (a)), we can print the number of elements of our list on the screen.

print (a[1]) –> first element in List a prints index. (Out: ayşe)

print (a[0:3]) –> starting from 0 to 3. it writes to the screen up to the index. Output (memet, ayşe, fatma) does not include 3ü.

print (a[1:3]) — > starting from 1 to 3. it writes to the screen up to the index.
Output (ayşe, fatma) does not include 3ü. Includes 1 i.

ADD AND SUBTRACT ELEMENTS TO THE LIST

To add a new element to our list and delete an existing element, we use the following codes.

a.append (“spring”) adds “Spring ” data to the end of the a list.

a.remove (“ayşe”) deletes ayşe data from a list

Tuple:

Tuples are very similar to lists. The difference between them is that tuples cannot be changed. Adding and subtracting cannot be done. So why use Tuple?

Because advanced programming is also very important in data speed. Data in the form of a list that is not changed is defined as Tuple, and bellet also works quickly because it takes up very little space. If you have a list that will never be changed, it will also be very useful to create it as a Tuple.

You can use the following structure to create a Tuple:

a=(“memet”, “ayşe”, “fatma”, “mert”, “ahmet”, ” veysel”)

As can be seen, the only difference from lists is that regular brackets are used instead of square brackets. We can’t add and subtract tuples. It continues as it was created when the Program was designed.

Dictionary:

Dictionaries also have an equivalent for each element. They have a structure like the Foreign Language Dictionaries we normally use. There are KEYS and VALUE values. You can use the following structure to create a dictionary:

sozluk = {“apple”:”elma” , “car”:”araba” , “train”: “tren”}

Here” apple “is the KEYS, and the VALUE of it is “elma“. To print a keys in value on the screen:

print (sozluk[“apple”]) –> screen output: “elma”

To add a value to the dictionary:
sozluk[“pencil”] = “kalem”

sozluk.clear () —-> deletes all contents of the dictionary
sozluk.pop (“apple”) ——> deletes” apple”

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *