0 characters
6. Immersion in Python
Summary- The article presents the syntax of the Python language using the example of creating a simple alarm clock.
- The example discusses built-in data structures such as lists, and the use of dot notation to access attributes of a time object.
- The conditional construction
if/elif/else
is used to check whether the minutes value is in the list and execute a block of code depending on the condition.- In Python, variables begin to exist at the first mention in the code, and their type is not important, since they adapt to any data type.
- The article briefly presents the basics of Python syntax, including modules, methods and attributes, as well as features of working with variables and indentation to highlight code blocks.
The first section might seem too boring and uninformative, and the example too banal. So now let's start with a more complex example that illustrates the syntax of the Python language and its simplicity.
Let's write a simple alarm clock, if that's what you can call it, which will check the time and, if the minutes match the values we have determined, then output "The signal at such and such a time", otherwise "It's not time yet". Below is the code of our program.
from datetime import datetime
minute_alarm_clock = [0, 15, 30, 45]
alarm_clock = datetime.today().minute
if alarm_clock in minute_alarm_clock:
print("The signal is at ", alarm_clock, " minutes.")
else:
print("It's not time yet, it's", alarm_clock, " minutes now.")
Now let's analyze each line. The first line imports a certain functionality, which we will use later. In this case, we took only one "submodule" of the datetime
library. It just so happens that it matches the name of the library (this does not always happen).
The Python standard library contains a lot of useful code designed for reuse. This is done so that programmers do not invent their own bike, but use a ready-made one that is efficient and thoroughly tested. So far, there is enough on this issue, we have repeatedly addressed it back to, and now let's move on to the next line.
minute_alarm_clock = [0, 15, 30, 45]
The Python language has built-in data structures. Now let's consider only one of them: list. This is the same array, but unlike C++, the elements are enclosed in square brackets. In the example, we have listed 4 elements that indicate the minutes when the alarm should go off. We have assigned this list to the minute_alarm_block
variable.
I think we need to digress and tell you more about the variables. In the same C++ or Java, before using a variable, you must first declare it, that is, explicitly specify what we will store in it: a number, a string, a Boolean value, etc. But in Python, you don't have to think about it. Variables begin to exist the first time they are mentioned in the code, and the type of values assigned to them is not important, since they will adjust to any one. Our variable is assigned a list of minute_alarm_block
, so it becomes a list itself. Now consider the following line.
alarm_clock = datetime.today().minute
There is a variable in it too. Here we will focus on the right side of the expression. To make it easier to understand, let's rewrite the string by entering an additional variable.
time_now = datetime.today()
alarm_clock = time_now.minute
Here we call the method today
, which is part of the datetime
submodule (i.e. inside the datetime
module, there is a datetime
submodule, inside which there is a method today
). The fact that the today
method is indicated by the parentheses after it. So, now in the variable time_now
stores a time object (this object consists of parts). If you display it on the screen, you will see the exact time and date. But we only need minutes, so we move on to the next line:
alarm_clock = time_now.minute
For those who are familiar with other programming languages, the syntax of dot notation is familiar. Let me explain: to refer to the minutes stored in the time_now
object, you just need to write minute
through a dot. In order to refer to the clock, the minute
attribute should be replaced with hour
. And to find out what attributes the method contains in general today
, you need to refer to the documentation. Next line:
if alarm_clock in minute_alarm_clock:
We have a list of numbers, and there is a value of minutes. Now, to check for the presence of such a minute value in the list, we will use the if
construction. Its essence is to check the condition and, if it is true, execute the block. Unlike C++, Python has the in operator, which will help us a lot in this case. In the example, we check whether the minutes value is contained in the list we have defined.
The next difference between Python and other programming languages is how blocks are allocated. After if
the condition is specified, then a colon (:
) is placed, after which the block begins, which will be executed if the condition is true. And that's not all. The block is visually indented, as in the example. If there is no indentation, the code will not work. In C++, you can not indent or wrap lines, since blocks are separated by curly brackets, and expressions are separated by semicolons.
The else
keyword is a continuation of the if
construction and is executed in cases where the condition in if
is false. Let's take a closer look at the conditional if
statement in further.
We can further complicate the example. If the alarm did not work, then we check whether the minutes are even or odd now:
from datetime import datetime
minute_alarm_clock = [0, 15, 30, 45]
minute_alarm_clock_odd = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59]
time_now = datetime.today()
alarm_clock = time_now.minute
if (alarm_clock in minute_alarm_clock):
print("The signal is at ", alarm_clock, " minutes.")
elif (alarm_clock in minute_alarm_clock_odd):
print("It's odd minutes now: ", alarm_clock, " minutes.")
else:
print("It's even minutes now: ", alarm_clock, " minutes.")
To do this, add another keyword: elif
. It is a kind of hybrid between if
and else
. This design works as follows: if it is not the time of the alarm, then we check whether the value of minutes is in the list of odd minutes, and if so, we output «It's odd minutes now». If all conditions are false, then print «It's even minutes now».
That's all. We briefly got acquainted with the Python syntax. We learned what modules, methods, and attributes are. We got acquainted with how variables work and why they are needed. We learned about the first data structure – lists. We applied the conditional construction if/elif/else
.
Let me remind you that in Python, the values of variables are assigned dynamically, expressions are separated not by a semicolon, as in many languages, but by a line break. In turn, the blocks are marked with mandatory indents.
In the next lesson we will look in detail at the types and data model in Python, as well as how the assignment operation (=
) works.
Test task
Subscribe to our Telegram channel!
News, useful material,
programming and InfoSec