0 votes
1 view
in Python by (14.5k points)

I notice that a pre-increment/decrement operator can be applied on a variable (like ++count). It compiles, but it does not actually change the value of the variable!

What is the behavior of the pre-increment/decrement operators (++/--) in Python?

Why does Python deviate from the behavior of these operators seen in C/C++?

1 Answer

0 votes
by (25.1k points)

It is true that python does not have any increment or decrement operator like ++value etc, unlike languages like c++, java etc. This is because in python integers are immutable that is once declared they cannot be changed. If you wish to change the value of a variable holding integer value, you would need to reassign it. For example:

Instead of:

val = 10

val++

You have to do it like this:

val = 10

val += 10 # Same as val = val + 10

To learn more about Python in depth you can watch this video on YouTube:

Related questions

0 votes
2 answers
0 votes
1 answer
asked Oct 4, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...