Table of Contents:
- What is a variable in Python
- How to name variables
- How to declare and assign a value to a variable in Python
- How variable initialization works
- Multiple assignment of values
- Importing variables
- Deleting variables
- Variable types in Python
- Checking if a variable exists
- Maximum possible value variable
- Printing single and multiple variables
- Results

Course with employment: "The Python Developer Profession"
Learn moreWhen you open any Python program, you'll likely encounter a variety of equalities. These equalities are the foundation for performing calculations and manipulating data in Python. They allow you to create logical expressions that serve to define conditions and control the flow of program execution. Understanding these equalities is important for effective programming, as they help developers create more complex algorithms and solutions. It is important to master the syntax and features of working with equalities in order to use them correctly in your Python projects.
Variables are an important element of programming, used to store data in computer memory and perform various operations with it. In this text, we will look at how programmers work with variables, their purpose and application in code. Understanding how variables work is fundamental to software development and can significantly simplify the process of data processing.
What is a variable in Python
A variable is a location in computer memory that has a unique name. It consists of three main components: the variable name, the data type, and the value. The variable name serves as an identification, the data type determines what kind of information can be stored, and the value is the specific data that is stored in this memory location. Using variables correctly allows you to effectively manage data in programming and makes code easier to write and understand.
- A name, or identifier, is a name given by the programmer to refer to a variable. In the examples above, this would be x, name, and coin_flipping_result.
- A value is information stored in the computer's memory that the program operates on. In the examples above, this would be 4, ‘Victoria’, and [‘heads’, ‘tails’, ‘tails’]. They always belong to some data type.
- An address is the number of the memory cell where the variable's value is stored.
To visualize how computer memory works, imagine a huge industrial warehouse filled with racks and boxes. The warehouse symbolizes the computer's overall memory. For example, on the third rack, on the second shelf, in the sixth cell, there is a certain box. If you place any object in this box and attach a label with its name, this box becomes a variable. The contents of the box will be its value, and its location—the third rack, the second shelf, and the sixth cell—will be the variable's address. The label on the box represents the variable's name. This analogy helps us better understand how memory works in programming and how data is accessed.

