Table of Contents

Data Types, Operators and Expressions in Python

Table of Contents

Data Types in Python Programming

In Python, there are several built-in data types that you can use to represent different types of data. Here are
some of the most commonly used data types in Python:

  • Numeric types: These include integers, floats, and complex numbers.
    • Integers are whole numbers, such as 1, 2, -3, and 0.
    • Floats are numbers with a decimal point, such as 3.14, 1.0, and -2.5.
    • Complex numbers are numbers with a real and imaginary part, such as 1+2j and 3-4j.
  • Boolean type: This data type represents True or False values.
  • String type: This data type represents a sequence of characters enclosed in quotes. You can use single quotes
    (‘…’) or double quotes (“…”) to define a string.
  • List type: This data type represents an ordered sequence of values, enclosed in square brackets. Each value in a
    list is separated by a comma.
  • Tuple type: This data type is similar to a list, but it is immutable (i.e., it cannot be changed once it is
    created). Tuples are enclosed in parentheses.
  • Set type: This data type represents an unordered collection of unique values, enclosed in curly braces. Each
    value in a set is separated by a comma.
  • Dictionary type: This data type represents a collection of key-value pairs, enclosed in curly braces. Each
    key-value pair is separated by a comma, and the key and value are separated by a colon.

In Python, variables can hold any data type, and you do not need to specify the data type of a variable when you
create it. Python automatically assigns the appropriate data type based on the value that is assigned to the
variable. For example, if you assign an integer value to a variable, Python will create an integer variable; if you
assign a string value to a variable, Python will create a string variable.


Operators in Python Programming

Operators are symbols or keywords in Python that perform some operation on one or more operands, which are values or
variables. Python supports a wide range of operators, including arithmetic operators, comparison (relational)
operators, logical operators, bitwise operators, assignment operators, membership operators, identity operators.

Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on numeric values or variables. Python supports the
following arithmetic operators:

  • + Addition
  • – Subtraction
  • * Multiplication
  • / Division
  • % Modulo (returns the remainder of a division)
  • ** Exponentiation
  • // Floor division (returns the quotient of a division rounded down to the nearest integer)

Here is an example:

x = 10
y = 3
print(x + y)    # Output: 13
print(x - y)    # Output: 7
print(x * y)    # Output: 30
print(x / y)    # Output: 3.3333333333333335
print(x % y)    # Output: 1
print(x ** y)   # Output: 1000
print(x // y)   # Output: 3

Comparison operators

Comparison operators are used to compare two values or variables and return a Boolean value (True or False). Python supports the following comparison operators:

  • == Equal to
  • != Not equal to
  • < Less than
  • > Greater than
  • <= Less than or equal to
  • >= Greater than or equal to

Here is an example:

x = 10
y = 3
print(x == y)   # Output: False
print(x != y)   # Output: True
print(x < y)    # Output: False
print(x > y)    # Output: True
print(x <= y)   # Output: False
print(x >= y)   # Output: True

Logical operators

Logical operators are used to combine multiple Boolean expressions and return a Boolean value. Python supports the following logical operators:

  • and Returns True if both operands are True
  • or Returns True if at least one operand is True
  • not Returns the opposite of the operand’s value

Here is an example:

x = 10
y = 3
z = 5
print(x > y and x > z)  # Output: True
print(x > y or x < z)   # Output: True
print(not x == y)       # Output: True

Bitwise operators

Bitwise operators are used to perform bitwise operations on binary numbers. Python supports the following bitwise operators:

  • & Bitwise AND
  • | Bitwise OR
  • ^ Bitwise XOR
  • ~ Bitwise NOT
  • << Bitwise left shift
  • >> Bitwise right shift

Here is an example:

x = 10
y = 3
print(x & y)   # Output: 2 (binary 1010 & 0011 = 0010)
print(x | y)   # Output: 11 (binary 1010 | 0011 = 1011)
print(x ^ y)   # Output: 9 (binary 1010 ^ 0011 = 1001)
print(~x)      # Output: -11 (binary ~1010 = -1011)
print(x << 2)  # Output: 40 (binary 1010 << 2 = 101000)
print(x >> 1)  # Output: 5 (binary 1010

Assignment operators

Assignment operator = is used to assign a value to a variable. It is a binary operator that takes the value on the right and assigns it to the variable on the left. Python supports the following assignment operators:

  • =
  • +=
  • -=
  • *=
  • /=
  • %=
  • //=
  • **=
  • &=
  • |=
  • ^=
  • >>=
  • <<=

Here is an example:

x = 10
x += 5   # Equivalent to x = x + 5
x *= 2   # Equivalent to x = x * 2

Membership operators

The membership operators in Python are used to test if a value is a member of a sequence (such as a string, list, or tuple). Python has two membership operators:

  • in
  • not in

Here is an example for in:

my_list = [1, 2, 3, 4, 5]
if 3 in my_list:
    print("3 is present in the list")

Here is an example for not in:

my_string = "Hello, World!"
if "a" not in my_string:
    print("The string does not contain the letter 'a'")

Identity operators

The identity operators in Python are used to compare the memory locations of two objects. In other words, they check if two variables refer to the same object in memory. Python has two identity operators:

  • is
  • is not

Here is an example for is:

x = [1, 2, 3]
y = [1, 2, 3]
z = x
if x is y:
    print("x and y refer to the same object")
if x is z:
    print("x and z refer to the same object")

Here is an example for is not:

x = [1, 2, 3]
y = [1, 2, 3]
z = x
if x is not y:
    print("x and y do not refer to the same object")
if y is not z:
    print("y and z do not refer to the same object")

Expressions and Order of Evaluation in Python

In Python, an expression is a combination of values, variables, operators, and function calls that can be evaluated to produce a result. For example, 2 + 3 is an expression that evaluates to 5.

Python follows the standard order of operations for evaluating expressions. The order of evaluation is:

  1. Parentheses: Expressions inside parentheses are evaluated first.
  2. Exponentiation: Exponentiation operators (**) are evaluated next.
  3. Multiplication and Division: Multiplication (*) and division (/) operators are evaluated from left to right.
  4. Addition and Subtraction: Addition (+) and subtraction (-) operators are evaluated from left to right.

When there are multiple operators of the same precedence, they are evaluated from left to right. For example, in the expression 2 + 3 * 4, the multiplication operator has higher precedence than the addition operator, so 3 * 4 is evaluated first to give 12, and then 2 + 12 is evaluated to give 14.

You can use parentheses to change the order of evaluation. For example, in the expression (2 + 3) * 4, the addition inside the parentheses is evaluated first to give 5, and then 5 * 4 is evaluated to give 20.

It’s important to keep the order of evaluation in mind when writing complex expressions, as it can affect the outcome of the calculation.

Category
Tags

Copyright 2023-24 © Open Code