Python Numbers

Python Numbers

Expression syntax is straightforward: the operators +, -, * and / work just like in most other languages (for example, Pascal or C); parentheses (()) can be used for grouping. For example:

Note: Division (/) always returns a float.

To do floor division and get an integer result (discarding any fractional result) you can use the // operator; to calculate the remainder you can use %:

   With Python, it is possible to use the ** operator to calculate powers

There is full support for floating point; operators with mixed type operands convert the integer operand to floating point:

In Python, the last printed expression is assigned to the variable _. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:

This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behavior.

So There are three numeric types in Python:

  • int: int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
  • float: Float, or “floating point number” is a number, positive or negative, containing one or more decimals
  • complex : Complex numbers are written with a “j” as the imaginary part:  (e.g. 3+5j).

Python Variable:

  • Variables are containers for storing data values.
  • Unlike other programming languages, Python has no command for declaring a variable.
  • A variable is created the moment you first assign a value to it.

The equal sign (=) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:

If a variable is not “defined” (assigned a value), trying to use it will give you an error:

* Variables do not need to be declared with any particular type and can even change type after they have been set.

Rules for Python:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables).

Remember that variables are case-sensitive

Sets in Python

A set is a collection which is unordered and doesn’t index them in a particular order.

Every element is unique (no duplicates) and must be immutable (which cannot be changed).

A set is itself mutable and allows addition or deletion of items.

With Sets, we can execute several mathematical operations such as Union, Intersection, Symmetric Difference, and Complement.

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top