List operations in Python

May 142 Published: 06/19/2024

List operations in Python

Here are the list of operations in Python:

Arithmetic Operations

Addition: The + operator is used for addition.

Example: a = 10; b = 20; result = a + b

Subtraction: The - operator is used for subtraction.

Example: a = 10; b = 20; result = a - b

Multiplication: The * operator is used for multiplication.

Example: a = 10; b = 20; result = a * b

Division: The / operator is used for division.

Example: a = 10; b = 20; result = a / b

Modulus (remainder): The % operator is used to get the remainder of an integer division.

Example: a = 10; b = 3; result = a % b

Exponentiation: The ** operator is used for exponentiation.

Example: base = 2; exponent = 3; result = base ** exponent

Floor Division (integer division): The // operator is used to get the floor of an integer division.

Example: a = 10; b = 3; result = a // b

Comparison Operations

Equal To (==): Checks if two values are equal.

Example: a = 10; b = 10; result = a == b

Not Equal To (!= or <>): Checks if two values are not equal.

Example: a = 10; b = 20; result = a != b

Greater Than (>): Checks if the first value is greater than the second.

Example: a = 10; b = 5; result = a > b

Less Than (<): Checks if the first value is less than the second.

Example: a = 10; b = 15; result = a < b

Greater Than or Equal To (>=): Checks if the first value is greater than or equal to the second.

Example: a = 10; b = 10; result = a >= b

Less Than or Equal To (<=): Checks if the first value is less than or equal to the second.

Example: a = 10; b = 5; result = a <= b

Logical Operations

And (and): Returns True if both conditions are True, otherwise returns False.

Example: a = 10; b = 20; result = (a > 10) and (b > 15)

Or (or): Returns True if at least one condition is True, otherwise returns False.

Example: a = 10; b = 20; result = (a > 10) or (b > 15)

Not (not): Returns the opposite of a boolean value.

Example: a = 10; result = not(a > 10)

Membership Operations

In (in): Checks if a value is in a sequence (e.g., list, tuple).

Example: my_list = [1, 2, 3]; number = 2; result = number in my_list

Not In (not in): Checks if a value is not in a sequence.

Example: my_list = [1, 2, 3]; number = 4; result = number not in my_list

Identity Operations

Is (is): Checks if two objects are the same (i.e., identical).

Example: a = 'hello'; b = 'hello'; result = a is b

IsNot (is not): Checks if two objects are not the same.

Example: a = 'hello'; b = 'goodbye'; result = a is not b

IsInstance (isinstance): Checks if an object is an instance of a class or type.

Example: my_object = MyClass(); my_class = MyClass; result = isinstance(my_object, my_class)

IsNotInstance (isnotinstance): The opposite of isinstance.

Example: my_object = MyClass(); my_other_class = OtherClass; result = not(isinstance(my_object, my_other_class))

Assignment Operations

Simple Assignment (=): Assigns a value to a variable.

Example: x = 10

Augmented Assignment (+=, -= , *= , /= , etc.): Updates the value of a variable based on an operation.

Example: x = 10; x += 20 is equivalent to x = x + 20

Compound Assignment (**=, //= , etc.): Same as augmented assignment, but for more complex operations.

Example: x = 2; x **= 3 is equivalent to x = x ** 3

Dictionary Assignment (={}): Assigns a dictionary value to a variable.

Example: my_dict = {'key': 'value'}

List Assignment (=[], [...]): Assigns a list value to a variable.

Example: my_list = [1, 2, 3]

Tuple Assignment (=(), (...)) : Assigns a tuple value to a variable.

Example: my_tuple = (1, 2, 3)

Bitwise Operations

Bitwise And (&): Performs a bitwise AND operation on two integers.

Example: a = 5; b = 3; result = a & b

Bitwise Or (|): Performs a bitwise OR operation on two integers.

Example: a = 5; b = 3; result = a | b

Bitwise Xor (^): Performs a bitwise XOR operation on two integers.

Example: a = 5; b = 3; result = a ^ b

Bitwise Not (~): Inverts the bits of an integer.

Example: a = 5; result = ~a

Left Shift (<<): Shifts the bits of an integer to the left.

Example: a = 5; result = a << 1

Right Shift (>>): Shifts the bits of an integer to the right.

Example: a = 5; result = a >> 1

Complex Number Operations

Complex Addition (+): Adds two complex numbers.

Example: a = 3 + 4j; b = 2 - 5j; result = a + b

Complex Subtraction (-): Subtracts one complex number from another.

Example: a = 3 + 4j; b = 2 - 5j; result = a - b

Complex Multiplication (*): Multiplies two complex numbers.

Example: a = 3 + 4j; b = 2 - 5j; result = a * b

Complex Division (/): Divides one complex number by another.

Example: a = 3 + 4j; b = 2 - 5j; result = a / b

Matrix Operations

Matrix Addition (+): Adds two matrices.

Example: a = [[1, 2], [3, 4]]; b = [[5, 6], [7, 8]]; result = a + b

Matrix Subtraction (-): Subtracts one matrix from another.

Example: a = [[1, 2], [3, 4]]; b = [[5, 6], [7, 8]]; result = a - b

Matrix Multiplication (*): Multiplies two matrices.

Example: a = [[1, 2], [3, 4]]; b = [[5, 6], [7, 8]]; result = a * b

Set Operations

Union (|): Returns the union of two sets.

Example: set1 = {1, 2, 3}; set2 = {2, 3, 4}; result = set1 | set2

Intersection (&): Returns the intersection of two sets.

Example: set1 = {1, 2, 3}; set2 = {2, 3, 4}; result = set1 & set2

Difference (-): Returns the difference between two sets.

Example: set1 = {1, 2, 3}; set2 = {2, 3, 4}; result = set1 - set2

These are some of the basic operations available in Python.

How to create lists in Python?

I'm happy to help! However, I must follow the rules you provided. Hence, I'll respond in Simplified Chinese.

my_list = []

my_tuple = ('a', 'b', 'c')

my_list = list(my_tuple)

my_list = ['a', 'b']

my_tuple = ('c', 'd')

my_list.extend(my_tuple)

my_list = ['a', 'b']

my_list.append('c')

my_list = ['a', 'b']

my_list.insert(1, 'c')

my_list = ['c', 'a', 'b']

my_list.sort()

my_list.reverse()

my_list = [i**2 for i in range(5)]