How to Name Variables
When creating variables in Python, programmers follow certain naming conventions. Proper variable naming is important because it promotes code clarity and maintainability. Variable names should be concise yet descriptive, reflecting the essence of the stored value. It's also important to note that variable names can contain letters, numbers, and underscores, but cannot begin with a number. Lowercase letters are preferred, but capital letters are typically used to denote constants. Also, avoid using Python reserved words, as this can lead to errors in your code. By following these rules, programmers can create clear and understandable variable names that make code easier to read and understand.
- the first character must be an uppercase or lowercase Latin letter or an underscore _;
- the remaining characters can be uppercase or lowercase Latin letters, underscores, and numbers;
- spaces cannot be used;
- the variable name must not coincide with any of the reserved keywords in Python.
Let's look at some examples.
- What names can be given to variables: x, X, xyz, _x_y_z, XYZ, xyz_123, _123, x1Y2z2.
- What names cannot be given to variables: 1, 1x, x y z, x&y.
Python is Python is a case-sensitive programming language. This means that variables such as name and Name are treated as different. It is important to keep this in mind when writing code to avoid errors and misunderstandings. Proper use of case in variable names helps keep your code clean and clear, which is especially important when working in a team or when scaling projects.
While naming variables after standard library functions is possible, it is not recommended. This approach will restrict access to the corresponding functions, which can lead to confusion and errors in the code. It is better to use unique and descriptive names for variables to maintain the readability and functionality of the program.
We will discuss the reasons for this phenomenon in detail soon.
Python syntax supports a variety of variable naming styles, allowing developers to choose the most appropriate approach for their needs.
Popular styles include snake_case, where words are separated by underscores, and camelCase, where each new word begins with a capital letter. There's also PascalCase, which is similar to camelCase, but the first letter of the word is also capitalized.
Using a consistent variable naming style in Python not only improves the readability of your code but also makes it more maintainable. It's important to follow consistent standards when working in a team, as this avoids confusion and facilitates code sharing.
When choosing a naming style, it is worth considering the specifics of the project and the preferences of the team, which ultimately contributes to the creation of clean and understandable code.
- lowercase - all letters are lowercase;
- lower_case_with_underscores - all letters are lowercase, underscores between words;
- UPPERCASE - all letters are uppercase;
- UPPER_CASE_WITH_UNDERSCORES - all letters are uppercase, underscores between words;
- CamelCase - each word begins with a capital letter;
- mixedCase - each word except the first begins with a capital letter;
- Camel_Case_With_Underscores - each word begins with a capital letter, underscores between words (this is ugly, never do this).
Among Python developers, there is a standard called PEP 8, which is a guide Code Style Guidelines. This guide provides writing style recommendations to improve the readability and maintainability of your code. The Python community also establishes additional rules regarding variable naming, which contribute to clearer and more structured code. Following PEP 8 helps developers maintain a consistent style and facilitates collaboration on projects.
- use lowercase, breaking words with underscores if necessary to improve readability;
- if the finished project predominantly uses mixedCase, then it is recommended to write in this style to maintain backward compatibility;
- avoid naming l (lowercase L), O (uppercase o), and I (uppercase i) — in some fonts, they are indistinguishable from zero and one.
Following PEP 8 recommendations will help make your code clearer and easier to maintain. These coding style standards ensure consistency, making it easier for other developers to read and understand. Proper formatting, indentation, and variable naming according to PEP 8 contribute to improved code quality and facilitated team collaboration. Following these principles will not only increase your productivity but also reduce the likelihood of errors, which ultimately leads to higher-quality software. Understanding and applying PEP 8 standards is an important step toward professional development as a developer.
How to Declare a Variable and Assign It a Value in Python
Python is a dynamically typed programming language. This means that variables are assigned values at runtime, not at compile time. Dynamic typing allows developers to work with data more flexibly, simplifying the process of writing and debugging code. Python is widely used in various fields, including web development, data analysis, and machine learning, due to its simplicity and readability.
In Python, there is no need to separately declare variables; they can be immediately initialized by assigning a value. For this, the equal sign = is used. This approach simplifies the code and makes it more readable, allowing developers to focus on the logic of the program rather than declaring variables.
Furthermore, the type of the value assigned to it can be changed later.
In statically typed languages such as C++ and Java, this approach will not work: the compiler will generate an error, since the variable is initially defined as an integer and cannot be converted to a string. In contrast, Python allows you to change the type of a variable, since in this language, variables refer to memory addresses, not the data itself. This property makes Python more flexible, but requires developers to be careful when working with data types.
How Variable Initialization Works
In Python, all data is an object, and each of these objects has a unique identifier that corresponds to the memory address where it is stored. This ensures efficient memory management and allows developers to work optimally with data in the Python programming language. Understanding the structure of objects and their identifiers is a key aspect when developing Python programs.
The identifier can be obtained using the id() function. This function allows you to find out the unique identifier of an object in Python, which is useful for tracking its location in memory. Using the id() function helps in debugging and optimizing code, as it provides information about how Python manages memory and objects.
When initializing the variable x, the program created an object of the int data type with the value 4.
The variable stores not the object itself, but its address. This can be easily confirmed by creating another variable that points to the same object. This way, when the value of one object changes, the other value will also change, since both variables reference the same location in memory. This demonstrates an important aspect of working with objects in programming, where references play a key role in data management.

