Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Operators

The document provides an overview of variables, data types, and operators in Python programming. It explains the four data types (String, Integer, Float, Boolean), type casting, and various operators including arithmetic, assignment, comparison, and logical operators. Additionally, it includes examples of expressions and their evaluations to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Operators

The document provides an overview of variables, data types, and operators in Python programming. It explains the four data types (String, Integer, Float, Boolean), type casting, and various operators including arithmetic, assignment, comparison, and logical operators. Additionally, it includes examples of expressions and their evaluations to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

LET US REVIEW!

1. Which is the variable?


2. What is the value of the variable?
3. What is the data type of the value of the variable?
4. What is the output of the source code?
LET US REVIEW!

1. Which is the variable?


2. Which command is being used to enter value?
3. What is the data type of the value of the variable?
4. What is the output of the source code?
LET US REVIEW!

1. Which are the variables?


2. What is the data type of the variables?
3. Which operator is being used?
4. What is the output of the source code?
REMEMBER!
Variables
We can think of variables as like boxes. Each box has a label, and we can
store information (data) in each box. This information can vary during the
running of a program. The boxes are emptied of data when the program
ends.

Key Term:
Adding strings together is
called concatenation
(con - cat - en - ay - shun).
Python has four data types:
• String (text or alphanumeric information);
• Integer (a whole number, a number which is not a fraction);
• Float (a number which can be a fraction);
• Boolean (can only be one of two values, True or False).

Type Casting
In programming we sometimes need to change the data type
of information. This is known as type casting.
Evaluate each expression and tell whether it's true or false.

3<2 FALSE
5>8 FALSE
3>=3 TRUE
4 == 4 TRUE
7 != 3 TRUE
Python: Operators
In This Lesson
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Arithmetic Operators
Arithmetic Operators

Arithmetic operators are used for mathematical


operations like addition, subtraction, and others.
Arithmetic Operators
Example
Operator Name Output
(x = 5, y = 2)
+ Addition x+y 7

- Subtraction x-y 3

* Multiplication x*y 10

/ Division x/y 2.5


Arithmetic Operators

Example
Operator Name Output
(x = 5, y = 2)
% Modulus x%y 1

** Exponentiation x ** y 25

// Multiplication x//y 2
Example
x = 5
y = 2
print("x + y = ",x + y) x+y= 7
print("x - y = ",x - y) x-y= 3
print("x * y = ",x * y) x * y = 10
print("x / y = ",x / y) x / y = 2.5
print("x % y = ",x % y) x%y= 1
print("x ** y = ",x ** y) x ** y = 25
print("x // y = ",x // y) x // y = 2
Example
print(5 + 2) 7
print(5 - 2) 3
print(5 * 2) 10
print(5 / 2) 2.5
print(5 % 2) 1
print(5 ** 2) 25
print(5 // 2) 2
Assignment Operators
Assignment Operators

Assignment operators are used to assign


values to a variable.
Example x = 3
print(x) 3
print(x + 3) 6
print(x - 3) 0
print(x * 3) 9
print(x / 3) 1.0
print(x % 3) 0
print(x // 3) 1
print(x ** 3) 27
Comparison Operators
Relational / Comparison Operators

Comparison operators returns a Boolean


either True or False.
Comparison Operators
Operator Description Example
Returns True if the left operand is
> higher than the right operand x>y
Returns True if the left operand is
< lower than the right operand x<y
Returns True if the left operand is
== equal to the right operand x==y
Comparison Operators
Operator Description Example
Returns True if the two operands
!= are not equal x != y
Returns True is the left operand is
>= higher than or equal to the right x >= y
operand
Returns True if the left operand is
<= lower than or equal to the right x <= y
operand
Example
x = 3 y = 2

print(x > y) True


print(x < y) False
print(x == y) False
print(x != y) True
print(x >= y) True
print(x <= y) False
Logical Operators
Logical Operators

Logical operators are used to combine


conditional statements and returns a
Boolean True or False.
Logical Operators

Operator Description Example


Returns True if both of the
AND statements or expressions are True x AND y = False
Returns True if one of the
OR statements or expressions is True x OR y= True
Reverse the results, returns False if
NOT it is True and True if it is False NOT x = False
Example

x = True
y = False
print(x and y) False
print(x or y) True
print(not x) False
Example

x = 3
y = 7
print(x > 4 and y < 10) False
print(x > 4 or y < 5) False
print(not(x > 2)) False

You might also like