print(4+2*3) # Output 10
print(4+(2*3)) # Output 10
print((4+2)*3) # Output 18
In the first line the output is 10 ( not 18 ) as multiplication is first done and then the addition is performed. In next two lines the operation within the Parentheses is executed first. This action is done based on the precedence to be followed while performing actions using operators. Operator | Details |
---|---|
( ) | Parentheses ( By grouping the expressions the precedence can change ) |
** | Exponent |
+x , -x , ~x | Positive, negative, bitwise NOT |
* , @ , / , // , % | Multiplication, matrix multiplication, division, floor division, remainder |
+ ,- | Addition and subtraction |
<< , >> | Shifts |
& | Bitwise AND |
^ | Bitwise XOR |
| | Bitwise OR |
in , not in , is , is not , < , <= , > , >= , != , == | Comparisons, including membership tests and identity tests |
not x | Boolean NOT |
and | Boolean AND |
or | Boolean OR |
if – else | Conditional Expression |
lambda | Lambda expression |
:= | Assignment expression |
P
,E
,MD
,AS
x+y+z*a
is same as (x+y)+(z*a)
.
math=40
english=50
avg=45
if math>=40 or english>=50 and avg>=50:
print("Passed")
else:
print("Failed")
Output is
Passed
Due to the higher Precedence of and following grouping is done.
if math>40 or (english > = 50 and avg > 50)
The actual grouping should be like this to get correct result.
(if math>40 or english > = 50) and avg > 50
print(10/2*6) # output is 30.0
print(24/6//2)# output is 2.0
Operators
All Built in Functions in Python dir() ord()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.