Previously, we could learn Python's basic syntax. If you haven't gone through it yet, follow the link below. 👇👇👇
Now let's get back again to the sample PyCharm Python script. I will strip it from any comments, leaving only pure interpretable code.
def print_hi(name):
print(f'Hi, {name}')
if __name__ == '__main__':
print_hi('PyCharm')
The script upon execution will print the following message.
❯ python3 main.py
Hi, PyCharm
Imagine now, we want to print the message three times. What could we do? Well, the easiest way would be to triple the print(...) function call. Let's try it.
def print_hi(name):
print(f'Hi, {name}')
print(f'Hi, {name}')
print(f'Hi, {name}')
Now, when we run it, we will see the following output.
❯ python3 main.py
Hi, PyCharm
Hi, PyCharm
Hi, PyCharm
So good so far. But you could definitely notice that we have copied and pasted the same code three times.
💡 Copy-and-paste is sometimes called an antipattern as it tends to duplicate code rather than using proper abstraction or refactoring techniques. It can lead to various issues, such as maintenance difficulties, code duplication, and an increased likelihood of bugs. Instead of copying and pasting code, developers should strive to create reusable components and follow best practices to improve code quality and maintainability.
And if we wanted to print the message a million times, would we copy&paste it a million times? That'd really be very cumbersome so definitely there must be a better way.
Variables
We can introduce a small improvement for now. Instead of just copying and pasting code, we can use a variable.
In Python, a variable is a symbolic name or identifier that holds a reference to a value stored in memory. Unlike some other programming languages, Python is dynamically typed, meaning you don’t need to explicitly declare the variable type. Instead, Python infers the data type based on the value assigned to the variable. For instance, you can create a variable like this:
>>> greetings = 'Hi, PyCharm'
In this case, the variable greetings
is assigned the string value Hi, PyCharm
. We can check the type of the variable, for that purpose we will use Python's built-in type(...) function.
>>> type(greetings)
<class 'str'>
Python will automatically determine that greetings
is of type str
. Later in the code, we can reassign the variable to a different value of any data type:
>>> greetings = 345
>>> type(greetings)
<class 'int'>
Now, greetings
holds the integer value 345, and its type has changed to int
.
Variables in Python can hold a variety of data types, including integers, strings, lists, and more. Python variables are flexible and can change their type throughout the execution of the program, depending on the value assigned to them.
Now we can improve the print_hi(...) function a bit and use a variable.
def print_hi(name):
greetings = f'Hi, {name}'
print(greetings)
print(greetings)
print(greetings)
References
As it has been already said, a variable is a symbolic name or identifier that holds a reference to a value stored in memory. Let's spare a moment at the previous simple example.
>>> greetings = 'Hi, PyCharm'
While it's just a simple assignment, it's worth understanding what happens underneath.
Firstly, Python allocates memory for an str
object. Once it's placed in memory, it has an address. Then the address is assigned to a greetings
variable, so we can use it to reference the text later. Once it is assigned, the reference counter is incremented to track the number of variables pointing to this particular str
object. If no variable references the object, hence counter is equal to 0, memory can be freed.
Again, to get the type of an object we can use the type(...) function.
>>> type(greetings)
<class 'str'>
To obtain the address of the object, we can use the id(...) function.
>>> id(greetings)
4699850608
To get the reference counter, it's more complicated. First, we have to import sys
module, and then we can use getrefcount(...) function from that module.
>>> import sys
>>> sys.getrefcount(greetings)
2
We can see that object is referenced twice, once by a greetings
variable and once by the getrefcount(...) function. Let's create another variable named hello
and assign greetings
to it.
>>> hello=greetings
>>> sys.getrefcount(hello)
3
We can easily notice that the reference counter has been increased and now the object is referenced three times.
Hope you've found it useful, cheers!