• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Python Keywords and Identifiers

August 25, 2020 by javainterviewpoint Leave a Comment

In this article, let’s learn about Python Keywords and Identifiers. Keywords in Python are the reserved words and cannot be used as variable names, function names, or class names, etc.  Python Identifiers are nothing but the name which we give to identify a variable, function, object, etc.

Python Keywords and Identifiers

Python Keywords and Identifiers

Python Keywords

Keywords are nothing but the reserved words used for defining a syntax or structure in the code, as they have a special meaning and purpose, and hence it cannot be used as an identifier.

For Example, let’s take ‘import’; it is a reserved keyword in Python used for importing a python module. If we are using it as a variable name, then we will be getting the “SyntaxError: invalid syntax” error.

There are 35 keywords in Python 3.8. In Python, Keywords are case sensitive all the keyword except True, False, and None should be in lower case.

We can get the list of all keywords in two different ways

  1. Using help()
  2. From the keyword module

1. Using help()

Open IDLE and type help(), to open the help utility of Python. Now type “keywords” command, to get the list of all keywords

>>> help()
help> keywords

Here is a list of the Python keywords.  Enter any keyword to get more help.

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                 

2. From the keyword module

The other way of getting all the keywords is from the keyword module.

Open the IDLE and import keyword module, now run the command “keyword.kwlist”

>>> import keyword
>>> 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']

Python Identifiers

Identifiers are nothing but the name given to the various entities used in our code, such as variables, functions, classes, etc.

Below are the rules which we need to follow while creating an identifier

Rules to follow while creating an Identifier

  1. An identifier can be a combination of lowercase letters(a -z), uppercase letters (A-Z), numbers (0-9), and underscore (_). Example: myAge, new_year, val1 are valid identifiers.
  2. Identifiers cannot start with a number, 1name is invalid, whereas name1 is valid.
  3. An Identifier cannot have Special characters such as <>/%^&:'”,@#*~-+?|\! and Spaces in it.
  4. We cannot use Python keywords as an identifier, hence import, if, break, etc. are not valid identifiers.
  5. Identifiers are case sensitive; therefore, myAge and myage are considered as different identifiers.

Python also provides some special identifiers as well which uses underscore ‘_’ prefixed or suffixed

  • Private Identifier _xxx
  • System-defined name __xxx__
  • Request private name mangling in classes __xxx

Python Variables

In the real world, we cannot directly keep working on the data. We may need to give names to the data so that we can save and process the data in that name. In short, we can consider the variable as a tag for the container. The type of the variable is the kind of data it stores.

In most of the strongly-typed languages such as C++ or Java, we need to declare the variable type before the variable since Python is not strongly typed, we don’t need to mention the variable type before the variable, and the Python interpreter will automatically assign them.

Variable Assignment

Using the = symbol we can perform a variable assignment, the syntax is

variable_name = data

Below are some of the valid assignments

val = 10

name = ‘CodingPointers’

price = 12.5

Whenever we assign an object to the variable the following happens, let’s take the int assignment as an example

  1. As a first step int type of object will be created in the memory
  2. Creation of the variable name ‘val’, The variable name as such will not have its own type, so if we check the type of the variable name, it gives us the type to which it points to.
  3. Now, attach the variable name with the object.

Augmented Assignment

The Augmented assignment is nothing but combining arithmetic or bitwise operation and followed by an assignment.

For Example, from Python 2 onwards, we can write x = x + 1 as x +=1

The Augmented assignment operator supported by Python are the below

+=, -=, /=, %=, <<=, >>=, &=, ^=, |=

Python does not support either post-increment or post-decrement operations

x++ or x– will throw Syntax error

Multiple Assignment

We can assign a single object to multiple variables in a single shot like below

>>> x = y = z = 10
>>> x
10
>>> y
10
>>> z
10

In the above code, we created an integer object (with the value 10), and all the three variables x, y, and z will refer to the same object int 10.

We can also assign multiple objects to multiple variables in a single assignment like below.

>>> x, y, z = 10, 22.5, 'CodingPointers'
>>> x
10
>>> y
22.5
>>> z
'CodingPointers'

In the above code, we have assigned int, float, and string objects to the variable x, y, and z in a single assignment.

This approach comes in handy in many places; one of them would be swapping. Suppose in other programming languages such as Java, to swap two variables we will be following the below approach.

int x = 10;
int y = 20;
int temp = x;
x = y;
y = temp;

We needed a temp variable to hold one of the values to perform the swapping.

In Python, with the help of multiple assignments, we will no longer need a third variable to hold the value. We can directly do the swap like below.

>>> x, y = 10, 20
>>> x
10
>>> y
20
>>> x, y = y, x
>>> x
20
>>> y
10

Happy Learning!!

Filed Under: Python Tagged With: Identifier, Keyword, Python Identifiers, Python Keywords, Python Keywords and Identifiers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·