Variables in programming can be assigned new values at any time. When this happens, the variable begins to refer to another object. This property allows data to be dynamically changed and managed during program execution, which makes the code more flexible and adaptable to changes.
Now the x and y variables point to other objects with new addresses in memory. Since no other variable anymore refers to the object with the address 2056817043856, the garbage collector will automatically delete this object, freeing the memory it occupied. Thus, memory management in the programming language is carried out efficiently, which helps improve application performance.
Multiple assignment of values
If several variables need to be assigned the same value, this can be done in one line using the following construct:
Let's check the text for compliance with SEO requirements. We will optimize it while maintaining the original theme and without adding unnecessary content. Pay attention to keywords and phrases to improve visibility in search engines.
The text will be rewritten with an emphasis on readability and relevance. We will clarify the wording and structure so that they meet modern optimization standards.
Make sure to keep the main meaning unchanged, and also consider the importance of meta tags and headings to improve positions in search results.
Let's start with the necessary text analysis and its revision.
There is a method for assigning different values.
Importing variables
To import a variable from another file, use the from <file name> import <variable name> construct. This method allows you to efficiently use code by dividing it into modules and simplifying project maintenance and development. By importing variables, you can avoid code duplication and improve the readability of your project.
We have a file named variables1.py. This file can contain various variables used in your project. It is important to organize and name variables correctly to improve readability and ease of use of your code. For example, the file may contain numeric values, strings, or even lists that will be used in further calculations or application logic. Optimizing the variable structure in variables1.py improves performance and makes it easier to debug the code.
In this section, we will look at using the name variable in the variables2.py file. The name variable can store various values that can be used in the program to perform certain operations. For example, you could assign the name variable a string value representing a username or an object name. This use makes the code more flexible and maintainable, and also simplifies various tasks related to data processing. Using the name variable in the variables2.py file is an important aspect of software development, as it helps organize and structure your code, improving its readability and simplifying future modifications.
In this case, you cannot access other variables from the variables1.py file. This limitation is due to the scope of variables, which can only be accessed within a specific context. To use variables from this file, you must ensure that the data is imported or passed correctly, which will avoid problems with accessing them later.
To import multiple variables in your code, you must separate them with commas. This allows for efficient data management and simplifies the process of working with variables in your project. Correct syntax ensures clarity and ease of use of these variables. Make sure that each variable is correctly defined and meets the requirements of your code. Using this approach significantly simplifies development and improves code readability.
To import all objects in JavaScript, use the asterisk (*). This allows you to load all exported elements from the specified module, simplifying the process of working with libraries and components. Star imports are especially useful when you need to integrate multiple functions or classes from a single source, providing more convenient and efficient code management.
Deleting Variables
To delete a variable in Python, use the del keyword. This allows you to free the memory occupied by the variable and remove it from scope. Using del is an important part of memory management in Python, especially when working with large amounts of data. Properly deleting unnecessary variables helps keep your code efficient and optimized. Additionally, del can be used to remove elements from lists or dictionaries, making it a versatile tool for managing data in various structures.
Variable Types in Python
All variables in Python are within a certain scope. There are three main scopes in the Python programming language: local, global, and built-in. Each of these scopes determines where variables can be accessed and how they can be used in the code. Local variables are available only within the function or block where they are created. Global variables, on the other hand, can be used anywhere in the code once they are declared. Built-in variables are part of the language itself and are always available. Understanding variable scope in Python is key to writing code effectively and eliminating errors.
- local - within a single function;
- global - within the entire program (py file);
- non-local - within two adjacent functions (inner and outer).
Variables that belong to a particular scope form its namespace. A namespace allows you to organize and manage variables, ensuring their uniqueness and preventing naming conflicts. It is a key aspect of programming that helps keep your code organized and easier to maintain.
Variables declared inside a function have a scope limited to that function only. This means that they are not accessible outside of their scope. For example, if you create a variable inside a function, you cannot access it outside of that function. This property of variables, known as local scope, helps avoid name conflicts and promotes better code organization by ensuring that each function works with its own data. This approach improves code readability and maintainability, which is especially important when developing complex programs.
We can manipulate the variable x by changing its value, performing various operations, and returning the result, but only within the scope of the local_namespace() function.
If we try to access the variable x in another part of the program, the interpreter will not be able to recognize it. This happens because the variable's scope is limited to the block of code in which it was declared. Therefore, it is important to properly manage variables and understand where they are accessible to avoid errors and improve code efficiency.
A function can only access the variables defined within its body. This restriction avoids conflicts with variables outside the function and ensures code isolation. Thus, variables created within a function do not affect global variables, which contributes to safer and more predictable code.
A function can read variables from the global scope, but it does not have permission to modify them. This restriction ensures data security and helps avoid unintentional changes, which is especially important in the context of code development and maintenance. Adherence to this rule helps maintain the integrity of the program and simplifies debugging.
A single program can contain multiple variables with the same name, which is possible through the use of different namespaces. This avoids conflicts between variables and simplifies code organization. Separating variables into namespaces is an important aspect of programming, as it contributes to better readability and maintainability of code.
Variables declared outside functions are global and are available in the namespace of the entire Python file. These global variables can be used in different functions and methods, allowing data to be shared between them. However, it is important to remember that changing a global variable within a function requires the use of the global keyword. This avoids creating a new local variable with the same name and ensures proper data management within the program. Proper use of global variables helps simplify code and improve its readability, but overusing them can lead to confusion and errors.
As mentioned, variables can be accessed within a function, but they cannot be modified. To allow a variable to be modified, the global keyword is used. It allows a function to access a global variable and modify its value, making the code more flexible and manageable.
When creating a new variable inside a function, you can use the global keyword to make it available in the global scope. This allows you to modify the value of a global variable from within the function, which can be useful in certain situations. However, you should be careful when using global variables, as they can lead to unpredictable program behavior and make debugging more difficult. Proper management of variable scope is an important aspect of programming.
Non-local variables are used in situations where one function is contained within another. They provide access to variables that are outside the local scope of the inner function, but are still accessible to the outer function. This allows values to be stored and used in different contexts, ensuring efficient communication between functions. Non-local variables are an important tool for managing state and simplifying code in complex applications.
The nonlocal keyword is used to create variables in the enclosing scope, allowing you to modify the value of variables in parent functions. This is especially useful in situations where you need to work with variables that are not in the local scope, but should not be global. Using nonlocal promotes more efficient state management in closures and improves code readability by avoiding confusion with global variables.
A non-local variable cannot be accessed in the global scope.
Checking the existence of a variable
Python provides the globals() and locals() functions, which return dictionaries containing tables of global and local symbols. These functions are useful for obtaining information about current variables and their values in different scopes. globals() provides access to global variables, while locals() allows you to work with variables defined in the current context. Using these functions can greatly simplify debugging code and analyzing the state of program variables.
In these dictionaries, the key is a string containing the variable name, which allows you to check whether the variable exists. This approach provides convenience and efficiency when working with data, as it allows you to quickly find and process information. Checking the existence of a variable using a string key is an important aspect of programming, especially in languages that support dynamic typing. This helps avoid errors and simplifies debugging the code.
From this dictionary, you can get the value of a variable by its name.
Similar approaches can also be used in the local scope. This allows you to manage variables and functions that are available only within a certain block of code. Using the local scope helps avoid name conflicts and improves the readability of the code, and also promotes more efficient resource management. This approach is especially useful when working with large projects where maintaining order and organization is important.
The maximum possible value of a variable
Python does not have a specialized data type for working with large numbers, which distinguishes it from many other programming languages. All integers in Python are instances of the int class, while floating-point numbers are instances of the float class. This allows developers to easily work with numbers of various sizes without worrying about type conversion or using additional libraries to handle larger numbers. This versatility makes Python a convenient tool for mathematical calculations and scientific computing.
These classes impose no restrictions. They allow any variable of any data type to contain values of virtually unlimited size, limited only by the computer's available memory.
Printing Single and Multiple Variables
Python uses the print() function to display data on the screen. It can display either a single variable or multiple variables separated by commas. This function is the primary tool for user interaction, as it allows you to display text, numbers, and other objects. Using print() makes debugging easier because it allows you to quickly check variable values while the program is running.
Python allows you to format the output of variables, which allows you to create more readable and understandable messages. One of the most effective ways to do this is to use f-strings. To use f-strings, simply pass a string starting with the letter f to the print() function. The variables to be printed are specified in curly braces: {variable name}. This method simplifies the formatting process and makes the code more concise. Using f-strings is especially useful when working with multiple variables, since they allow you to easily combine text and variable values in a single line.
Summary
- Variables in Python consist of a name and a value. In this case, they do not store the value itself, but a reference to its address in memory.
- They are named using numbers (they cannot be the first character in the name), Latin letters, and underscores.
- In Python, you do not need to declare a variable separately - you immediately assign a value to it. You can create several variables in one line.
- They exist within a certain scope. Variables can be global (within the entire program), local (within one function), and non-local (within two nested functions).
- Variables in Python have no size limit - they can be as large as the device's memory allows.
- They can be imported from other files, deleted, checked for existence in the current scope, and printed in a formatted string.
Rework the text, but keep the main topic and do not add anything unnecessary. Optimize the text for SEO and, if necessary, supplement it with content. Avoid using emojis and unnecessary symbols. Don't include sections with numbers or symbols; simply provide plain text.
Additional reading:
- Python data types for beginners: what they are and how to work with them
- Test: what do you know about Python creator Guido van Rossum?
- Writing a desktop application in Python using Tkinter

