# Operators
---
title: Agenda
description:
duration: 300
card_type: cue_card
---
### Agenda
1. Operators
2. Arithmetic operators
3. Precedence of operators
4. Boolean operators
5. Comparison operators
6. Assignment operators
7. Logical Operators
---
title: Operators
description:
duration: 2000
card_type: cue_card
---
## Operators
- **Operators** are symbols of operation.
- **Operands** are values on which operation is happening
- Operation/Expression are combination of operands and operators
- Operation/Expression gives a result after execution.
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/405/original/operators.png?1689918923" width=600 height=250>
## Arithmetic operators
- operators such as `+`, `-`, `*`, `/`, `//`, `**`, `%`
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/407/original/arithmetic_op.png?1689919111" width=600 height=100>
\
Code:
``` python=
# Addition
10 + 3
```
>Output:
```
13
```
Code:
``` python=
# Subtraction
10 - 3
```
>Output:
```
7
```
Code:
``` python=
# Multiplication
10 * 3
```
>Output:
```
30
```
- The return type of the division operator `/` is always a floating point object.
Code:
``` python=
# Division
10 / 3
```
>Output:
```
3.3333333333333335
```
Code:
``` python=
# float + float -> float
5.0 +5.0
```
>Output:
```
10.0
```
### Subset diagram of Number System -
integers ⊂ floats ⊂ real numbers
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/411/original/number_system.png?1689919391" width=500 height=400>
\
Python retains as much information as it can.
- for `+`, `-`, `*` if one of the operand is float, then result is float.
- for `/` the result is always float.
---
title: Quiz-1
description:
duration: 60
card_type: quiz_card
---
# Question
What would be the output of the following?
``` python
x = 10
y = 2.5
print(x / y)
```
# Choices
- [ ] 4
- [x] 4.0
- [ ] 4.5
---
title: Exponentiation, Floor Division & Modulas operators
description:
duration: 2000
card_type: cue_card
---
## Exponentiation operator (`**`)
```x**y``` = x $^ y$
Code:
``` python=
5 ** 2
```
>Output:
```
25
```
Code:
``` python=
5 ** -1
```
>Output:
```
0.2
```
Code:
``` python=
5 ** 0.5 # square root
```
>Output:
```
2.23606797749979
```
Code:
``` python=
5.0 ** 2
```
>Output:
```
25.0
```
## Floor Division operator (`//`)
`x//y` = floor(x/y)
### Floor function
- The floor() function gives the biggest integer less than the value.
- On a number line, it gives the closest integer to the left of the value.
- Example:
- `floor(-0.67)` gives -1
- `floor(2.3)` gives 2
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/412/original/floor1.png?1689920271" width=600 height=200>
\
Code:
```python=
10//3
```
>Output:
```
3
```
Code:
```python=
-10//3
```
>Output:
```
-4
```
## Modulus operator (`%`)
`x % y` -> remainder of `x / y`
- If `x` is **'+ve'** -> remainder of `x / y`
- If `x` is **'-ve'** -> `y - (x % y)`
Code:
``` python=
10 % 3
```
>Output:
```
1
```
Code:
``` python=
-10 % 3
```
>Output:
```
2
```
---
title: Quiz-2
description:
duration: 60
card_type: quiz_card
---
# Question
Predict the output.
``` python
print(10**-1)
```
# Choices
- [ ] 10
- [ ] 1/10
- [x] 0.1
---
title: Quiz-3
description:
duration: 60
card_type: quiz_card
---
# Question
What would be the output of the following?
``` python
15//2
```
# Choices
- [ ] 7.5
- [x] 7
- [ ] 1
---
title: Precedence of operators
description:
duration: 360
card_type: cue_card
---
## Precedence of operators
#### Question: Predict the ouptut
``` python
10-4*2
```
**Ans:** 2
- Division and Multiplication have the same order of precedence.
- Addition and Subtraction have the same order of precedence.
- In case of encountering operators with same precedence, go from **left to right**.
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/415/original/bodmas.png?1689922896" width=600 height=300>
#### Question: Predict the output
``` python
10-4*2+5-6/2
```
**Ans:** 4.0
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/416/original/question_bodmas.png?1689922995" width=600 height=350>
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/417/original/bodmas_question_2.png?1689923639" width=600 height=250>
---
title: Quiz-4
description:
duration: 60
card_type: quiz_card
---
# Question
Predict the output.
``` python
x = 11
y = 2
z = 4
res = (x + y - z) ** (x % z)
print(res)
```
# Choices
- [x] 729
- [ ] 81
- [ ] 9
---
title: Quiz-4 explanation, `bool()`
description:
duration: 700
card_type: cue_card
---
### Quiz-4 explanation
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/418/original/quiz7.png?1689923709" width=500 height=350>
### The `bool()` function
In Python,
- Every number except 0 is `True`.
- Every non-empty strings is `True`.
Code:
``` python=
bool(1)
```
>Output:
```
True
```
Code:
``` python=
bool(0)
```
>Output:
```
False
```
Code:
``` python=
bool(-123)
```
>Output:
```
True
```
Code:
``` python=
bool("Hello")
```
>Output:
```
True
```
Code:
``` python=
bool("")
```
>Output:
```
False
```
Code:
``` python=
bool(" ")
```
>Output:
```
True
```
#### **Question:** Predict the output
```python
print(bool('false'))
```
A. True
B. False
**Ans:** `True`
#### **Question:** Predict the output
```python
print(int(True))
```
A. 1
B. 0
**Ans:** `1`
---
title: Break & Doubt Resolution
description:
duration: 600
card_type: cue_card
---
### Break & Doubt Resolution
`Instructor Note:`
* Kindly take this time (up to 5-10 mins) to give a short break to the learners.
* Meanwhile, you can ask the them to share their doubts (if any) regarding the topics covered so far.
---
title: Comparison, Assignment & Logical operators
description:
duration: 2400
card_type: cue_card
---
## Comparison operators
- operators such as `>`, `<`, `>=`, `<=`, `==`, `!=`
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/419/original/operators_comparison.png?1689924424" width=600 height=200>
\
Code:
``` python=
my_marks = 50
my_cousin_marks = 90
my_marks > my_cousin_marks
```
>Output:
```
False
```
Code:
``` python=
my_marks == my_cousin_marks
```
>Output:
```
False
```
Code:
``` python=
my_marks = 90
my_marks == my_cousin_marks
```
>Output:
```
True
```
Code:
``` python=
my_marks!=my_cousin_marks
```
>Output:
```
False
```
## Assignment operator
- `=` is an assignment operator.
- It assigns the RHS operand value to the LHS operand.
Code:
``` python=
a = 4 * 4
a
```
>Output:
```
16
```
Code:
``` python
a = 5
a = a + 2
a
```
>Output:
```
7
```
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/420/original/assignment_op.png?1689924874" width=500 height=250>
\
Code:
``` python=
# Question - What will be the output?
marks = 50
correction = 0.5
marks = marks + correction
marks = marks + correction
marks
```
>Output:
```
51.0
```
Code:
``` python=
# Question - What will be the output?
a = 5
a += 2
print(a)
a -= 2
print(a)
a *= 2
print(a)
```
>Output:
```
7
5
10
```
## Logical operators
- `and`, `or`, `not`
- used when there are multiple conditions
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/421/original/logical_op.png?1689925052" width=600 height=200>
### `and` operator
- `True` only if all the conditions are satisfied.
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/422/original/and_op.png?1689925148" width=600 height=250>
### `or` operator
- `True` if any one of the conditions is satisfied.
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/423/original/or_op.png?1689925216" width=600 height=350>
### Truth Tables
- **x** and **y** are conditions
- 1 is `True`, 0 is `False`
#### Truth table for `and`
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/424/original/tt_for_and.png?1689925318" width=300 height=250>
#### Truth table for `or`
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/425/original/tt_or.png?1689925403" width=300 height=250>
\
Code:
``` python=
# Question - What will be the output?
a=10
a>5 or a>20 or a<2 #True
a>5 and a==20 and a<2 #False
```
>Output:
>
True
False
Code:
``` python=
# Question - Check if a number is a multiple?
amount = 1000
amount % 500 == 0
```
>Output:
```
True
```
Code:
``` python=
# ATM Dispatch
amount= int(input("Please enter the amount to withdraw: "))
amount % 500 == 0 or amount % 200 == 0
```
>Output:
```
Please enter the amount to withdraw: 2039
False
```
### `not` operator
- Works only with **boolean** operands.
Code:
``` python=
print(not True)
print(not False)
print(not 24 < 56)
```
Output:
```
False
True
False
```
---
title: Practice Coding Question(s)
description:
duration: 600
card_type: cue_card
---
### Practice Coding Question(s)
You can pick the following question and solve it during the lecture itself.
This will help the learners to get familiar with the problem solving process and motivate them to solve the assignments.
<span style="background-color: pink;">Make sure to start the doubt session before you start solving the question.</span>
Q. https://www.scaler.com/hire/test/problem/96782/