Basic Operations

In this section, we delve into the core of programming with Python by exploring its fundamental operations. Mastering these operations is crucial for executing tasks and solving problems with Python, laying the groundwork for more advanced programming concepts.

Arithmetic Operations

Python allows you to perform arithmetic operations using familiar mathematical symbols. These operators enable you to perform calculations on numeric data types such as integers and floats. Here's a quick overview of these operators:

  • Addition (+): Combines two numbers. For example, 3 + 2 yields 5.
  • Subtraction (-): Deducts one number from another. For instance, 5 - 2 gives 3.
  • Multiplication (*): Multiplies two numbers. For example, 4 * 2 results in 8.
  • Division (/): Divides one number by another, returning a float. For instance, 9 / 3 results in 3.0.
  • Floor Division (//): Similar to division, but returns the largest integer less than or equal to the division result. For example, 7 // 2 yields 3.
  • Modulus (%): Returns the remainder of a division. For instance, 7 % 2 results in 1.
  • Exponentiation (**): Raises one number to the power of another. For example, 2 ** 3 gives 8.

These operations can be combined to form more complex expressions. Python follows the standard rules of arithmetic precedence, meaning it evaluates multiplication and division before addition and subtraction, unless parentheses are used to explicitly define the order.

Comparison Operations

Python enables you to compare values using comparison operators. These operators return a Boolean value (True or False), which is fundamental in decision-making processes in programming:

  • Equal (==): Checks if two values are equal. For example, 5 == 5 returns True.
  • Not Equal (!=): Checks if two values are not equal. For instance, 5 != 3 yields True.
  • Greater Than (>): Checks if the left value is greater than the right. For example, 7 > 5 returns True.
  • Less Than (<): Checks if the left value is less than the right. For instance, 3 < 5 yields True.
  • Greater Than or Equal To (>=): Checks if the left value is greater than or equal to the right. For example, 7 >= 7 returns True.
  • Less Than or Equal To (<=): Checks if the left value is less than or equal to the right. For instance, 5 <= 5 yields True.

Logical Operations

Logical operators allow you to combine multiple conditions and are crucial for constructing complex decision-making structures:

  • And (and): Returns True if both operands are True. For example, True and False returns False.
  • Or (or): Returns True if at least one operand is True. For example, True or False yields True.
  • Not (not): Inverts the Boolean value. For instance, not True returns False.

Assignment Operations

Assignment operators are used to assign values to variables. The most common assignment operator is the equal sign (=), which assigns the value on the right to the variable on the left:

x = 10
y = 5

Python also provides shorthand assignment operators that combine a basic operation with assignment:

  • Add and Assign (+=): Adds and then assigns. For example, x += 3 is equivalent to x = x + 3.
  • Subtract and Assign (-=): Subtracts and then assigns. For instance, y -= 2 is equivalent to y = y - 2.
  • Multiply and Assign (*=): Multiplies and then assigns. For example, x *= 2 is equivalent to x = x * 2.
  • Divide and Assign (/=): Divides and then assigns. For instance, y /= 2 is equivalent to y = y / 2.

These fundamental operations form the backbone of Python programming, enabling you to perform calculations, compare values, and make decisions based on conditions. Understanding and mastering these operations will empower you to write more dynamic and functional Python scripts. As you practice, try creating small programs that utilize these operations to reinforce your understanding and build a solid foundation for future learning.

© 2024 ApX Machine Learning