Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k 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 Mar 1, 2021 in Python by Rekha (2.2k points)
0 votes
2 answers
asked Oct 4, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...