#11 Python Tutorial for Beginners | Operators in Python
- Get link
- X
- Other Apps
#11 Python Tutorial for Beginners | Operators in Python
Working with Operators in Python
Welcome back! In this video, we'll be discussing different types of operators in Python, including:
- Arithmetic Operators
- Assignment Operators
- Unary Operators
- Relational Operators
- Logical Operators
Welcome back! In this video, we'll be discussing different types of operators in Python, including:
- Arithmetic Operators
- Assignment Operators
- Unary Operators
- Relational Operators
- Logical Operators
Arithmetic Operators
We're already familiar with arithmetic operators, such as addition, subtraction, multiplication, and division. We can use variables to perform these operations, like:
x = 2y = 3x + y # Output: 5x - y # Output: -1x * y # Output: 6x / y # Output: 0.6666666666666666
We can also use the modulo operator (%) to get the remainder of a division:
x % y # Output: 2
We're already familiar with arithmetic operators, such as addition, subtraction, multiplication, and division. We can use variables to perform these operations, like:
x = 2y = 3x + y # Output: 5x - y # Output: -1x * y # Output: 6x / y # Output: 0.6666666666666666We can also use the modulo operator (%) to get the remainder of a division:
x % y # Output: 2Assignment Operators
We use the assignment operator (=) to assign values to variables:
x = 8
We can also use shorthand notation to perform an operation and assign the new value to the variable:
x += 2 # Same as x = x + 2
We can use the same shorthand notation for subtraction, multiplication, and division:
x -= 3 # Same as x = x - 3x *= 3 # Same as x = x * 3x /= 2 # Same as x = x / 2
We can also assign values to multiple variables in one line:
a, b = 5, 6
We use the assignment operator (=) to assign values to variables:
x = 8We can also use shorthand notation to perform an operation and assign the new value to the variable:
x += 2 # Same as x = x + 2We can use the same shorthand notation for subtraction, multiplication, and division:
x -= 3 # Same as x = x - 3x *= 3 # Same as x = x * 3x /= 2 # Same as x = x / 2We can also assign values to multiple variables in one line:
a, b = 5, 6Unary Operators
A unary operator operates on only one operand. The negation operator (-) is an example of a unary operator:
n = 7-n # Output: -7
A unary operator operates on only one operand. The negation operator (-) is an example of a unary operator:
n = 7-n # Output: -7Relational Operators
Relational operators are used to compare two values. We can use the following operators:
- < (less than)
- > (greater than)
- <= (less than or equal to)
- >= (greater than or equal to)
- == (equal to)
- != (not equal to)
For example:
a, b = 5, 6a < b # Output: Truea == b # Output: Falsea <= b # Output: Truea != b # Output: True
Relational operators are used to compare two values. We can use the following operators:
- < (less than)
- > (greater than)
- <= (less than or equal to)
- >= (greater than or equal to)
- == (equal to)
- != (not equal to)
For example:
a, b = 5, 6a < b # Output: Truea == b # Output: Falsea <= b # Output: Truea != b # Output: TrueLogical Operators
Logical operators are used to combine multiple conditions. We can use the following operators:
- and
- or
- not
For example:
a, b, c = 2, 3, 4(a < b) and (b < c) # Output: True(a < b) or (b > c) # Output: Truenot (a < b) # Output: False
Relational Operators:
- Used to relate two different values.
- a ≠ b is an example of a relational operator.
Logical Operators:
- Used to combine two conditions based on and, or, or not.
- And returns true only if both conditions are true.
- Or returns true if at least one condition is true.
- Not reverses the output.
Example:
- Let's say we have two variables: a = 5 and b = 4.
- If we want to check if both conditions are true (a < 8 and b < 5), we can use the logical operator and.
- If we want to check if a < 8 and b < 2, we get false because b < 2 is false.
- The truth table for and is:
x y x and y 0 0 0 0 1 0 1 0 0 1 1 1
- All returns true if at least one condition is true.
- The truth table for all is the same as and.
- Not reverses the output.
Example:
- If we have x = true, we can use not to get false.
- The value of x can be reversed using not or x = !x.
These operators will be used frequently in the future videos when talking about loop patterns and complex code.
Logical operators are used to combine multiple conditions. We can use the following operators:
- and
- or
- not
For example:
a, b, c = 2, 3, 4(a < b) and (b < c) # Output: True(a < b) or (b > c) # Output: Truenot (a < b) # Output: FalseRelational Operators:
- Used to relate two different values.
- a ≠ b is an example of a relational operator.
Logical Operators:
- Used to combine two conditions based on and, or, or not.
- And returns true only if both conditions are true.
- Or returns true if at least one condition is true.
- Not reverses the output.
Example:
- Let's say we have two variables: a = 5 and b = 4.
- If we want to check if both conditions are true (a < 8 and b < 5), we can use the logical operator and.
- If we want to check if a < 8 and b < 2, we get false because b < 2 is false.
- The truth table for and is:
| x | y | x and y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
- All returns true if at least one condition is true.
- The truth table for all is the same as and.
- Not reverses the output.
Example:
- If we have x = true, we can use not to get false.
- The value of x can be reversed using not or x = !x.
These operators will be used frequently in the future videos when talking about loop patterns and complex code.
- Get link
- X
- Other Apps
Comments
Post a Comment