Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Python Numbers

In this Python Numbers tutorial, we'll cover the basics of working with numbers in Python, including numeric data types, arithmetic operations, and some useful numeric functions.

  1. Numeric data types in Python
  2. Arithmetic operations
  3. Useful numeric functions

1. Numeric data types in Python

Python supports several numeric data types, including:

  • int: Integer numbers, which can be positive or negative whole numbers without a decimal point. They have unlimited precision and can grow to any size (limited only by the available memory).

    Example:

    a = 42
    b = -7
    
  • float: Floating-point numbers, which can have a decimal point and support real numbers (both positive and negative). They have limited precision and can represent only a certain number of decimal places.

    Example:

    c = 3.14
    d = -0.001
    
  • complex: Complex numbers, which consist of a real part and an imaginary part, represented as a + bj, where a and b are real numbers and j is the imaginary unit.

    Example:

    e = 2 + 3j
    f = -1.5 + 4j
    

2. Arithmetic operations

Python supports various arithmetic operations, including:

  • Addition (+): Adds two numbers.

    result = 3 + 5
    print(result)  # Output: 8
    
  • Subtraction (-): Subtracts the second number from the first.

    result = 10 - 4
    print(result)  # Output: 6
    
  • Multiplication (*): Multiplies two numbers.

    result = 7 * 6
    print(result)  # Output: 42
    
  • Division (/): Divides the first number by the second, returning a float.

    result = 15 / 2
    print(result)  # Output: 7.5
    
  • Floor Division (//): Divides the first number by the second, returning the largest integer not greater than the result.

    result = 15 // 2
    print(result)  # Output: 7
    
  • Modulus (%): Returns the remainder of the division of the first number by the second.

    result = 15 % 4
    print(result)  # Output: 3
    
  • Exponentiation (**): Raises the first number to the power of the second.

    result = 2 ** 3
    print(result)  # Output: 8
    

3. Useful numeric functions

Python provides several useful numeric functions, such as:

  • abs(): Returns the absolute value of a number.

    result = abs(-5)
    print(result)  # Output: 5
    
  • round(): Rounds a floating-point number to the nearest integer, or to a specified number of decimal places.

    result = round(3.14159, 2)
    print(result)  # Output: 3.14
    
  1. Working with integers in Python:

    • Description: Integers are whole numbers without a decimal point. Python supports basic arithmetic operations on integers.
    • Example Code:
      x = 5
      y = 3
      sum_result = x + y
      product_result = x * y
      print(f"Sum: {sum_result}, Product: {product_result}")
      
  2. Complex numbers in Python:

    • Description: Complex numbers have both a real and imaginary part. They can be represented using j or J.
    • Example Code:
      complex_number = 3 + 4j
      print(f"Real part: {complex_number.real}, Imaginary part: {complex_number.imag}")
      
  3. Python number operations and arithmetic:

    • Description: Python supports standard arithmetic operations such as addition, subtraction, multiplication, division, and modulus.
    • Example Code:
      a = 10
      b = 3
      division_result = a / b
      modulus_result = a % b
      print(f"Division: {division_result}, Modulus: {modulus_result}")
      
  4. Type conversion in Python for numbers:

    • Description: Convert between numeric types using functions like int(), float(), and complex().
    • Example Code:
      x = 5
      y = float(x)
      z = complex(x)
      print(f"Original: {x}, Float: {y}, Complex: {z}")
      
  5. Python math module for advanced numeric operations:

    • Description: The math module provides functions for advanced numeric operations like square root, logarithm, and trigonometry.
    • Example Code:
      import math
      
      sqrt_result = math.sqrt(25)
      log_result = math.log(10)
      sin_result = math.sin(math.radians(30))
      print(f"Square Root: {sqrt_result}, Logarithm: {log_result}, Sine: {sin_result}")
      
  6. Random number generation in Python:

    • Description: Use the random module for generating random numbers and making random choices.
    • Example Code:
      import random
      
      random_number = random.randint(1, 10)
      random_choice = random.choice(['apple', 'banana', 'orange'])
      print(f"Random Number: {random_number}, Random Choice: {random_choice}")
      
  7. Numeric comparisons and relational operators in Python:

    • Description: Use relational operators (<, >, <=, >=, ==, !=) for numeric comparisons.
    • Example Code:
      x = 5
      y = 8
      greater_than = x > y
      equal_to = x == y
      print(f"Greater Than: {greater_than}, Equal To: {equal_to}")
      
  8. Handling numerical errors and exceptions in Python:

    • Description: Handle errors that may occur during numeric operations using try-except blocks.
    • Example Code:
      try:
          result = 10 / 0
      except ZeroDivisionError as e:
          print(f"Error: {e}")