Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Basic Operators and Expressions in Python

A comprehensive guide to basic operators and expressions in Python programming, covering variables, data types, arithmetic operations, comparison, logical, assignment, and membership testing. …


Updated May 29, 2023

A comprehensive guide to basic operators and expressions in Python programming, covering variables, data types, arithmetic operations, comparison, logical, assignment, and membership testing.

Definition of the Concept

In Python, basic operators and expressions are used to perform various calculations and comparisons on variables. These operators allow you to manipulate and combine values to produce new results. Understanding these fundamental concepts is essential for working with variables and data types in Python.

Step-by-Step Explanation: Variables and Data Types

Before diving into operators and expressions, it’s crucial to grasp the basics of variables and data types in Python:

  • Variables: A variable is a name given to a location in memory where you can store a value. In Python, you don’t need to declare variables before using them.
  • Data Types: Python has several built-in data types, including:
    • Integers (int): Whole numbers, e.g., 1, 2, 3, etc.
    • Floating Point Numbers (float): Decimal numbers, e.g., 3.14, -0.5, etc.
    • Strings (str): Text values, e.g., “hello”, ‘hello’, etc.
    • Boolean Values (bool): True or False
    • Lists (list): Ordered collections of items, e.g., [1, 2, 3], [‘a’, ‘b’, ‘c’]

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numbers. Python supports the following arithmetic operators:

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation (e.g., 2**3 = 8)
// Floor division (e.g., 7//2 = 3)
% Modulus (remainder of division, e.g., 17 % 5 = 2)

Example Code:

x = 10
y = 2

# Addition
result1 = x + y
print(result1)  # Output: 12

# Subtraction
result2 = x - y
print(result2)  # Output: 8

# Multiplication
result3 = x * y
print(result3)  # Output: 20

Comparison Operators

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

Operator Description
== Equality (e.g., 5 == 5, ‘a’ == ‘A’)
!= Inequality (e.g., 5 != 3, ‘a’ != ‘b’)
< Less than (e.g., 5 < 10, ‘apple’ < ‘banana’)
> Greater than (e.g., 10 > 5, ‘banana’ > ‘apple’)
<= Less than or equal to (e.g., 5 <= 5, ‘a’ <= ‘b’)
>= Greater than or equal to (e.g., 10 >= 5, ‘b’ >= ‘a’)

Example Code:

x = 5
y = 3

# Equality
result1 = x == y
print(result1)  # Output: False

# Inequality
result2 = x != y
print(result2)  # Output: True

Logical Operators

Logical operators are used to combine Boolean values.

Operator Description
and Logical AND (e.g., True and True, False and True)
or Logical OR (e.g., True or False, False or False)
not Logical NOT (e.g., not True, not False)

Example Code:

x = True
y = False

# Logical AND
result1 = x and y
print(result1)  # Output: False

# Logical OR
result2 = x or y
print(result2)  # Output: True

# Logical NOT
result3 = not x
print(result3)  # Output: False

Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description
= Simple assignment (e.g., x = 5, y = ‘hello’)
+= Addition and assignment (e.g., x += 2)
-= Subtraction and assignment (e.g., x -= 1)
*= Multiplication and assignment (e.g., x *= 3)
/= Division and assignment (e.g., x /= 4)

Example Code:

x = 5

# Simple assignment
x = 10
print(x)  # Output: 10

# Addition and assignment
x += 2
print(x)  # Output: 12

Membership Operators

Membership operators are used to check if a value is present in a collection (e.g., list, tuple).

Operator Description
in Check membership (e.g., ‘apple’ in [‘apple’, ‘banana’])
not in Check non-membership (e.g., ‘grape’ not in [‘apple’, ‘banana’])

Example Code:

fruits = \['apple', 'banana', 'orange'\]

# Membership check
result1 = 'banana' in fruits
print(result1)  # Output: True

# Non-membership check
result2 = 'grape' not in fruits
print(result2)  # Output: True

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp