## Data Types and Operators

## Data Types

There are a decent number of Data Types in Python by default and to make it easier to understand, we could actually classify them.

1. Numeric Data Types

**Integer:**Positive or negative whole numbers (without a fractional part)**Float:**Any real number with a floating-point representation in which a fractional component is denoted by a decimal symbol or scientific notation-
**Complex number:**A number with a real and imaginary component represented as x+yj. x and y are floats and j is -1(square root of -1 called an imaginary number)

2. Boolean

Boolean is essentially a data type that denotes True and False, Null and Not Null, and so on.

Boolean has two values to itself and in Python, we denote them as `True`

and `False`

.

3. Sequence Type

**String**: A string value is a collection of one or more characters put in single, double, or triple quotes.**List**: A list object is an ordered collection of one or more data items, not necessarily of the same type, put in square brackets.**Tuple**: A Tuple object is an ordered collection of one or more data items, not necessarily of the same type, put in parentheses.

4. Dictionary

**Dictionaries** are **Python's** implementation of a data structure that is more generally known as an associative array. A **dictionary** consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.

Try this out in the editor below to understand better!

`>>> type(1234)`

>>> type(55.50)

>>> type(6+4j)

>>> type("hello")

>>> type([1,2,3,4])

>>> type((1,2,3,4))

>>> type({1:"one", 2:"two", 3:"three"})

Data objects of the above types are stored in a computer's memory for processing. Some of these values can be modified during processing, but contents of others can't be altered once they are created in the memory.

Number values, strings, and tuple are immutable, which means their contents can't be altered after creation.

On the other hand, the collection of items in a List or Dictionary object can be modified. It is possible to add, delete, insert, and rearrange items in a list or dictionary. Hence, they are mutable objects.

## Operators

Try these expressions in the editor below and try to figure out how they work.

Python language supports the following types of operators.

- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators

### Arithmetic operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

Operator | Meaning | Example |
---|---|---|

+ | Add two operands or unary plus | x + y+ 2 |

- | Subtract right operand from the left or unary minus | x - y- 2 |

* | Multiply two operands | x * y |

/ | Divide left operand by the right one (always results into float) | x / y |

% | Modulus - the remainder of the division of left operand by the right | x % y (remainder of x/y) |

// | Floor division - division that results in the whole number adjusted to the left in the number line | x // y |

** | Exponent - left operand raised to the power of right | x**y (x to the power y) |

### Comparison operators

Comparison operators are used to compare values. It returns either `True`

or `False`

according to the condition.

Operator | Meaning | Example |
---|---|---|

> | Greater than - True if left operand is greater than the right | x > y |

< | Less than - True if the left operand is less than the right | x < y |

== | Equal to - True if both operands are equal | x == y |

!= | Not equal to - True if operands are not equal | x != y |

>= | Greater than or equal to - True if the left operand is greater than or equal to the right | x >= y |

<= | Less than or equal to - True if the left operand is less than or equal to the right | x <= y |

### Logical operators

Logical operators are the `and`

, `or`

, `not`

operators.

Operator | Meaning | Example |
---|---|---|

and | True if both the operands are true | x and y |

or | True if either of the operands is true | x or y |

not | True if the operand is false (complements the operand) | not x |

### Bitwise operators

Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.

For example, 2 is `10`

in binary and 7 is `111`

.

**In the table below:** Let `x` = 10 (`0000 1010`

in binary) and `y` = 4 (`0000 0100`

in binary)

Operator | Meaning | Example |
---|---|---|

& | Bitwise AND | x & y = 0 (`0000 0000` ) |

| | Bitwise OR | x | y = 14 (`0000 1110` ) |

~ | Bitwise NOT | ~x = -11 (`1111 0101` ) |

^ | Bitwise XOR | x ^ y = 14 (`0000 1110` ) |

>> | Bitwise right shift | x >> 2 = 2 (`0000 0010` ) |

<< | Bitwise left shift | x << 2 = 40 (`0010 1000` ) |

### Assignment operators

Assignment operators are used in Python to assign values to variables.

`a = 5`

is a simple assignment operator that assigns the value 5 on the right to the variable `an` on the left.

There are various compound operators in Python like that adds to the variable and later assigns the same. It is equivalent to `a = a + 5`

.

Operator | Example | Equivalent to |
---|---|---|

= | x = 5 | x = 5 |

+= | x += 5 | x = x + 5 |

-= | x -= 5 | x = x - 5 |

*= | x *= 5 | x = x * 5 |

/= | x /= 5 | x = x / 5 |

%= | x %= 5 | x = x % 5 |

//= | x //= 5 | x = x // 5 |

**= | x **= 5 | x = x ** 5 |

&= | x &= 5 | x = x & 5 |

|= | x |= 5 | x = x | 5 |

^= | x ^= 5 | x = x ^ 5 |

>>= | x >>= 5 | x = x >> 5 |

<<= | x <<= 5 | x = x << 5 |

### Special operators

Python language offers some special types of operators like the identity operator or the membership operator. They are described below with examples.

#### Identity operators

`is`

and `is not`

are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

Operator | Meaning | Example |
---|---|---|

is | True if the operands are identical (refer to the same object) | x is True |

is not | True if the operands are not identical (do not refer to the same object) | x is not True |

### Membership operators

`in`

and `not in`

are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set, and dictionary).

In a dictionary, we can only test for the presence of a key, not the value.

Operator | Meaning | Example |
---|---|---|

in | True if value/variable is found in the sequence | 5 in x |

not in | True if value/variable is not found in the sequence | 5 not in x |

It's highly recommended to try these out in the editor below and go through the documentation as well for an in-depth understanding. However, it is to be noted that for some new developers, it could get confusing. Hence, new developers are suggested to try these operators in the editor below with their own understanding. Bitwise Operators are not really a common sight in usual programming situations, especially in Python.

Additional Resources: https://docs.python.org/3/library/datatypes.html, https://docs.python.org/3/library/operator.html