Operator in Python
Now that I’m really re-learning about python, I’m trying to go back to remembering and practicing python in data science or analysis.
And this is my note for me to study and maybe you can study together with me. FYI this is the first time I use English for my post.
In this post, I study the operators available in Python. Maybe some of you already know In Python, the operators are divided into 6 groups:
Arithmetic Operators
As you already know this operator is used on numeric data types, to perform simple math operations. Operator Symbol
Operator Symbols : + — */ % ** //
Assignment Operators
Used to declare a value directly to a variable
Operator Symbols : +=, — =, *=, /= ,%=, **= , //=
Example:
x = 3x + = 2 is equivalent to x = x + 2
will change the x value to 5
x **= 2 is equivalent to x = x ** 2
will change the x value to 9
Comparison Operator
Used to compare two values, here are examples of comparison operators.
Operator Symbols: ==, !=, >, <, >=,<=
Example:
34! = 33 will output: True because it is true that 34 does not equal 33
33! = 33 will produce the output: False because 33 is equal to 33
Logical Operators
Used to combine several truth values for a logical statement
Operator Symbols: and,or, not
Identity Operators
Can be used to compare the identities of two variables.
Operator Symbols: is, is not
In general, the identity operator is often used in conjunction with the type () function, the type () function accepts an object and returns the data type of that object. Examples of using the identity operator and the type () function.
Membership Operators
Can be used to examine members of a sequence/set data type, membership operator.
Operator Symbols: in, not in
x = [“Dian”, “Eka”, “Josh”]
y = “Josh”
z = “Dodi”
print (y in x) will return True
print (z in x) will return False
x = [“Dian”, “Eka”, “Josh”]
y = “Josh”
z = “Dodi”
print (y in not x) will return False
print (z in not x) will return True