Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "2*2" and "2**2" in Python?

Tags:

python

syntax

What is the difference between the following codes?

code1:

var=2**2*3 

code2:

var2=2*2*3 

I see no difference. This raises the following question.

Why is the code1 used if we can use code2?

like image 728
Léo Léopold Hertz 준영 Avatar asked Jun 25 '09 16:06

Léo Léopold Hertz 준영


People also ask

What does *= mean in Python?

*= Multiply AND. It multiplies right operand with the left operand and assign the result to left operand. c *= a is equivalent to c = c * a. /= Divide AND. It divides left operand with the right operand and assign the result to left operand.

What is the difference between and \\ in Python?

There is no difference at runtime. The only difference between the two types of quotes is the one you have already pointed out: Single quotes need to be escaped inside single quoted string literals but not inside double-quoted string literals.

What is double multiplication in Python?

For numeric data types double asterisk (**) is defined as exponentiation operator >>> a=10; b=2 >>> a**b 100 >>> a=1.5; b=2.5 >>> a**b 2.7556759606310752 >>> a=3+2j >>> b=3+5j >>> a**b (-0.7851059645317211+2.350232331971346j)

What is the difference between i += 1 and i i 1 in Python?

i+=1 does the same as i=i+1 there both incrementing the current value of i by 1. Do you mean "i += 1"? If so, then there's no difference.


1 Answers

Try:

2**3*2 

and

2*3*2 

to see the difference.

** is the operator for "power of". In your particular operation, 2 to the power of 2 yields the same as 2 times 2.

like image 188
OscarRyz Avatar answered Sep 24 '22 12:09

OscarRyz