GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing codescracker.com — Operators in Python with Examples
Operators in Python with ExamplesOperators in Python work the same as in mathematics, that is, operators such as +, -, *, etc. are used to perform operations. Now these are the two questions that arise in our minds:
The answers to these two questions are:
For example, the following program demonstrates the addition operation (using +) on two variables and the subtraction operation (using -) on two values: a = 20 b = 10 c = a + b print(c) res = 100 - 50 print(res) This program produces the output shown in the snapshot given below: As you can see, the addition operation is performed on a and b, and whatever the result is, it gets initialized to the "c" variable. And in the second case, the subtraction operation is performed directly on two values, which are 100 and 50, and again, whatever the result value comes out as, it gets initialized to the "res" variable. Note: For operator precedence and operator modules, refer to their separate tutorials. Types of Operators in PythonOperators that are used in Python are divided into seven categories, as listed here:
Now let's discuss all these operators in brief with well-designed example programs for each category. Arithmetic operators in PythonTo perform arithmetical operations in a Python program, we have arithmetic operators. The table given below lists all the arithmetic operators available, along with syntax to use and examples. The example is given with the variables' values as a = 5 and b = 3:
Important: The difference between the / (division) and // (floor division) operators is that the division operator divides and returns the exact division result value, where the floor division operator divides and then takes the floor value of the result value. For example, 5/3 gives 1.666666666667, whereas 5//3 gives 1. Here the floor value indicates an integer value (a whole number) that is less than or equal to the original result. Note: When the ** operator applies to a variable or a value, for example, a ** b, it gets treated as ab. Therefore, 5 ** 3 gets treated as 53, which gives 125 as its result. The program given below uses all the arithmetic operators. This program is created to clear all your remaining doubts (if any): a = 5 b = 3 c = a + b print(a, "+", b, "=", c) c = a - b print(a, "-", b, "=", c) c = a * b print(a, "*", b, "=", c) c = a / b print(a, "/", b, "=", c) c = a % b print(a, "%", b, "=", c) c = a ** b print(a, "*", b, "=", c) c = a // b print(a, "//", b, "=", c) The snapshot given below shows the sample output produced by this program: Assignment operators in PythonTo assign (set) or re-assign the new value to a variable in Python programming, we have another category of operator named the assignment operator. The table given below lists all those operators used for assigning purposes, along with their syntax and examples.
The program given below implements all the above operators. This program was designed to help you better understand assignment operators by demonstrating the following: a = 5 b = a print("Simple Assign Result:", b) a = 5 b = 0 b += a print("Add then Assign Result:", b) a = 5 b = 12 b -= a print("Subtract then Assign Result:", b) a = 5 b = 2 b *= a print("Multiply then Assign Result:", b) a = 5 b = 14 b /= a print("Divide then Assign Result:", b) a = 5 b = 27 b %= a print("Modulo then Assign Result:", b) a = 5 b = 14 b //= a print("Floor Divide then Assign Result:", b) a = 5 b = 2 b **= a print("Exponent then Assign Result:", b) a = 10 b = 4 b &= a print("Bitwise AND then Assign Result:", b) a = 10 b = 4 b |= a print("Bitwise OR then Assign Result:", b) a = 10 b = 4 b ^= a print("Bitwise XOR then Assign Result:", b) a = 1 b = 10 b >>= a print("Bitwise Right Shift then Assign Result:", b) a = 1 b = 10 b <<= a print("Bitwise Left Shift then Assign Result:", b) Here is the output that this Python program of assignment operators produced: In the assignment operator, the expression: b += a is basically the short version of: b = b + a Comparison operators in PythonComparison operators are used in Python programming to compare two variables or directly values.These operators are used to compare two entities. The table given below shows all the comparison operators along with the syntax to use and an example. The example is taken with two variables, namely, a and b. The values of these two variables are a = 12 and b = 7.
The program given below uses all these comparison operators. Since the comparison operators are used to compare two values, therefore, on printing the comparison result, we'll only get the output in the form of "True" or "False." For example, the statement "a < b" returns True if the value of a is less than b, otherwise False gets returned. Let's have a look at the example given below: a = 12 b = 7 print("\n---------", a, "==", b, "?--------") print(a == b) print("\n---------", a, "!=", b, "?--------") print(a != b) print("\n---------", a, ">", b, "?--------") print(a > b) print("\n---------", a, "<", b, "?--------") print(a < b) print("\n---------", a, ">=", b, "?--------") print(a >= b) print("\n---------", a, "<=", b, "?--------") print(a <= b) Here is the output produced: Logical operators in PythonTo connect two or more expressions and evaluate their logical output, we have another operator category named the logical operator. The table given below shows all the logical operators along with their syntax and output. The initial values of three variables, namely a, b, and c, are a = 12, b = 7, and c = 9.
Now the example given below uses all three logical operators: a = 12 b = 7 c = 9 print(a > b and a < c) print(a > b or a < c) print(not(a > b)) And here is the output produced: Bitwise operators in PythonThis section includes all the bitwise operators that can be used in the Python language. To learn how all of these Bitwise operators perform operations, see Bitwise Operators with Example, which covers everything about Bitwise operators in a nutshell. For now, the table given below shows all bitwise operators along with syntax and output. The example taken with two variables, namely a and b, whose values are supposed to be a = 10 and b = 3, is
The example given below uses all these bitwise operators: a = 10 b = 3 print(a, "&", b, "=", a & b) print(a, "|", b, "=", a | b) print(a, "^", b, "=", a ^ b) print("˜", a, "=", ˜a) print(a, ">>", b, "=", a >> b) print(a, "<<", b, "=", a << b) The output that this program generates is: Identity operators in PythonTo determine whether a variable or a value is of a certain type or class or not in Python programming, we've got another category of operators called the identity operator. That is basically used to identify variables or values. The table given below lists identity operators that can be used in Python, along with syntax and examples. The example is taken with a and b variables whose values are a=[1, 2, 3], b=[4, 5]:
The example given below implements these two identity operators available in Python: a = [1, 2, 3] b = [4, 5] c = a print(a is b) print(a is c) print(a is not b) print(a is not c) The image below displays an example of the output this Python program produced: Membership operators in PythonThis is the last category of operator used in Python. This category of operators is basically used to check whether a specific value is a member of any specific list, string, or tuple or not. The table given below lists membership operators with syntax, examples, and output. The example taken with a variable named a whose initial value is a = [1, 2, 3] is:
Let's have a look at the example given below to better understand the above two membership operators: a = [1, 2, 3] print(2 in a) print(4 in a) print(2 not in a) print(4 not in a) The screenshot provided below displays a sample of the output this program produced: Let's have a look at another example that shows the famous use of the membership operator (the "in" operator) in Python programming: a = ["python", "operators", "tutorial"] print("Enter an element to check for existence in list: ") elem = input() if elem in a: print("\nIt is available") else: print("\nIt is not available") And here is the sample output produced by this program after supplying "Python" as input: More ExamplesHere is a list of some more examples that use operators in Python:
« Previous Tutorial Next Tutorial » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |