Identifiers in Python
Last updated on 25th Sep 2020, Artciles, Blog
Today we will learn about Python keywords and identifiers. Earlier we learned how to install python and get started with it in a python articlesl for beginners
Python Keywords
Well simply, python keywords are the words that are reserved. That means you can’t use them as names of any entities like variables, classes and functions.
So you might be thinking what are these keywords for. They are for defining the syntax and structures of Python language.
You should know there are 33 keywords in Python programming language as of writing this tutorial. Although the number can vary in course of time. Also keywords in Python are case sensitive. So they are to be written as it is. Here is a list of all keywords in python programming.
Subscribe For Free Demo
Error: Contact form not found.
If you look at all the keywords and try to figure out all at once, you will be overwhelmed. So for now just know these are the keywords. We will learn their uses respectively. You can get the list of python keywords through python shell help.
>>> help() Welcome to Python 3.6’s help utility! help> keywords Here is a list of the Python keywords. Enter any keyword to get more help. False def if raise None del import return True elif in try and else is while as except lambda with assert finally nonlocal yield break for not class from or continue global pass help>
Below is a simple example showing usage of if-else in python programs.
- var = 1; if(var==1): print(“odd”) else: print(“even”)
When we run the above program, python understands the if-else block because of fixed keywords and syntax and then does the further processing.
Python Identifiers
Python Identifier is the name we give to identify a variable, function, class, module or other object. That means whenever we want to give an entity a name, that’s called identifier.
Sometimes variables and identifiers are often misunderstood as the same but they are not. Well for clarity, let’s see what is a variable?
Variable in Python
A variable, as the name indicates, is something whose value is changeable over time. In fact a variable is a memory location where a value can be stored. Later we can retrieve the value to use. But for doing it we need to give a nickname to that memory location so that we can refer to it. That’s the identifier, the nickname.
Rules for writing Identifiers
There are some rules for writing Identifiers. But first you must know Python is case sensitive. That means Name and name are two different identifiers in Python. Here are some rules for writing Identifiers in python.
- 1. Identifiers can be a combination of uppercase and lowercase letters, digits or an underscore(_). So myVariable, variable_1, variable_for_print all are valid python identifiers.
- 2. An Identifier can not start with a digit. So while variable1 is valid, 1variable is not valid.
- 3. We can’t use special symbols like !,#,@,%,$ etc in our Identifier.
- 4. Identifiers can be of any length.
Though these are hard rules for writing identifiers, also there are some naming conventions which are not mandatory but rather good practices to follow.
- 1. Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
- 2. Starting an identifier with a single leading underscore indicates the identifier is private.
- 3. If the identifier starts and ends with two underscores, then means the identifier is a language-defined special name.
- 4. While c = 10 is valid, writing count = 10 would make more sense and it would be easier to figure out what it does even when you look at your code after a long time.
- 5. Multiple words can be separated using an underscore, for example this_is_a_variable.
Here’s a sample program for python variables.
- myVariable=”hello world” print(myVariable) var1=1 print(var1) var2=2 print(var2)
If you run the program, the output will be like the image below.
Python Keywords
Keywords are the reserved words in Python.
We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.
In Python, keywords are case sensitive.
There are 33 keywords in Python 3.7. This number can vary slightly over the course of time.
All the keywords except True, False and None are in lowercase and they must be written as they are. The list of all the keywords is given below.
False | await | else | import | pass |
None | break | except | in | raise |
True | class | finally | is | return |
and | continue | for | lambda | try |
as | def | from | nonlocal | while |
assert | del | global | not | with |
async | elif | if | or | yield |
Looking at all the keywords at once and trying to figure out what they mean might be overwhelming.
If you want to have an overview, here is the complete list of all the keywords with examples.
Python Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.
Rules for writing identifiers
- 1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to
Pega Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download
- 9) or an underscore _. Names like myClass, var_1 and print_this_to_screen, all are valid examples.
- 2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is a valid name.
Keywords cannot be used as identifiers.
global = 1
Output
File “<interactive input>”, line 1
global = 1
^
- SyntaxError: invalid syntax
We cannot use special symbols like !, @, #, $, % etc. in our identifier.
a@ = 0
Output
File “<interactive input>”, line 1
a@ = 0
^
- SyntaxError: invalid syntax
- An identifier can be of any length.
Things to Remember
Python is a case-sensitive language. This means, Variable and variable are not the same.
Always give the identifiers a name that makes sense. While c = 10 is a valid name, writing count = 10 would make more sense, and it would be easier to figure out what it represents when you look at your code after a long gap.
Multiple words can be separated using an underscore, like this_is_a_long_variable.
Are you looking training with Right Jobs?
Contact Us- Top Python Framework’s
- Python Interview Questions and Answers
- Python Tutorial
- Advantages and Disadvantages of Python Programming Language
- Python Career Opportunities
Related Articles
Popular Courses
- Java Online Training
11025 Learners
- C And C Plus Plus Online Training
12022 Learners
- Dot Net Training
11141 Learners
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know