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). Look at what happens when we try to create an invalid variable: Any appropriate identifier can be used as the name of a variable, depending on the following rules: The type of variable depends on the types of the assigned value. It turns out that class is one of the keywords in Python. The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable names. An ID is changed when a variable is replaced by another value. Variables in Python are objects. Use the type() function to get the class name of an object. For example, the following shows the class name of the variable num. If we use the same name, the variable refers to a new value and type. 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.
There are also things we can`t do. This is not possible when naming variables: 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. In Python, variable is the name given to a value, so it becomes easy to refer to a value later. In other words, a variable refers to an object. A literal value is assigned to a variable with the = operator, where the left side must be the name of a variable and the right side must be a value. In the following, a name is assigned to an integer value. 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: For optimization purposes, the interpreter creates objects for integers between [-5, 256] at startup and then reuses them while the program is running.
Therefore, if you assign separate variables to an integer value in this range, they actually refer to the same object. If you give a variable an invalid name, you get a syntax error. In the following example, none of the variable names are allowed. The underscore ( _) can also appear in a name. It is often used in names with multiple words, such as my_name or price_of_tea_in_china. There are situations where names that begin with an underscore have a special meaning, so a safe rule for beginners is to start all names with a letter. 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. 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. There is another restriction on identifier names. The Python language reserves a small set of keywords that identify specific features of the language. No object can have the same name as a reserved word. Python variables are containers that store values. Python is not „statically typed”. We do not need to declare variables before using them or declaring their type. A variable is created when we first assign it a value. A Python variable is a name assigned to a location. This is the basic storage unit in a program.
This assignment creates an integer object with a value of 300 and assigns the variable n to reference that object. In many programming languages, variables are statically typed. This means that a variable is initially declared with a specific data type and any value assigned to it during its lifetime must always have that type. 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: on the other hand, they are not necessarily all 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. The most commonly used methods for creating a multi-word variable name are the last three examples: So you`ll find instructors who deliberately don`t choose meaningful names when teaching beginners – not because they don`t think it`s a good habit, but because they`re trying to reinforce the message you, The programmer, must write code. to calculate the average. You may need to write an assignment statement to give a variable the value you want. All variables are actually an object of a class, depending on the value. Assigning a comma-separated value to each variable results in a syntax error, as shown below. However, this is not the case because a variable name cannot start with a number: you can keep this list handy.
If the interpreter complains about one of your variable names and you`re not sure why, check to see if it`s on this list. Variable names with more than one word can be difficult to read. If you give a variable an invalid name, you will receive a syntax error when you try to run the code. What happens when it runs? Python does not create another object. It simply creates a new symbolic name or reference (m) that points to the same object as n. The Python Code Style Guide, also known as PEP 8, includes naming conventions that list suggested standards for the names of different types of objects. PEP 8 includes the following recommendations: Nothing prevents you from creating two different variables in the same program called Age and Age, or agE. But it`s probably ill-advised. It would certainly confuse anyone trying to read your code, and even yourself after you`ve been away from it for a while.
Beginners sometimes confuse „useful for the human reader” with „useful for the computer”. So you will mistakenly think that because you called a variable mean or pi, you will automatically calculate an average or automatically associate the variable pi with the value 3.14159. No! The computer does not add semantic meaning to your variable names. Just as a literal value can be displayed directly from the interpreter prompt in a REPL session without requiring print(), so can a variable: From what you now know about variable assignment and object references in Python, the following probably won`t surprise you: If we name our variables like this, We have no problem: The attempt Creating a variable with the same name as a reserved word results in an error: 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. Global variables are those that are defined and declared outside of a function, and we must use them in a function.
For example, all of the following variable names are valid: 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. Here`s what you`ll learn in this tutorial: you`ll learn how to describe each piece of data in a Python program by the abstract term object, and you`ll learn how to manipulate objects using symbolic names called variables. True or False: The following is a legal variable name in Python: A_good_grade_is_A+ The variable name 76trumpeten is illegal because it starts with a number. The name more@ is illegal because it contains an @ illegal character. But what`s wrong with the class? 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. Note: One of the additions to Python 3 was full Unicode support, which also allows Unicode characters in a variable name.
In an upcoming tutorial, you will learn more about Unicode. 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: This tutorial covered the basics of Python variables, including object references and identity, and naming Python identifiers.