CodeGym /Courses /Python SELF EN /Data Types in Python

Data Types in Python

Python SELF EN
Level 5 , Lesson 1
Available

1.1 Built-in Types

Python offers a variety of built-in data types that allow you to solve a wide range of tasks in programming. We've already touched on some of them; now let's go into them a little more. Here's a quick rundown of the main built-in data types:

  1. int — Integer values without a fractional part.
  2. float — Floating point numbers (real numbers).
  3. complex — Complex numbers (consisting of a real part and an imaginary part).
  4. str — Strings, which are sequences of characters.
  5. bool — Boolean data type, taking values True or False.
  6. None — Special type representing the absence of a value.
  7. bytes — Immutable sequence of bytes.
  8. bytearray — Mutable sequence of bytes.
  9. memoryview — An object that allows you to work with data in binary form without copying it.

The table below shows these data types and their characteristics:

Data Type Description Example Use
int Whole numbers x = 10
float Floating point numbers y = 3.14
complex Complex numbers z = 1 + 2j
str Strings s = "Hello, world!"
bool Boolean type is_valid = True
None Absence of a value result = None
bytes Immutable sequence of bytes b = bytes([50, 100, 76])
bytearray Mutable sequence of bytes ba = bytearray([50, 100])
memoryview Representation of data in memory mv = memoryview(b'abc')

Using these data types allows you to implement any logic in Python, from basic arithmetic operations and working with text to complex binary data processing and memory management.

1.2 Collections

Besides storing data, Python lets you store whole groups of objects. These are called collections. Collections are different data structures that allow you to store, manage, and process groups of elements. Here are the main types of collections in Python:

  1. list — A mutable ordered collection of elements.
  2. tuple — An immutable ordered sequence of elements.
  3. range — A sequence of numbers, commonly used in loops.
  4. set — An unordered collection of unique elements.
  5. dict — A collection of key-value pairs with unique keys.
  6. frozenset — An immutable version of set.

The table below shows these collections and their characteristics:

Collection Type Description Example
list Mutable, elements can repeat my_list = [1, 2, 3]
tuple Immutable, elements can repeat my_tuple = (1, 2, 3)
range Immutable sequence of numbers my_range = range(1, 10)
set Unordered collection of unique elements my_set = {1, 2, 3}
dict Key-value pairs, unique keys my_dict = {'a': 1, 'b': 2}
frozenset Immutable collection of unique elements my_frozenset = frozenset([1, 2, 3])

Each of these collections has its own unique properties and methods, making them suitable for different tasks in programming, from simple data storage to more complex data processing and management. We'll cover them in more detail in future lectures.

1.3 Classes and Objects

Built-in types are great, but at some point, they might not be enough. That's when Python lets you declare your own types — classes.

Classes in Python provide a way to bundle related functions and data. They also let you model real or abstract objects with specific behaviors and properties.

For example, imagine you need to describe a "Smartphone" class. This class could contain data about the model, manufacturer, memory size, and operating system. It could also have functions (methods) describing the behavior of the smartphone, like powering on and off, installing apps, and so on.

A class is created using the class keyword. This statement creates a new type of object and allows the new class to inherit attributes and methods from another class.

First, we'll learn to use existing classes and create their objects, and when the time comes, we'll create our own.

What you might find useful to know — classes have their own built-in functions (also called class methods), and these functions are called like this:


object.function(parameters)

Remember when we used the format() function on a string? That's exactly the kind of thing we're talking about. We'll dive deeper into functions, classes, and objects in the upcoming lectures.

2
Task
Python SELF EN, level 5, lesson 1
Locked
Strength in Diversity
Strength in Diversity
2
Task
Python SELF EN, level 5, lesson 1
Locked
Collector
Collector
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 36, San Diego, United States
2 June 2025
good summary of what a Class is relative to data types