0 characters
3. Variables and comments in Python
Summary- A variable in Python is a named memory area for storing data.
- The name of the variable must begin with a letter or an underscore and not match the keywords of the language.
- Variables can store data of different types and sizes.
- Rules for using variables: assigning data to the right of a variable, the variable name begins with a letter or an underscore, using the
print()
function.- Comments in Python are used for explanations and can be single-line or multi-line.
- Codebra site examples often use comments to simplify the code and exclude the execution of certain code blocks.
You probably installed Python on your computer and ran the first program. Now it's time to learn what variables are and how to use comments.
Variables in Python
A variable is a named memory area that can be used to store data. Simply put, a variable is a container for data. The data stored in a variable is called a value. The name of a variable in Python must begin with a letter character or an underscore (_
) and must not match reserved (keywords). In fact, there are not so many keywords, and in the process of learning Python, you will remember them.
It's hard to imagine programming without variables. They can store data of different types (you will learn about data types a little later) and sizes.
A few simple rules for using variables in Python:
- when assigning a value to a variable, the data must be to the right of the variable;
- the variable name can start with a letter character or an underscore;
- to print a variable using the
print()
function, it must be written without quotes inside parentheses.
For example, we have the number of pages in a textbook and we need to make some calculations with this number:
number_of_pages = 120 # assigning a value to a variable
number_of_pages = number_of_pages / 2 # divide the value of the variable into two
print(number_of_pages) # => 60
In this example, we divide the number of pages in the textbook by two. Here we use the stored number in the number_of_pages
variable for just one calculation. But if there are dozens or hundreds of such calculations, then the need for variables becomes obvious.
Try to come up with variable names according to the data stored in it. Also use the underscore character (_
) to separate the words in the variable name:
numberofpages
number_of_pages # is that better, or not?
By the way, in Python you can initialize several variables at once:
a, b, c = 1, 'Hello', [1, 2, 3]
Let's go back to the variables again in the lesson about data types.
Comments in Python
You have already seen the lattice symbol (#
) in the examples. When writing code, it is often necessary to leave explanatory notes, i.e. comments. Python has a one-line comment that starts with a lattice character (#
):
# Comment
a = 2
b = 1 # variable b
As you can see, the comment can be placed on a separate line or on the same line with the code – it doesn't matter. It is allowed to specify any information in the comment, since the Python interpreter ignores it.
To create a multiline comment, you can use the following construction:
'''
a comment
on
several lines
'''
a = 2
Или:
""" a comment
on
several lines """
a = 2
That is, to create a multiline comment in Python, you need to use a pair of three consecutive single or double quotes.
Comments are often used in programming to exclude the execution of a single line or block of code for testing or debugging purposes.
Features of comments in the Codebra site examples
The Codebra site examples often use comments, for example, in order not to clutter up the code, the print()
function is omitted. That is:
a = 2
print(a) # => 2
The =>
symbol in the comment means that the number two will be output to the console. The following example is equivalent to it.
a = 2
a # => 2
If you decide to run it on your computer, nothing will be output to the console, since there is no print()
function. That is, you need to add the print()
functions to the example, where a sequence of characters =>
occurs in the comment.
Also, sometimes in the comments there may be a sequence of characters <=
, meaning that the user enters some data into the console. The function is equivalent to it input()
.
In this lesson, you got to know variables and comments better and learned about the features in the Codebra site examples.
In the next lesson let's summarize the section and then let's continue to dive into Python.
Test task
Subscribe to our Telegram channel!
News, useful material,
programming and InfoSec