YouTip LogoYouTip

Python3 Basic Operators

Python3.x Python3 Operators

\\n\\n
\\n\\n

What is an operator?

\\n\\nThis chapter mainly explains Python's operators.\\n\\nTake a simple example:\\n\\n4 + 5 = 9\\nIn the example, 4 and 5 are called operands, and + is called the operator.\\n\\nThe Python language supports the following types of operators:\\n\\n\\n\\nNext, let's learn Python's operators one by one.\\n\\n
\\n\\n

Python Arithmetic Operators

\\n\\nAssuming variable a=10 and variable b=21:\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n
OperatorDescriptionExample
+Add - Adds two objectsa + b outputs 31
-Subtract - Gets a negative number or subtracts one number from anothera - b outputs -11
*Multiply - Multiplies two numbers or returns a string repeated several timesa * b outputs 210
/Divide - x divided by yb / a outputs 2.1
%Modulo - Returns the remainder of divisionb % a outputs 1
**Power - Returns x raised to the power of ya**b is 10 to the power of 21
//Floor Division - Returns the integer part of the quotient (rounds down)>>> 9//24>>> -9//2-5
\\n\\nThe following example demonstrates the operation of all arithmetic operators in Python:\\n\\n

Example(Python 3.0+)

\\n\\n
a = 21\\nb = 10\\nc = 0\\n\\nc = a + b\\nprint("1 - c The value is:", c)\\n\\nc = a - b\\nprint("2 - c The value is:", c)\\n\\nc = a * b\\nprint("3 - c The value is:", c)\\n\\nc = a / b\\nprint("4 - c The value is:", c)\\n\\nc = a % b\\nprint("5 - c The value is:", c)\\n\\na = 2\\nb = 3\\nc = a**b\\nprint("6 - c The value is:", c)\\n\\na = 10\\nb = 5\\nc = a//b\\nprint("7 - c The value is:", c)\\n
\\n\\nOutput of the above example:\\n\\n
1 - c The value is: 31\\n2 - c The value is: 11\\n3 - c The value is: 210\\n4 - c The value is: 2.1\\n5 - c The value is: 1\\n6 - c The value is: 8\\n7 - c The value is: 2\\n
\\n\\n
\\n\\n

Python Comparison Operators

\\n\\nAssuming variable a is 10 and variable b is 20:\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n
OperatorDescriptionExample
==Equal - Compares whether objects are equal(a == b) returns False.
!=Not Equal - Compares whether two objects are not equal(a != b) returns True.
>Greater than - Returns whether x is greater than y(a > b) returns False.
<Less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is respectively equivalent to the special variables True and False. Note the capitalization of these variable names.(a < b) returns True.
>=Greater than or equal - Returns whether x is greater than or equal to y.(a >= b) returns False.
<=Less than or equal - Returns whether x is less than or equal to y.(a <= b) returns True.
\\n\\nThe following example demonstrates the operation of all comparison operators in Python:\\n\\n

Example(Python 3.0+)

\\n\\n
a = 21\\nb = 10\\nc = 0\\n\\nif (a == b):\\n    print("1 - a equal to b")\\nelse:\\n    print("1 - a Noequal to b")\\n\\nif (a != b):\\n    print("2 - a Noequal to b")\\nelse:\\n    print("2 - a equal to b")\\n\\nif (a < b):\\n    print("3 - a less than b")\\nelse:\\n    print("3 - a Greater than or equal to b")\\n\\nif (a > b):\\n    print("4 - a greater than b")\\nelse:\\n    print("4 - a Less than or equal to b")\\n\\na = 5\\nb = 20\\nif (a <= b):\\n    print("5 - a Less than or equal to b")\\nelse:\\n    print("5 - a greater than b")\\n\\nif (b >= a):\\n    print("6 - b greater than or equal to a")\\nelse:\\n    print("6 - b less than a")\\n
\\n\\nOutput of the above example:\\n\\n
1 - a Noequal to b\\n2 - a Noequal to b\\n3 - a Greater than or equal to b\\n4 - a greater than b\\n5 - a Less than or equal to b\\n6 - b greater than or equal to a\\n
\\n\\n
\\n\\n

Python Assignment Operators

\\n\\nAssuming variable a is 10 and variable b is 20:\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n
OperatorDescriptionExample
=Simple assignment operatorc = a + b assigns the result of a + b to c
+=Addition assignment operatorc += a is equivalent to c = c + a
-=Subtraction assignment operatorc -= a is equivalent to c = c - a
*=Multiplication assignment operatorc *= a is equivalent to c = c * a
/=Division assignment operatorc /= a is equivalent to c = c / a
%=Modulo assignment operatorc %= a is equivalent to c = c % a
**=Exponentiation assignment operatorc **= a is equivalent to c = c ** a
//=Floor division assignment operatorc //= a is equivalent to c = c // a
:=Walrus operator, the main purpose of this operator is to assign a value and return that value simultaneously within an expression. Operator added in Python 3.8.In this example, the assignment expression can avoid calling len() twice: if (n := len(a)) > 10: print(f"List is too long ({n} elements, expected
← C Break StatementC Nested Loops β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.