Skip to content

Data Types

Amin Zamani edited this page May 24, 2023 · 4 revisions

Built-in Data Types

In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories:

  • Text Type: str
  • Numeric Types: int, float, complex
  • Sequence Types: list, tuple, range , str
  • Mapping Type: dict
  • Set Types: set, frozenset
  • Boolean Type: bool
  • Binary Types: bytes, bytearray, memoryview
  • None Type: NoneType

Getting the Data Type

You can get the data type of any object by using the type() function: Print the data type of the variable x:

x = 5
print(type(x))

Setting the Data Type

In Python, the data type is set when you assign a value to a variable:

x00 = "Hello World"  # str
x01 = 20  # int
x02 = 20.5  # float
x03 = 1j  # complex
x04 = ["apple", "banana", "cherry"]  # list
x05 = ("apple", "banana", "cherry")  # tuple
x06 = range(6)  # range
x07 = {"name": "John", "age": 36}  # dict
x08 = {"apple", "banana", "cherry"}  # set
x09 = frozenset({"apple", "banana", "cherry"})  # frozenset
x10 = True  # bool
x11 = b"Hello"  # bytes
x12 = bytearray(5)  # bytearray
x13 = memoryview(bytes(5))  # memoryview
x14 = None  # NoneType

Setting the Specific Data Type

If you want to specify the data type, you can use the following constructor functions:

x15 = str("Hello World")  # str
x16 = int(20)  # int
x17 = float(20.5)  # float
x18 = complex(1j)  # complex
x19 = list(("apple", "banana", "cherry"))  # list
x20 = tuple(("apple", "banana", "cherry"))  # tuple
x21 = range(6)  # range
x22 = dict(name="John", age=36)  # dict
x23 = set(("apple", "banana", "cherry"))  # set
x24 = frozenset(("apple", "banana", "cherry"))  # frozenset
x25 = bool(5)  # bool
x26 = bytes(5)  # bytes
x27 = bytearray(5)  # bytearray
x28 = memoryview(bytes(5))  # memoryview

Python Collections (Arrays)

There are four collection data types in the Python programming language:

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
  • Dictionary is a collection which is ordered** and changeable. No duplicate members.

When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.

Sequence

In Python, a sequence is an ordered collection of elements, where each element is identified by an index. Sequences are a fundamental data type in Python and provide a way to store and access multiple values.

There are several built-in sequence types in Python, including:

  1. Strings: A sequence of characters, such as "Hello" or 'World'. String values are immutable, meaning they cannot be changed once created.

  2. Lists: A sequence of elements enclosed in square brackets [ ], such as [1, 2, 3] or ['apple', 'banana', 'orange']. Lists are mutable, allowing you to modify, add, or remove elements.

  3. Tuples: A sequence of elements enclosed in parentheses ( ), such as (1, 2, 3) or ('red', 'green', 'blue'). Tuples are immutable like strings.

  4. Range: A sequence of numbers generated using the range() function, such as range(0, 10) or range(5). The range represents an immutable sequence of numbers within a given range.

  5. Bytes and Bytearrays: Sequences of integers representing binary data. Bytes are immutable, while bytearrays are mutable.

Sequences in Python can be indexed and sliced. Indexing allows you to access individual elements by their position, and slicing allows you to extract a portion of the sequence.

For example, you can access elements in a sequence using square brackets, like sequence[index], where sequence is the name of the sequence and index is the position of the element you want to access.

my_string = "Hello, World!"
print(my_string[0])  # Output: 'H'

my_list = [1, 2, 3, 4, 5]
print(my_list[2])  # Output: 3

my_tuple = ('red', 'green', 'blue')
print(my_tuple[1])  # Output: 'green'

Sequences provide a way to work with collections of data in Python, and each sequence type has its own characteristics and use cases.

Immutable Data types

Boolean Int Float Strings Tuples Complex Frozenset

Mutable Data types

List Dict Set

Iterable Objects

آبجکت های پیمایش پذیر در پایتون

این پیمایشکردن با استفاده از حلقه ها صورت میگیره

Lists Tuples Dictionaries Strings Sets

Python

Python Essentials 1 (PCEP)

Introduction to Python and computer programming

Data types, variables, basic I/O operations, and basic operators

Boolean values, conditional execution, loops, lists and list processing, logical and bitwise operations

Clean Code

Algorithms

Django

Django Rest Framework

API

pip

SQLAlchemy

FastAPI

Pytest

TDD

Git

Linux

Docker

Python Testing

Interview Questions

Clone this wiki locally