Practical Python Operators Types

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Practical 4: Python Programs to Apply Different Operators and Types

Objective:

To understand and apply different types of operators in Python such as arithmetic, relational, logical,

and assignment operators.

To work with different data types such as integers, floats, and strings.

Theory/Concept:

- Operators: Operators are special symbols used to perform operations on values or variables.

Python supports several types of operators:

1. Arithmetic Operators: +, -, *, /, //, %, **

2. Relational Operators: ==, !=, <, >, <=, >=

3. Logical Operators: and, or, not

4. Assignment Operators: =, +=, -=, *=, /=

- Data Types: Python supports various data types, including:

1. int: Integer type, used to represent whole numbers.

2. float: Floating-point type, used to represent real numbers.

3. str: String type, used to represent text.

Materials or Tools Required:

- Python programming language.

- Python IDE (like IDLE, or PyCharm).

Algorithm:

1. Choose two variables with different types (e.g., integer and float).

2. Apply arithmetic operators and store the results.

3. Apply relational operators and print the comparison results.

4. Use logical operators with boolean values.


5. Demonstrate assignment operators by changing the value of variables.

Code:

```python

# Program to apply different operators

# Arithmetic Operators

a = 10

b=3

print('Arithmetic Operators:')

print(f'Addition: {a + b}')

print(f'Subtraction: {a - b}')

print(f'Multiplication: {a * b}')

print(f'Division: {a / b}')

print(f'Integer Division: {a // b}')

print(f'Modulus: {a % b}')

print(f'Exponentiation: {a ** b}')

# Relational Operators

print('\nRelational Operators:')

print(f'a == b: {a == b}')

print(f'a != b: {a != b}')

print(f'a > b: {a > b}')

print(f'a < b: {a < b}')

print(f'a >= b: {a >= b}')

print(f'a <= b: {a <= b}')


# Logical Operators

x = True

y = False

print('\nLogical Operators:')

print(f'x and y: {x and y}')

print(f'x or y: {x or y}')

print(f'not x: {not x}')

# Assignment Operators

a += 5

print('\nAssignment Operators:')

print(f'a after a += 5: {a}')

```

Output:

Arithmetic Operators:

Addition: 13

Subtraction: 7

Multiplication: 30

Division: 3.3333333333333335

Integer Division: 3

Modulus: 1

Exponentiation: 1000

Relational Operators:

a == b: False

a != b: True
a > b: True

a < b: False

a >= b: True

a <= b: False

Logical Operators:

x and y: False

x or y: True

not x: False

Assignment Operators:

a after a += 5: 15

Conclusion:

This program demonstrated the use of various operators in Python, such as arithmetic, relational,

logical, and assignment operators.

I learned how to work with different data types (integers, floats, and booleans) and perform

operations on them.

You might also like