Python Language Skill UP
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Python Keywords, Identifiers and Variables

Python Keywords, Identifiers and Variables The objective of this tutorial is to learn about the Python keywords, identifiers and variables. In this topic, we are going to know about keywords in Python and how to identify them.
After that we will discuss different types of keywords available in Python and finally We will also brief look at Identifiers and variables with some examples.

1. Python Keywords:
“ Keywords in Python are unique reserved words that cannot use as a variable name, function name, or other identifiers.”.
Keywords in python are used to perform some specific action or task .
Each keyword serves different purpose and all together they built python script or program or we can say vocabulary of the Python Language.
According to version 3.8 to 3.11.2 in python, there are 35 keywords that it supports. These are the basic building blocks of Python programming. Hence, you must know everything about them.
Most of them have a purpose very similar to their actual meaning in English Language.

All keywords in Python are case sensitive. So, you must be careful while using them in your code.
Example To list out or find total number of the possible Python keywords.

# e.g Python script To list out total number of Keywords
>>>Import keyword
>>>print(keyword.kwlist)

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']


Another way to list out total number of Keywords in python is given below. Just use the help() function.
e.g
>>>help(“keywords”)
or
>>>help()
after this help prompt will be displayed
help>
Here is a list of the Python keywords.
Enter any keyword to get more help.
Help>keywords
False       class       from       or
None       continue       global       pass
True       def       if       raise
and       del       import       return
as       elif       in       try
assert       else       is       while
async       except       lambda       with
await       finally       nonlocal       yield
break       for       not
One important thing is that we should keep in mind that the list of python keywords may change because new keywords may bring in the language and old one will not be continued in the future releases.

2.Python Identifiers:


“Identifiers” or “symbols” are the name given to entities like variables, class, functions and labels in programs etc.
If we assign some name to a programmable entity in Python, then it is nothing but technically called an identifier. Identifier must be different from any keywords. Keywords can’t be used as identifiers.
Python language lays down a set of rules for programmers to create meaningful identifiers.

Rules for naming Identifiers in Python.
1. To form meaningful an identifier use Combination of alphabets in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _
For example – Names like shapeClass, shape_1, and upload_shape_to_db are all valid identifiers.

>>>shapeClass=1
Is valid and following is invalid.
>>>shape Class=1
SyntaxError: invalid syntax

2. Digits cannot be used as the starting of an identifier.
For example – The name, 1Shape is incorrect, but shape1 is a valid identifier.
>>> 1shape=1
SyntaxError: invalid decimal literal

3. Keywords cannot be used as identifiers.
For example – False       class       from       or
None is a invalid identifiers.

4. Special symbols !, @, #, $, % etc. cannot be used in an identifier.
E.g #age,@name


Following are some of the key facts about Python variables.
1. In Python Variables don’t require declaration. However, you must initialize them before use.
For example –
age = 20
The above expression will lead to the following actions.
• Creation of an object to represent the value 20.
• If the variable (age) doesn’t exist, then it’ll get created.
The variable ‘age’ is a reference to the value ’20’.
2. Whenever the expression changes, Python associates a new object (a chunk of memory) to the variable for referencing that value. And the old one goes to the garbage collector.

Example.
>>> age = 20
>>> id(20)
1716585200
>>> age = 11
>>> id(age)
1716585232
>>>

3. Also, for optimization, Python builds a cache and reuses some of the immutable objects, such as small integers and strings.
4. An object is just a region of memory which can hold the following.
• The actual object values.
• A type designator to reflect the object type.
• The reference counter which determines when it’s OK to reclaim the object.
5. It’s the object which has a type, not the variable. However, a variable can hold objects of different types as and when required.
Example.
>>> age = 20
>>> type(age)
<class 'int'>
>>> name = 'Python'
>>> type(name)
<class 'str' >
>>> name = {'Python', 'C', 'C++'}
>>> type(name)
<class 'set'>

3. Rules For Naming Python Variable: –

