Infinity

For applications that require using infinity, Python allows creating float values that represent positive and negative infinity.

  • Positive infinity: float("inf"),
  • negative infinity: float("-inf").

inf is used to initialise values or to handle special cases for various algorithms. E.g., to find a maximum value, you may start with -inf and compare you numbers to it. Using -inf in the initial step will ensure that your program will update the current maximum value.

Exponentials

Python also has a built-in operation ** for computing exponentials. E.g., 2**4 computes \(2^4\).

Augmented Assignments

Python also provides a shorthand notation for updating values of numerical variables. These type of operations are called in-place updates or augmented assignments.

  • Increment: a += b is equivalent to a = a + b, add then assign.
  • Decrement: a -= b is equivalent to a = a - b, substract then assign.

These operations, first evaluate an expression on the right side of = using the old value of a variable a, then assign the result to update a in-place.

Additionally, Python also supports the following:

  • a *= b is equivalent to a = a * b,
  • a /= b is equivalent to a = a / b.

These assignments are not limited to simple operations and can be used with more complex function calls.

Formatting Numbers

For easier entry and readability, Python also allows entering numbers with _ separator. For instance, to enter 1,000,000 you could either use 1000000 or 1_000_000, and the latter is easier to read. You can put _ between any two numeric characters including float numbers.

It is also common to use scientific notation for entering very large or very small numbers.

When printing or reading numbers on the screen we “see” the representation of the number, such as using scientific notation for very small or large numbers. You can also set your preferred representation using the format specification when formatting strings with f-strings and format method.

In the above, f and e, in 1.2e or in 018.12f, are types.

  • f and F refer to float, and
  • e and E to scientific notation.

Another common type to use is the general type g which uses both scientific and floating point (float) representations depending on the value of the number.

Formatting can also be used with other types, e.g., to specify the width of the resulting string representation of the value.

Here we are formatting value of a string ' Hi! ', ^ is center alignment, and - is the filler and can be any character, 15 specifies the width.

See the official format specification documentation to see more examples and details about string formatting.

Comparison and Logical Operations

Comparison expressions use logical operators to test equality or compare values and result in bool values, True or False. These types of expressions are also known as Boolean expressions. Python defines the following comparison operators to be used in Boolean expressions,

  • == : equality test.
  • != : not equal, returns True if values are not equal.
  • >, <: larger than, and less than.
  • >=: larger than or equal, and
  • <=: less than or equal.

Additionally, built-in logical operators and, or, not, and in can be used to compute more complex Boolean expressions.

  • A and B : True if both A and B are True, False otherwise.
  • A or B : True if either A or B are True, False if both A and B False.
  • not EXPRESSION: negates a Boolean expression, e.g., not True is False.

x < y is equvalent to (x <= y) and (x != y) with the latter using three operations instead of just one <.

  • A in B : True if A is in B (used in collections like sequences and strings).

Equality operations, == and != can be used with any data type. For instance, you can check if a variable’s value is equal to a string “Python”.