Attempting to create a variable with the same name as a reserved word generates an error: a Python variable is a symbolic name that represents a reference or pointer to an object. Once an object has been assigned to a variable, you can refer to the object by that name. But the data itself is still contained within the object. What happens when you assign variables? This is an important question in Python because the answer is slightly different from what you`ll find in many other programming languages. We choose the names of our variables ourselves. However, there are certain rules for creating a legal variable (a name we are allowed to use). Variable names with more than one word can be difficult to read. Several variables assigned to the same literal value have the same ID, for example: B: In Python, each object created receives a number that uniquely identifies it. It is guaranteed that two objects do not have the same identifier for a period of time when their lifespan overlaps. Once the number of references of an object drops to zero and it is a garbage collector, as was the case with the 300 objects above, its identification number becomes available and can be reused. from collections import namedtupleL = [[“Jerry Zheng”, 12345678], [“Terry Zhang”, 23456789], [“Jimmy Yang”, 34567890]]def stu_info(info: list): We can redeclare the Python variable once we have already declared the variable. The global keyword is a keyword that allows a user to modify a variable outside the current range.
It is used to create global variables from a non-global scope, that is, within a function. The global keyword is used in a function only when we want to make assignments or modify a variable. Global is not required for printing and access. All of them are probably better decisions than n or ncg or whatever. At least you can tell from the name what the value of the variable is supposed to represent. The underscore ( _ ) can appear in a name. It is often used in names with multiple words, such as my_name or airspeed_of_unladen_swallow. Variable names can start with an underscore, but we generally avoid it unless we are writing library code that others can use. You`ll see later that variables aren`t the only things that can get names.
You can also name functions, classes, modules, and so on. The rules that apply to variable names also apply to identifiers, a more general term for the names assigned to program objects. The built-in Python function id() returns the integer identifier of an object. You can use the id() function to check if two variables really refer to the same object: the variable name 76trombones is not allowed because it starts with a number. The name more@ is illegal because it contains an @ illegal character. But what`s wrong with the class? Unlike other programming languages such as C# or Java, Python is a dynamically typed language, which means you don`t have to declare a variable type. The type is dynamically assigned based on the assigned value. For example, all of the following variable names are valid: Global variables are those that are defined and declared outside of a function, and we must use them in a function. Variables in Python are not subject to this restriction. In Python, a variable can be assigned a value of one type and later a value of another type: think of a variable as a name added to a particular object. In Python, variables do not need to be declared or set in advance, as is the case in many other programming languages.
To create a variable, simply assign it a value and then start using it. The assignment is done with a single equal sign (=): programmers usually choose meaningful names for their variables and document what the variable is for. For optimization purposes, the interpreter creates objects for integers between [-5, 256] at startup and then reuses them while the program runs. Therefore, if you assign separate variables to an integer value in this range, they actually refer to the same object. Just as a literal value can be displayed directly from the interpreter prompt in a REPL session without the need for print(), so can a variable: all variables are actually an object of a class, depending on their value. The tutorials in this series sometimes refer to the lifetime of an object. The life of an object begins when it is created, creating at least one reference to it. During the lifetime of an object, additional references can be created for it, as you saw above, and references to it can also be removed. An object remains alive, so to speak, as long as there is at least one indication of it. Different operations can be performed on variables with different operators depending on the type of variable. For example, the + operator adds two int variables while concatenating two string variables, as shown below. Python also allows you to assign a single value to multiple variables at the same time using the “=” operators.
For example, data types are the classification or categorization of data elements. It represents the type of value that indicates which operations can be performed on specific data. Since everything is an object in Python programming, data types are actually classes and variables are instances (objects) of those classes. If we use the same name, the variable refers to a new value and type. If we name our variables like this, we have no problem: # Don`t forget to access the parameters in a namedtuple object, you have to use the “. (dot) operator.# You can also use indexing to access attributes in the right order, but less efficiently. It is recommended that you call attributes by name. In the example above, the first value int 10 is assigned to the first variable x, the second value to the second variable y, and the third value to the third variable z. Values must be assigned to variables in the order listed.
Note that the matter is important. Lowercase and uppercase letters are not the same. The use of the underscore is also important. Each of the following defines a different variable: The following code checks whether n refers to an entire object: In this tutorial, you`ll learn what you`ll learn here: you`ll learn how to describe each piece of data in a Python program by the abstract term object and how to use symbolic names called variables. edit. The examples you`ve seen so far have used short, concise variable names like m and n. However, variable names can be more wordy. In fact, it is usually advantageous for them to be, as it makes the purpose of the variables clearer at first glance. It is worth giving a variable a name that is descriptive enough to make it clear what it is for. Let`s say you count the number of people who graduated from university. You can choose one of the following options: However, they are not all necessarily readable in the same way. As with many things, this is a matter of personal preference, but most people would find the first two examples, where the letters are all pushed together, harder to read, especially the one in capital letters.