HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>The data we work with in programs have essential attributes. In Python, they're built right into the language. Data also have methods - functions within properties. Properties and methods are expressions, just like variables or function calls. We can combine all of these in different ways. We cover these topics in more detail in separate courses that focus on the object-oriented features of Python. We will look at the basics of them in this lesson.</p>
1 <p>The data we work with in programs have essential attributes. In Python, they're built right into the language. Data also have methods - functions within properties. Properties and methods are expressions, just like variables or function calls. We can combine all of these in different ways. We cover these topics in more detail in separate courses that focus on the object-oriented features of Python. We will look at the basics of them in this lesson.</p>
2 <h2>Objects</h2>
2 <h2>Objects</h2>
3 <p>In programming, we operate with data, create numbers and strings, perform various operations on them, and use their results. We apply either operators or functions to operate:</p>
3 <p>In programming, we operate with data, create numbers and strings, perform various operations on them, and use their results. We apply either operators or functions to operate:</p>
4 <p>In the example above, there's a clear division: we separated the data and functions from each other. But this isn't the only way to organize code. In Python, there's another approach used alongside this separation, the<strong>Object Oriented approach</strong>.</p>
4 <p>In the example above, there's a clear division: we separated the data and functions from each other. But this isn't the only way to organize code. In Python, there's another approach used alongside this separation, the<strong>Object Oriented approach</strong>.</p>
5 <p>Object-oriented code combines data and functions into one entity - an<strong>object</strong>. In this case, data is<strong>attributes</strong>, and functions are<strong>methods</strong>.</p>
5 <p>Object-oriented code combines data and functions into one entity - an<strong>object</strong>. In this case, data is<strong>attributes</strong>, and functions are<strong>methods</strong>.</p>
6 <p>It is what it looks like:</p>
6 <p>It is what it looks like:</p>
7 <p>Python strings are objects. In the example above, we are calling a method. It is a function that is associated with a string. We made the call using a period right after the variable name. Other than that, methods work like normal functions.</p>
7 <p>Python strings are objects. In the example above, we are calling a method. It is a function that is associated with a string. We made the call using a period right after the variable name. Other than that, methods work like normal functions.</p>
8 <p>Also, we can make the call directly:</p>
8 <p>Also, we can make the call directly:</p>
9 <p>There are many methods built into strings, which developers use all the time. See the<a>documentation</a>for a list of them. Here are some examples:</p>
9 <p>There are many methods built into strings, which developers use all the time. See the<a>documentation</a>for a list of them. Here are some examples:</p>
10 <p>The same goes for numbers and other data types we have not looked at. You could say that in Python, almost everything is an object:</p>
10 <p>The same goes for numbers and other data types we have not looked at. You could say that in Python, almost everything is an object:</p>
11 <p>We see in the example above a method name with two underscores at the beginning and end. In Python, this is the name given to methods we do not usually call directly. Functions have been created for them that call methods themselves:</p>
11 <p>We see in the example above a method name with two underscores at the beginning and end. In Python, this is the name given to methods we do not usually call directly. Functions have been created for them that call methods themselves:</p>
12 <p>The creator of Python<a>decided</a>that it would be more clear to express mathematical or mathematical-like operations in functions. He wanted these functions to resemble operations such as addition or subtraction. It is more familiar to those who have studied mathematics.</p>
12 <p>The creator of Python<a>decided</a>that it would be more clear to express mathematical or mathematical-like operations in functions. He wanted these functions to resemble operations such as addition or subtraction. It is more familiar to those who have studied mathematics.</p>
13 <p>This is also how the len() function works:</p>
13 <p>This is also how the len() function works:</p>
14 <p>In addition to methods, objects have attributes, but Python's built-in objects don't have many of them. For example, the __doc__ attribute, which returns the function documentation. Therefore, functions are also considered objects:</p>
14 <p>In addition to methods, objects have attributes, but Python's built-in objects don't have many of them. For example, the __doc__ attribute, which returns the function documentation. Therefore, functions are also considered objects:</p>
15 <p>Attributes work and look like variables, only specified with a dot after the object.</p>
15 <p>Attributes work and look like variables, only specified with a dot after the object.</p>
16 <p>Now let's talk about the immutability of data types.</p>
16 <p>Now let's talk about the immutability of data types.</p>
17 <h2>Immutability</h2>
17 <h2>Immutability</h2>
18 <p>Imagine we have this call:</p>
18 <p>Imagine we have this call:</p>
19 <p>When you call the .upper() method, it returns a new value with all letters converted to the uppercase, but it doesn't change the original string. So inside the variable will be the old value: 'Tirion'. This logic holds for methods of all primitive types.</p>
19 <p>When you call the .upper() method, it returns a new value with all letters converted to the uppercase, but it doesn't change the original string. So inside the variable will be the old value: 'Tirion'. This logic holds for methods of all primitive types.</p>
20 <p>Instead of changing the value, you can<strong>replace</strong>it. It requires variables:</p>
20 <p>Instead of changing the value, you can<strong>replace</strong>it. It requires variables:</p>
21 <p>Next, let us talk about methods.</p>
21 <p>Next, let us talk about methods.</p>
22 <h2>Methods as expressions</h2>
22 <h2>Methods as expressions</h2>
23 <p>Methods are expressions, like variables or function calls. It means we can combine them in different ways.</p>
23 <p>Methods are expressions, like variables or function calls. It means we can combine them in different ways.</p>
24 <p>For example, you can use them in operations:</p>
24 <p>For example, you can use them in operations:</p>
25 <p>Or in function arguments:</p>
25 <p>Or in function arguments:</p>
26  
26