DEV Community

ananyaa sri
ananyaa sri

Posted on

Identifiers, Variables, Keywords and Data Types

Today, I learned about identifiers, variables, naming rules, Python keywords, the Zen of Python, IDEs, and basic data types.

Let's understand each concept with simple explanations and practical examples.

  1. What are Identifiers in Python?

An identifier is a name used to identify an element in a Python program.

For example:

name = 'Sanjay'
doubt = False
doubt = True
questions = 5
Enter fullscreen mode Exit fullscreen mode

In this example:

  • name is an identifier.
  • doubt is an identifier.
  • questions is an identifier.

The values assigned to these identifiers are:

  • 'Sanjay' – String
  • True – Boolean
  • 5 – Integer

Understanding the Assignment Operator (=)

name = 'Narasimma'
Enter fullscreen mode Exit fullscreen mode

Here:

  • name – Identifier (variable name)
  • = – Assignment operator
  • 'Narasimma' – Value

The assignment operator assigns the value on the right-hand side to the variable on the left-hand side.

  1. Rules for Naming Identifiers

Python follows certain rules when naming identifiers.

  1. Alphabets: Both lowercase and uppercase letters are allowed.
  2. Digits: Numbers from 0 to 9 are allowed, but an identifier cannot start with a digit.
  3. Underscore (_): An underscore can be used in an identifier.

Valid Identifiers

name = 'Raja'
name1 = 'Raja'
name2 = 'Sanjay'
_name = 'Megal'
Enter fullscreen mode Exit fullscreen mode

Invalid Identifier

1name = 'Raja'
Enter fullscreen mode Exit fullscreen mode

This is invalid because an identifier cannot start with a number.

Understanding Underscores

Python uses underscores in different ways:

  • _name – A convention indicating that the variable is intended for internal use.
  • __name – Uses a double leading underscore, which triggers name mangling in classes.
  • __name__ – A special naming convention commonly used for Python's built-in special methods and attributes.

Top comments (0)