1. A Variable name must start with a letter or the underscore character.
2. Variable names are case-sensitive (e,g Name,name and NAME are different)
3. A Variable name cannot start with a number(eg. 9age,0name)
4. A variable name can only contain alpha-numeric character and and underscore (_,A-Z and 0-9)

e.g.
#legal Variable names
myname=”Python”
my_name=”Python”
_my_name=”Python”
myName=”Python”
MYNAME=”Python”
myname2=”Python”

#Illegal variable names
2myname=”Python”
my-name=”Python”
my var=”Python”

4. Difference Between Python Variables and Identifiers.

Variables

Identifiers

A variable is a unique name to a memory location

Unique name given to entity is called Identifiers

Variable names are called Identifiers

Range of identifiers are higher than variables

Variable is a name given to memory location that hold some kind of values

An identifier is a name given to variable,constant, List, Tuple, Class etc.

Next topic Python Data Types



Next topic Python Data Types

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python.
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple 'Python' Scripts
    • Namespace in 'Python'
    • Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • Assignments
  • Input Output in Python
    • Accept Input
    • Output Formating
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • short hand IF-ELSE
    • Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • while loop assignments
    • for Loop
    • For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • break,continue and else in Loops
    • Difference between for and while loop
    • for each loop
    • for each Assignments
  • List
    • List in Python
    • Access List elements
    • List functions
    • Iterate(loop)List
    • List comprehension
    • List Assignments
  • Tuple
    • Tuple in Python
    • Access tuple elements
    • tuple functions
    • Iterate(loop)tuple
    • Unpack Tuple
    • tuple comprehension
    • tuple Assignments
  • set
    • Set in Python
    • Access set elements
    • set Methods
    • Iterate(loop)set
    • Pack/Unpack Tuple
    • tuple comprehension
    • set Assignments
    • Set comprehension
  • Dictionary
    • Dictionary
    • Access dictionary Items
    • dictionary method
    • Iterate(loop)Dictionary
    • formating Dictionaries
    • Nested Dictionaries
    • dictionary comprehension
    • dictionary Assignments
  • Diff list tuple set dictionary
    • list vs tuple
    • list vs set
    • list vs dictionary
    • tuple vs set
    • tuple vs dictionary
    • dictionary vs set
  • Exception
    • Error vs Exception
    • Exception Handling
    • Types of Exception
    • User Defined Exception
    • Logging Exception
    • assignments
  • Functions
    • Introduction
    • Modular Programming
    • Type of functions
    • Inbuilt Function
    • Need For User-Defined Function
    • Elements Of User Defined Function
    • Function Argument
    • Nesting of Function
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • Assignments
  • Python Module
    • introduction to module
    • Inbuilt Module in Python
    • User Defined Module in Python
    • Assignments
  • File Handling
    • Introduction to files
    • Create File
    • Read files
    • Write to File
    • Rename File
    • Copy File
    • Move file
    • List files in Directory
    • Binary files
    • Zipping and unzipping files
    • Assignments
  • Strings
    • Basics of Strings
    • String Special Operator
    • String Formatting operator
    • String methods
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • Diff object oriented and procedural oriented programming
    • difference between classes and object
    • Constructors
    • destructor
    • built class methods and attributes
    • class and instance variable
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in python
    • Abstract class vs interface
    • Public in python
    • Private in python
    • Protected in python
    • Overloading Vs Overriding
    • inheritance vs composition
    • Encapsulation
    • Polymorphism
    • inner classes
    • Opps Assignments
  • Mysql database in python
    • introduction
    • DBMS vs file
    • connecting to database
  • Mysql database Operation
    • Select
    • Operators
    • DDL
    • DML
    • subqueries
    • joins
    • Case study
  • Graphics
  • Iterator in python
  • Generator in python
  • decorator in python
  • Threads in Python
    • introduction
    • process and threads
    • concurrent programming and GIL
    • Uses of threads
    • creating Threads
    • Single tasking
    • multi tasking
    • thread synchronization
  • Frequently Asked Interview Questions (FAQ)
  • Case Studies
  • Multiple Choice Questions

Get in touch

  • tech2dsm@gmail.com

© tech2dsm. All rights reserved.