-1

Is it possible to modify variables in if statements, loops, and function calls in Python like you can in C and Java?

Ex:

i=0
while((i+=1)<10): #invalid syntax
    print(i) 

If not why is that?

thedeg123
  • 408
  • 2
  • 5
  • 11

1 Answers1

1

as long as the datatype is mutable

def modified(data):
    data['a'] = 5

x = {'b':7}
modified(x)
print(x)


def increment_a(data):
    data['a'] += 1
    return data['a']

x = {'a':1}
while increment_a(x) < 10:
    print(x)

however strings and integers are immutable

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179