Skip to content

Latest commit

 

History

History
130 lines (100 loc) · 4.02 KB

File metadata and controls

130 lines (100 loc) · 4.02 KB

Python Identifiers:

A python identifier is a name used to identify a variable, function, class, module or other objects.

An Identifier starts with a letter A-Z or a-z or an underscore( _ ) followed by zero or more letters, underscore & digits (0 to 9).

The following are naming conventions for Python Identifiers:

Class names start with an uppercase letter. All other identifiers start with a lowercase letter and these identifier are called as public identifier.

Starting an Identifier with a single leading underscore indicates that the identifier is protected

Starting an Identifier with two leading underscore indicating a strongly private identifiers.

If the Identifier starts and ends with two trailing underscores the identifier is a language-defined special name.

Public, Protected and Private Attributes

Public attributes can be accessed anywhere inside or outside of the class definition.

Protected (restricted) attributes should only be used under certain conditions.

Private attributes can only be accessed inside of the class definition.

Naming Type Meaning
name Public These attributes can be freely used inside or outside of a class definition
_name Protected Protected attributes should be used outside of the class definition, unless inside of a subclass definition
__name Private This kind of attribute is inaccessible and invisible. It's neither possible to read nor to write those attributes, except inside of the class definition itself

Also, the str.isidentifier() function will tell us if a string is a valid identifier. This is available since Python 3.0.

  '__99__'.isidentifier()

Output: True

Python Keywords

Keywords are reserved words and you cannot use them as constant or variable or any other identifier names.

All the python keywords contain lowercase letters only.

and def exec if not return
assert del finally import or try
break elif for in pass while
class else async from print is
continue except global lambda raise yield

Variables

Variables are nothing but reserved memory locations to store values. This means when you create a variable, you reserve some space in memory.

Based on the datatype of a variable, the interpreter allocates memory & decided what can be stored in the reserved memory.

Assigning values to variables:

Python variables do not need explicit declaration ton reserve memory space. The declaration happens automatically when you assign a value to variable. The equal sign ( = ) is used to assign value to variables.

Example:
  area_code = 110074 # type - int
  name = "Speed" # type - str (string)
  floating_variable = 12.25 # type - float

  # assigning single values to multiple variables
    a, b, c = 1

  # assigning multiple values to multiple variables
  a, b, c = 12, 13, 14

Deleting a variable:

  a = 5
  print(a) # output: 5
  del a
  print(a) # output: NameError: 'a' is not defined