Python for Beginners: Working with Variables
If you are confident programming in Python, feel free to skip this post. But if you don’t even have its interpreter installed yet or you don’t know what it is but want to find out, the text will greatly help you.
Python is one of the most popular, in demand and at the same time easy to learn languages, and in order to start working with it, there is no need to pay for some obscenely expensive courses. With the right guide, you can do this yourself, and the right guide for Python beginners has a very specific name. This is a book “Python Programming for Beginners” experienced programmer and textbook author Mike McGrath.
The text of this book is intended for those who are not yet familiar with Python. The author explains the features of the language, tells what it is based on, why and how it appeared, helps with installation, runs through the simplest arithmetic functions: the entry into training is surprisingly easy, and if you were afraid to apply for coding, then this book will be able, among other things, to overcome such fear.
Let's see how, for example, working with variables in Python is explained.
In programming, a variable is some container in the computer's memory where data is stored. Once the data is saved, it can be recalled using the name of this variable. The programmer can choose any name for the variable, with the exception of Python language keywords. It's best to choose meaningful names for your variables that reflect their content.
![](https://habrastorage.org/getpro/habr/upload_files/37c/845/0d0/37c8450d0ce726486e97680e756c902b.png)
In Python programs, data that needs to be stored in variables is entered using the assignment operator =, for example, to store the numeric value 8 in a variable named a, you would write:
a = 8
You can then access the variable's stored value using its name. Thus, the statement print( a ) will print the stored value 8. Variables can be successively assigned different values, and therefore the variable is capable of taking on different values as the program runs – it is not a coincidence that it is named that way.
In Python, a variable must be assigned an initial value (initialized) in the statement that declares the variable in the program. Otherwise, the interpreter will generate a not defined error message. It is possible to initialize multiple variables to the same value in a single statement. This can be done using the assignment operator =. For example, to initialize variables a, b, and c and assign them the value 8, we would write:
a = b = c = 8
Conversely, multiple variables can be initialized with different values and written all in one statement, using a comma as a delimiter. For example, to initialize the variables a, b and c with the numeric values 1, 2, 3, we use the notation:
a, b, c = 1, 2, 3
Some programming languages, such as Java, require variable types to be specified when declaring them. This reserves a certain amount of memory. This is known as static typing. Variables in Python are not subject to this restriction; memory is allocated according to the values assigned to the variables (dynamic typing). This means that a variable can contain integers, floating-point numbers, text strings, or logical values.
![](https://habrastorage.org/getpro/habr/upload_files/a1d/9d4/bbf/a1d9d4bbf6a76416b0a6319b128067e7.png)
You can add comments to your Python programs to describe instructions or sections of code. The # symbol is used for this. Anything after this character until the end of the line is ignored by the Python interpreter. Comments are very useful – they help make your code clear to others, as well as to yourself when you come back to it later.
![](https://habrastorage.org/getpro/habr/upload_files/178/8dc/2eb/1788dc2ebc81490288ef731622c94e83.png)
1. Launch a text editor, declare and initialize a variable, then print the value stored in it.
# Initialize the variable with an integer value
var = 8
print(var)
2. Then assign the new value to the variable and display it on the screen.
# Assign a floating point value to the variable
var = 3.142
print(var)
3. Now we assign another value and display it again.
# Assign a string value to the variable
var=”Python in easy steps”
print(var)
4. Finally, we assign another value and display the result again.
# Assign a boolean value to the variable
var = True
print(var)
5. Save the file in your working directory, then open a command prompt from that directory and run the program to see its output.
![](https://habrastorage.org/getpro/habr/upload_files/8ac/032/d9a/8ac032d9ac9f6abb99c2ce132065ee49.png)
As you can see, this chapter explains everything step by step, in stages, and repeating the instructions from the Python tutorial will hardly be an impossible task for anyone. This is a good and accessible in all respects (both in terms of learning and in terms of financial and time costs) base for anyone who wants to master a popular programming language and, in the future, perhaps connect their career with it.