This article provides a comprehensive guide on Python identifiers, covering naming rules, reserved keywords, and practical examples for variable declaration.
Abstract
The article "Python Identifiers: Python Complete Course — Part 8" is designed to educate readers on the proper use of identifiers in Python. It begins by defining identifiers as names for variables, functions, classes, modules, or other objects within Python. The author emphasizes the importance of adhering to specific naming rules to avoid exceptions when running code. These rules include starting identifiers with a letter or underscore, followed by letters, underscores, or digits, and avoiding punctuation characters. The article also lists Python's reserved keywords, which cannot be used as identifiers. Practical examples are provided to illustrate variable assignment, including how to define multiple variables in a single line and the case sensitivity of Python. The author concludes by summarizing the key points about Python identifiers and invites readers to subscribe for more content and consider Medium membership to support the writer.
Opinions
The author believes that understanding identifiers is crucial for writing error-free Python code.
The case sensitivity of Python is highlighted as an important concept for developers to grasp.
The article suggests that following the outlined rules for identifier naming will lead to more readable and maintainable code.
By providing both correct and incorrect variable naming examples, the author conveys the importance of syntax in Python programming.
The use of real-world examples and visual aids, such as images and code snippets, is intended to enhance the learning experience for readers.
The author encourages reader engagement by inviting them to subscribe and follow their work, indicating a commitment to building a community of learners.
The mention of earning potential on Medium serves as an incentive for readers to become Medium members, which also supports the author financially.
Python Identifiers: Python Complete Course — Part 8
This article is a part of Python Complete Beginner to Expert Course
which you can find it here.
All resources are available in the “Resources” section below.
This article is also available as a YouTube video here.
Introduction
After you have learned how to install Python and take your first step in Python by running the ‘hello world’ program, now is the time to learn how you can identify variables in Python.
So, this article will cover the following outlines:
The identifier is a name used to identify a variable, a function, a class, a module, or any other object in Python. In other words, we can say the identifier is the object ID.
However, to be able to run your code without any problem, you have to respect some rules when you want to give your variable a name. Otherwise, the interpreter will raise an exception and you will not be able to run your code.
So, what are the identifier naming rules?
2. Identifier Naming Rules
An identifier should start with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, or digits (0 to 9).
Python does not allow punctuation characters such as ( #, @, $, %), to be used in the identifiers.
Python is a case-sensitive programming language. Thus, Manpower ( with a capital M) and manpower (with a small m) are different identifiers.
Note:
You CANNOT start your identifier name with digits.
Define a variable with the name a, its value equal to 10 and print its value on the screen.
Hint to help you:
To define a new variable, type the variable name (a) then equal sign (=) then its value (10)
Solution Input:
a = 10print(a)
Output:
10
Define three variables a, b, and c and make their value equal to 1 in one line of code, then print their value on the screen.
Hint to help you:
To define multiple variables with the same values in one line of code, type the variables’ names and separate between them using equal signs (a = b = c) because they are equal to each other and finally type the value.
(a = b = c = 10).
To print more than one variable using one print statement type print and between the brackets type the variables' names that you want to print and separate between them using commas.
Solution Input:
a = b = c = 1print(a , b, c)
Output:
1 11
Let us make the example more advanced by defining three variables a, b, c but now with different values, and make their values 1, 2, 3 in one line of code, then print their values on the screen.
Hint to help you:
You can assign different values to your variables by using commas between thevariables' names and between the values.
Solution Input:
a, b, c = 1, 2, 3print(a, b, c)
Output:
1 23
Now, to be sure that Python is case sensitive define two variables A, a then give them different values for example 1, 2 and print both of them what do you get?
Solution Input:
a = 1
A = 2print(a, A)
Output:
1 2
Great, till now everything is clear and simple as I guess. What about trying to add digits to your variables' names, what would you get?
Solution Input:
a0 = 1A1 = 2
print(a0, A1)
Output:
1 2
After we see different ways to create identifiers, let us take examples of some cases that would raise exceptions or errors in order to avoid them in your code later.
First, let us try to start the variable name with a digit.
For example:
0a = 5
Output:
SyntaxError: invalid syntax
Second, Let us use the keyword if as the variable name.
Input:
if=5
Output:
SyntaxError: invalid syntax
Third, define a variable with the name ‘hello’ and try to print the variable with the name ‘Hello’
Input:
hello = 5print(Hello)
Output:
NameError: name ‘Hello’ is not defined
Fourth, use @, a punctuation character, in the variable name.
Input:
c@ = 4
Output:
SyntaxError: invalid syntax
It is simple, right? So far you have tried 4 use cases in making different mistakes during defining your variables and see how the interpreter will raise an exception with different messages.
Python identifier: is a name for a function, a class, a variable, or a module.
An Identifier name should start with (A-Z or a-z or _) and then you can add (A-Z or a-z or _ or 0–9), but punctuation characters like #, @, % and &... Are not allowed.
Python is a case-sensitive language.
DO NOT use keywords as identifiers.
P.S.: A million thanks for your time reading my story. Before you leave let me mention quickly two points:
First, to get my posts in your inbox directly, would you please subscribe here, and you can follow me here.
Second, writers made thousands of $$ on Medium. To get unlimited access to Medium stories and start earning, sign up now for Medium membershipwhichonly costs $5 per month. By signing up with this link, you can directly support me at no extra cost to you.