MyWhiteBoard TrainingHome

CHAPTER 2 ยท PYTHON PROGRAMMING

Python identifiers

Open Editor

← Back to course contents

Python Identifiers

A Python identifier is a name used to identify a variable, function, class, module, or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).

Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus India and india are two different identifiers in Python.

Here are following identifier naming convention for Python:

  • Class names start with an uppercase letter and all other identifiers with a lowercase letter.
  • Starting an identifier with a single leading underscore indicates by convention that the identifier is meant to be private.
  • Starting an identifier with two leading underscores indicates a strongly private identifier.
  • If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.

Python - Variable Types

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

Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.

Assigning Values to Variables

Python is scripting language. So, no need to specify data type for declaring variable.

The operand to the left of the = operator is the name of the variable, and the operand to the right of the = operator is the value stored in the variable.

Data type of a variable gets determined by the assigned value. For Example:

x = 10  # This is Number

y = "Hello World"  # This is a string variable

Example program

Watch out: Check difference between python variable and c/c++ variable.

Multiple Assignment

Python allows you to assign a single value to several variables simultaneously. For example:

a = b = c = 1

Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example:

a, b, c = 1, 2, "john"

Here two integer objects with values 1 and 2 are assigned to variables a and b, and one string object with the value "john" is assigned to the variable c.

Standard Data Types

  • Text Type: str
  • Numeric Types: int, float, complex (example of complex: 3+4.4j)
  • Sequence Types: list, tuple, range
  • Mapping Type: dict
  • Set Types: set
  • Boolean Type: bool
  • Binary Types: bytes, bytearray

int

Integer numbers are made up of digits 0 to 9 and can be prefixed with unary operators like + or -. Here are some examples:

>>> 142
142
>>> 0
0
>>> +100
100
>>> -5
-5

For readability purposes, you can use underscores in between the digits.

>>> 1_000_000_000
1000000000

Warning: Underscore cannot be used as the first or last character, and cannot be used consecutively.

float

Here are some examples for floating-point numbers.

>>> 3.14
3.14
>>> -1.12
-1.12

Python also supports the exponential notation.

>>> 543.15e20
5.4315e+22
>>> 1.5e-5
1.5e-05

Unlike integers, floating-point numbers have a limited precision. While displaying very small or very large floating-point numbers, Python will automatically convert them to the exponential notation.

>>> 0.0000000001234567890123456789
1.2345678901234568e-10
>>> 31415926535897935809629384623048923.649234324234
3.1415926535897936e+34

Arithmetic operators

All arithmetic operators you'd typically expect are available. If any operand is a floating-point number, result will be of float data type. Use + for addition, - for subtraction, * for multiplication and ** for exponentiation. As mentioned before, REPL is quite useful for learning purposes. It makes for a good calculator for number crunching. You can also use _ to refer to the result of the previous expression (this is applicable only in the REPL, not in Python scripts).

>>> 25 + 17
42
>>> 10 - 8
2
>>> 25 * 3.3
82.5
>>> 32 ** 42
1645504557321206042154969182557350504982735865633579863348609024

>>> 5 + 2
7
>>> _ * 3
21

There are two operators for division. Use / if you want a floating-point result. Using // between two integers will give only the integer portion of the result (no rounding).

>>> 4.5 / 1.5
3.0
>>> 5 / 3
1.6666666666666667
>>> 5 // 3
1

Use the modulo operator % to get the remainder. Sign of the result is same as the sign of the second operand.

>>> 5 % 3
2

>>> -5 % 3
1
>>> 5 % -3
-1
>>> 6.5 % -3
-2.5

Operator precedence

Arithmetic operator precedence follows the familiar PEMDAS or BODMAS abbreviations. Precedence, higher to lower is listed below:

  • Expression inside parentheses
  • exponentiation
  • multiplication, division, modulo
  • addition, subtraction

Expression is evaluated left-to-right when operators have the same precedence. Unary operator precedence is between exponentiation and multiplication/division operators. See docs.python: Operator precedence for complete details.

Integer formats

The integer examples so far have been coded using base 10, also known as the decimal format. Python has provision for representing binary, octal and hexadecimal formats as well. To distinguish between these different formats, a prefix is used:

  • 0b or 0B for binary
  • 0o or 0O for octal
  • 0x or 0X for hexadecimal

All of these four formats fall under the int data type. Python displays them in decimal format by default. Underscores can be used for readability for any of these formats.

>>> 0b1000_1111
143
>>> 0o10
8
>>> 0x10
16

>>> 5 + 0xa
15

Decimal format numbers cannot be prefixed by 0, other than 0 itself.

>>> 00000
0

>>> 09
  File "<python-input-1>", line 1
    09
    ^
SyntaxError: leading zeros in decimal integer literals are not permitted;
             use an 0o prefix for octal integers