Use numbers and variables data types in Python

Business Benefits

Assign values to variables to use in mathematical functions and to build clean, reusable code.


Assign numbers or mathematical operators to variables to store data in scripts, access and manipulate data, or use in loops and functions.

Variables can be assigned and changed when needed, which helps refactor code by taking up fewer lines.

For example:
a = 2b = 3c = a+b

a, b and c in the above example are variables. a and b are assigned numbers and c is assigned a mathematical operator (+).

Follow Python naming rules to create valid variable names.

Python has the following rules to create a valid variable name:

  • Use only letters ( a-z, A-Z ), underscores ( _ ) and numbers ( 0-9 ) to create variable names, nothing else.
  • Variable names must begin with a underscore ( _ ) or a letter.
  • Variable names are case sensitive.
  • Don’t use reserved keywords (see below) as variable names.

Reserved keywords have very specific meanings in Python, so they can’t be used as variable names. Python reserved keywords include:

  • and
  • as
  • assert
  • break
  • class
  • continue
  • def
  • del
  • elif
  • else
  • except
  • False
  • finally
  • for
  • from
  • global
  • if
  • import
  • in
  • is
  • lambda
  • None
  • nonlocal
  • not
  • or
  • pass
  • raise
  • return
  • True
  • try
  • while
  • with
  • yield

Python is case-sensitive language, meaning that TEST_VAR and test_var denote two different variables. For instance:

TEST_VAR = 3
test_var = 5

out_var = TEST_VAR + test_var

Output:

8

```Note: uppercase TEST_VAR is different to lowercase test_var and hence the out_var was assigned the sum of both TEST_VAR and test_var = 8.


## Use `variable_name = number` assignment statements to assign numbers or static values to variables.

`variable_name` is the name of the variable and `=` is the assignment operator. An example of a variable assignment statement would be: `test_var = 123`

This statement creates a variable named `test_var` and assigns it the value of `123`. The Python Interpreter then:

- Stores the variable `123` in memory.
- Uses the variable `test_var` to refer to it.

`test_var` doesn't contain the actual value; rather it refers to a memory location storing the value.

Unlike other languages like C, C++, and Java, variable types aren't declared ahead of time in Python. In fact, trying to do this will cause a syntax error.


## Use variables in expressions to assign values to other variables.

Expressions are simple mathematical operations. They are combinations of values, variables, and operators. When scripting, you'll sometimes come across situations where the value from the expression needs to be validated before proceeding to the next step. For example:

a = 1
b = 2

test_var = a+b

if test_var > 1:
   print('Proceed to next step')
else:
   print('Do not proceed to next step')

```In the above expression, a and b are static variables and test_var is assigned a value based on the sum of variables a and b.

If test_var is greater than 1 (>1), then the script will display the text “Proceed to next step.”

test_var can be assigned another value like test_var = 10, or even an expression like test_var = c+d later on in the code.

Choose numbers to use based on your intended purpose for them.

There are four types of numbers in Python:

Integers, or int for short, are whole numbers. For example, 100, 77, and -992 are integers.

Floating Point Numbers, or float, are numbers that contain floating decimal points. For example, 1.2, 0.21, -99.0 are floating point numbers. They can also be written using scientific notation in the form a x 10^b, which is useful when writing very small or very large numbers. For example, float 0.000000123 can be written succinctly in scientific notation as 1.23 x 10^-7. Exponents (e or E) can be used to express numbers in Scientific notation in Python. For example, 0.000000123 would be written as 1.23E-7.

Complex Numbers are numbers which can’t be represented on a number line. They are expressed in the form a + ib, where a is the real part and ib is the imaginary part. For example, 2 + 3i is complex number. Complex numbers can also be expressed in Python using a trailing j. For example, Python would treat 10j, 9.12j as complex numbers. Note that Python only represents the imaginary part of the complex number. To create a complex number with both a real and imaginary part, simply add a number to the imaginary part. For example, the complex number 2 + 3i can be written in Python as 2 + 3j.

Boolean numbers are true or false and can be used to validate expressions whose outputs will either be true or false.
For example:
a=1b=2
if a < b then:
test_var == true
else:
test_var == false

Last edited by @hesh_fekry 2023-11-14T15:10:24Z