Basic Operations and Numerical Data Types
Basic Operations and Numerical Data Types in Python
In this tutorial, we will cover one of the fundamental aspects of Python programming - Basic Operations and Numerical Data Types. By the end of this guide, you will have a clear understanding of how to perform basic operations in Python and use various numerical data types.
Numerical Data Types
In Python, there are three numerical data types:
Integers (int): These are whole numbers, positive or negative, without decimals, of unlimited length.
Floating Point Numbers (float): These are real numbers with a decimal point dividing the integer and fractional parts.
Complex Numbers (complex): These are in the form of
a + bj
wherea
andb
are floats andj
represents the square root of -1 (which is an imaginary number).
Here is how you can use them:
# Integers
x = 10
print(type(x))
# Floating Point Numbers
y = 20.5
print(type(y))
# Complex Numbers
z = 1j
print(type(z))
Basic Operations
Python offers a full range of arithmetic operators that you can use for numerical calculations:
Addition (+): Adds values on either side of the operator.
Subtraction (-): Subtracts right-hand operand from left-hand operand.
Multiplication (*): Multiplies values on either side of the operator.
Division (/): Divides left-hand operand by right-hand operand.
Modulus (%): Divides left-hand operand by right-hand operand and returns the remainder.
Exponentiation ()**: Performs exponential (power) calculation on operators.
Floor Division (//): The division of operands where the result is the quotient in which the digits after the decimal point are removed.
Here is how you can use them:
# Addition
print(5 + 3) # Outputs: 8
# Subtraction
print(5 - 3) # Outputs: 2
# Multiplication
print(5 * 3) # Outputs: 15
# Division
print(5 / 3) # Outputs: 1.6666666666666667
# Modulus
print(5 % 3) # Outputs: 2
# Exponentiation
print(5 ** 3) # Outputs: 125
# Floor Division
print(5 // 3) # Outputs: 1
Conclusion
Understanding numerical data types and basic operations is crucial for solving mathematical problems and performing operations on data in Python. Now that you are familiar with these concepts, you can start creating your Python programs. Remember, the best way to learn programming is by doing. So, start coding!
In the next tutorial, we will cover more complex topics. Be sure to practice the concepts you've learned so you're ready to build upon them in the next lesson. Happy coding!