5.Numbers and Operators in Python

Python provides powerful support for numeric operations and a variety of operators. This tutorial covers numeric types, arithmetic operators, comparison operators, and assignment operators with examples.

Numeric Types in Python

Python supports three main numeric types:

• int: Integer numbers without a fractional part (e.g., 10, -5)

• float: Floating-point numbers with decimal points (e.g., 3.14, -0.001)

• complex: Complex numbers with real and imaginary parts (e.g., 2+3j)

Example:

x = 10        # int
pi = 3.14     # float
z = 2 + 3j    # complex

Arithmetic Operators

Python provides several arithmetic operators:

Operator

Description

+

Addition

Subtraction

*

Multiplication

/

Division (float result)

//

Floor Division

%

Modulus (remainder)

**

Exponentiation

Python provides several arithmetic operators:

Examples:

a = 10
b = 3
print(a + b)   # 13
print(a – b)   # 7
print(a * b)   # 30
print(a / b)   # 3.333…
print(a // b)  # 3
print(a % b)   # 1
print(a ** b)  # 1000

Comparison Operators

Comparison operators are used to compare two values and return a Boolean result:

Operator

Description

==

Equal to

!=

Not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

Examples:

x = 5
y = 10
print(x == y)  # False
print(x != y)  # True
print(x > y)   # False
print(x < y)   # True
print(x >= y)  # False
print(x <= y)  # True

Assignment Operators

Assignment operators are used to assign values to variables and perform operations:

Operator

Description

=

Assign value

+=

Add and assign

-=

Subtract and assign

*=

Multiply and assign

/=

Divide and assign

//=

Floor divide and assign

%=

Modulus and assign

Assignment operators are used to assign values to variables and perform operations:

Examples:

x = 10
x += 5    # x = 15
x -= 3    # x = 12
x *= 2    # x = 24
x /= 4    # x = 6.0
x //= 2   # x = 3
x %= 2    # x = 1

Conclusion

Understanding numbers and operators in Python is fundamental for performing calculations and building logic in your programs. Practice these operators to become comfortable with Python’s syntax and capabilities.

Scroll to Top
Tutorialsjet.com