Python Crash Course for Beginners | Python Keywords and Identifiers
Python is a high-level programming language that is widely used in various fields including web development, data analysis, and scientific computing. Like any programming language, Python has its own set of reserved words known as keywords, and also a set of user-defined names known as identifiers. In this article, we will take a closer look at Python keywords and identifiers, and how they are used in Python programming.
Keywords are reserved words in Python that have special meaning and cannot be used as identifiers or variable names. They are used to define the syntax and structure of the Python programming language. There are 35 keywords in Python 3.9.6, including:
and, as, assert, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, return, True, try, while, with, yield
These keywords have a specific syntax and purpose in the Python programming language. For example, the keyword
if is used to define a conditional statement, while the keyword
def is used to define a function. It is important to note that these keywords cannot be used as variable names, function names, or any other identifiers.
Identifiers are user-defined names used to identify variables, functions, classes, and other objects in Python. An identifier can be a combination of letters (a-z or A-Z), digits (0–9), and underscore (_). However, an identifier cannot start with a digit.
In Python, the naming convention for identifiers is to use lowercase letters for variable and function names, and uppercase letters for class names. Identifiers can also have a combination of uppercase and lowercase letters.
Let’s take a look at some coding examples to better understand Python keywords and identifiers.
# Example of Python keywords for i in range(5): print(i) # Example of Python identifiers my_variable = 10 my_function = lambda x: x**2 class MyClass: pass
In this code, we use the Python keyword
for to define a loop that iterates through a range of numbers. We then define three Python identifiers:
my_variable is a variable that holds the value of 10,
my_function is a lambda function that returns the square of its input, and
MyClass is a class that does nothing (represented by the
It is important to note that we cannot use any of the Python keywords as identifiers in our code. For example, if we try to define a variable with the name
if, we will get a syntax error because
if is a reserved keyword in Python.
# Example of a syntax error caused by using a Python keyword as an identifier if = 5
The output of this code will be a syntax error, because
if is a reserved keyword in Python and cannot be used as an identifier.
In summary, Python keywords are reserved words in the Python programming language that have special meaning and cannot be used as identifiers. Identifiers are user-defined names used to identify variables, functions, classes, and other objects in Python. It is important to use proper naming conventions for identifiers and to avoid using Python keywords as identifiers in order to avoid syntax errors in our code.
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.