0% found this document useful (0 votes)
37 views5 pages

Python Operators Q&A Guide

Uploaded by

iam5747462
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views5 pages

Python Operators Q&A Guide

Uploaded by

iam5747462
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Answer Sheet for Python Operators Questions

Arithmetic Operators:

1. Arithmetic operators in Python are used to perform mathematical operations. The operators are:

- `+` (Addition): Adds two numbers. Example: `5 + 3 = 8`

- `-` (Subtraction): Subtracts one number from another. Example: `5 - 3 = 2`

- `*` (Multiplication): Multiplies two numbers. Example: `5 * 3 = 15`

- `/` (Division): Divides one number by another. Example: `5 / 3 = 1.666...`

- `//` (Floor Division): Returns the quotient without the decimal part. Example: `5 // 3 = 1`

- `%` (Modulus): Returns the remainder. Example: `5 % 3 = 2`

- `**` (Exponentiation): Raises one number to the power of another. Example: `5 ** 3 = 125`

2. Python Program:

```python

x = 10

y=3

print("Addition:", x + y)

print("Subtraction:", x - y)

print("Multiplication:", x * y)

print("Division:", x / y)

print("Floor Division:", x // y)

print("Modulus:", x % y)

print("Exponentiation:", x ** y)

```

3. The `/` operator performs division and returns a float, whereas `//` performs floor division,
discarding the decimal part.

4. Python Function:

```python

def area_of_rectangle(length, breadth):

return length * breadth

print(area_of_rectangle(5, 3))

```

5. For `x = 15` and `y = 4`, `x % y` results in 3.

Logical Operators:

6. Logical operators are used to combine conditional statements:

- `and`: Returns True if both statements are true. Example: True and False = False

- `or`: Returns True if at least one statement is true. Example: True or False = True

- `not`: Reverses the result. Example: not True = False

7. Outputs:

- True and False -> False

- False or True -> True

- not (True and True) -> False

8. Python Function:

```python

def is_positive_and_even(num):

return num > 0 and num % 2 == 0

print(is_positive_and_even(4))
```

9. The not operator reverses a condition's logical value, while and and or combine conditions.

10. For x = 10, the expression x > 5 and x < 20 evaluates to True.

Comparison Operators:

11. Comparison operators compare two values:

- `==` (Equal to): Example: 5 == 3 -> False

- `!=` (Not equal to): Example: 5 != 3 -> True

- `>` (Greater than): Example: 5 > 3 -> True

- `<` (Less than): Example: 5 < 3 -> False

- `>=` (Greater than or equal to): Example: 5 >= 3 -> True

- `<=` (Less than or equal to): Example: 5 <= 3 -> False

12. Python Program:

```python

def compare_numbers(a, b):

if a > b:

print("First number is greater.")

elif a < b:

print("First number is smaller.")

else:

print("Both numbers are equal.")

compare_numbers(7, 5)

```
13. Outputs:

- a == b -> False

- a != b -> True

14. Leap Year Check Function:

```python

def is_leap_year(year):

return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)

print(is_leap_year(2024))

```

15. Outputs:

- 10 >= 10 -> True

- 5 < 3 -> False

- 8 != 8 -> False

Bonus:

16. Combined Expression:

```python

def is_multiple_of_3_and_5(num):

return num % 3 == 0 and num % 5 == 0

print(is_multiple_of_3_and_5(15))

```

17. Python Program:

```python

def is_within_range(num):
return num >= 1 and num <= 100

print(is_within_range(50))

```

You might also like