List operations in Python
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
-
operator is used for subtraction.
Example: a = 10; b = 20; result = a - b
*
operator is used for multiplication.
Example: a = 10; b = 20; result = a * b
/
operator is used for division.
Example: a = 10; b = 20; result = a / b
%
operator is used to get the remainder of an integer division.
Example: a = 10; b = 3; result = a % b
**
operator is used for exponentiation.
Example: base = 2; exponent = 3; result = base ** exponent
//
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
!=
or <>
): Checks if two values are not equal.
Example: a = 10; b = 20; result = a != b
>
): Checks if the first value is greater than the second.
Example: a = 10; b = 5; result = a > b
<
): Checks if the first value is less than the second.
Example: a = 10; b = 15; result = a < b
>=
): Checks if the first value is greater than or equal to the second.
Example: a = 10; b = 10; result = a >= b
<=
): 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
): Returns True if at least one condition is True, otherwise returns False.
Example: a = 10; b = 20; result = (a > 10) or (b > 15)
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
): 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
is not
): Checks if two objects are not the same.
Example: a = 'hello'; b = 'goodbye'; result = a is not b
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
): 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
+=
, -=
, *=
, /=
, etc.): Updates the value of a variable based on an operation.
Example: x = 10; x += 20
is equivalent to x = x + 20
**=
, //=
, etc.): Same as augmented assignment, but for more complex operations.
Example: x = 2; x **= 3
is equivalent to x = x ** 3
={}
): Assigns a dictionary value to a variable.
Example: my_dict = {'key': 'value'}
=[]
, [...]
): Assigns a list value to a variable.
Example: my_list = [1, 2, 3]
=()
, (...)
) : 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
|
): Performs a bitwise OR operation on two integers.
Example: a = 5; b = 3; result = a | b
^
): Performs a bitwise XOR operation on two integers.
Example: a = 5; b = 3; result = a ^ b
~
): Inverts the bits of an integer.
Example: a = 5; result = ~a
<<
): Shifts the bits of an integer to the left.
Example: a = 5; result = a << 1
>>
): 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
-
): Subtracts one complex number from another.
Example: a = 3 + 4j; b = 2 - 5j; result = a - b
*
): Multiplies two complex numbers.
Example: a = 3 + 4j; b = 2 - 5j; result = a * b
/
): 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
-
): Subtracts one matrix from another.
Example: a = [[1, 2], [3, 4]]; b = [[5, 6], [7, 8]]; result = a - b
*
): 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
&
): Returns the intersection of two sets.
Example: set1 = {1, 2, 3}; set2 = {2, 3, 4}; result = set1 & set2
-
): 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)]