Support Online
Skip to main content

Python Data Types

In Python, data types are used to define the type of a variable.

In this article, we will list all the data types and examine how each one works.

If you're new to Python, don't forget to take a look at Python Tutorial for Beginners first.

Summary Notes

  • Replaceable etc. Mutable vs Immutable
    Lists and dictionaries can be modified at any time, but tuples and strings are not.
    For example, you can add new elements to a list, but you cannot change the letters of a string one by one.

  • It is important to choose the right data type
    You can't use the same tool for every job
    If you want to maintain order, lists are a good choice.
    If you want the values ​​to remain constant, you should use tuple, for fast membership checking, you should use set, and if you need a key-value relationship, you should use dict.

  • Memory management
    Python works with reference counting and garbage collection in the background.
    This means that if variables are intertwined (reference loop), it may consume unnecessary memory.
    In such cases, context manager or weak reference comes in handy.

  • Unicode support
    Python strings natively support Unicode, so you can type anything from Turkish characters to Japanese.
    But when importing data from outside, you need to specify the encoding setting clearly, otherwise you may get the "square instead of Chinese letter" error :)

  • Advanced types
    Types like memoryview and bytearray are lifesavers when working with large files.
    It uses less memory and prevents unnecessary copies.

  • When exact math is needed
    It is critical that there are no errors after the comma in financial calculations.
    So use module decimal; You won't get stuck with the small but annoying rounding errors of floating-point numbers.

Data Types in Python

There are many built-in data types in Python. Here are the most basic ones:

  • Numericint, float, complex
  • Text (String)str
  • Sequence Typeslist, tuple, range
  • Binarybytes, bytearray, memoryview
  • Mappingdict
  • Logical (Boolean)bool
  • Setset, frozenset

Python Numeric Data Type

Numeric data types in Python are used to hold different types of number values:

  • int → Holds integers. There is no length limit, you can store even very large numbers.
  • float → Holds decimal numbers. It gives accurate results up to approximately 15 digits.
  • complex → Used for complex numbers (e.g. 3+5j).

Note:

The long data type used in Python 2 is no longer available in Python 3. In current versions int already supports integers of unlimited length.

When defining a variable in Python, you do not need to specify a data type as in C or C++.
You can directly assign a value to a variable.

But let's say, "What data type does this variable currently hold?" you wondered.
Then you can use the type() function.

# Tam sayı (integer) değişken oluşturma
a = 100
print("Değer:", a, " -> Tipi:", type(a))

# Ondalıklı sayı (float) değişken oluşturma
b = 10.2345
print("Değer:", b, " -> Tipi:", type(b))

# Karmaşık sayı (complex) değişken oluşturma
c = 100 + 3j
print("Değer:", c, " -> Tipi:", type(c))
Değer: 100 -> Tipi: <class 'int'>
Değer: 10.2345 -> Tipi: <class 'float'>
Değer: (100+3j) -> Tipi: <class 'complex'>

Python String Data Type

String is a data type in which characters are stored sequentially.
Python allows us to use characters from different languages ​​without any problems, thanks to its Unicode support.

You can use single quotes ('...') or double quotes ("...") to create a string:

# Çift tırnakla string oluşturma
a = "çift ​​tırnak içindeki string"

# Tek tırnakla string oluşturma
b = 'tek ​​tırnak içindeki string'

# Ekrana yazdırma
print(a)
print(b)

# Virgül (,) ile string birleştirme
print(a, "ile birleştirilmiş", b)

# Artı (+) ile string birleştirme
print(a + " ile birleştirilmiş " + b)

The output is like this:

çift tırnak içinde bir string
tek tırnak içinde bir string
çift ​​tırnak içindeki string ile birleştirilmiş tek ​​tırnak içindeki string
çift ​​tırnak içindeki string ile birleştirilmiş tek ​​tırnak içindeki string

Python List Data Type

list (list) in Python is a very flexible data type.
It is similar to the array structure in C/C++, but there is an important difference:
Python lists can hold different types of data at the same time.

With its official definition; list in square brackets ([])
and an ordered group of data separated by commas (,).

# sadece tam sayı içeren liste
a = [1, 2, 3, 4, 5, 6]
print(a)

# sadece string içeren liste
b = ["hasan", "ahmet", "mehmet"]
print(b)

# hem tam sayı hem de string içeren liste
c = ["ali", "hamza", 1, 2, 3, "go"]
print(c)

# indeksler 0’dan başlar. Bu, listedeki tek bir öğeyi yazdırır
print(c[1]) # listede "hamza" ifadesini yazdırır

Python Tuple

Tuple is a data type similar to a list.
The difference is that it is immutable.
This means that the data in the tuple cannot be updated or deleted later.

Tuples are defined using parentheses () and commas ,.

Tuple Examples

# Sadece integer veri tutan tuple
a = (1, 2, 3, 4)
print(a) # tüm tuple'ı yazdırır

# Farklı tipte veriler tutan tuple
b = ("merhaba", 1, 2, 3, "go")
print(b) # tüm tuple'ı yazdırır

# Tuple indeksleri de 0'dan başlar
print(b[0]) # "merhaba" değerini verir

Python Dictionary

Dictionary is a structure that stores data in the form of key : value (key : value).
It is not ordered like a list or tuple, so it works unordered.

Dictionaries are written in curly brackets {} and allow you to access data very quickly.
In short, it's incredibly useful when searching through large data sets.

Dictionary Example

# Örnek bir dictionary değişkeni
a = {1: "ad", 2: "soyad", "yas": 33}

# Anahtara göre değer yazdırma
print(a[1]) # "ad"
print(a[2]) # "soyad"
print(a["yas"]) # 33

Result

In this article, we learned Python's basic data types: numbers, strings, lists, tuples and dictionaries.
Each of them has different usage areas.

In short, if you know how to use Python data types correctly, your code will be more understandable and your programs will run more efficiently.
Now you have a solid foundation to start your own projects in Python.