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:
-
int
— Integer values without a fractional part. -
float
— Floating point numbers (real numbers). -
complex
— Complex numbers (consisting of a real part and an imaginary part). -
str
— Strings, which are sequences of characters. -
bool
— Boolean data type, taking valuesTrue
orFalse
. -
None
— Special type representing the absence of a value. -
bytes
— Immutable sequence of bytes. -
bytearray
— Mutable sequence of bytes. -
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:
-
list
— A mutable ordered collection of elements. -
tuple
— An immutable ordered sequence of elements. -
range
— A sequence of numbers, commonly used in loops. -
set
— An unordered collection of unique elements. -
dict
— A collection of key-value pairs with unique keys. -
frozenset
— An immutable version ofset
.
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.
GO TO FULL VERSION