python reference variable code example

Example 1: declaring variables in python

my_name = "your name here "# you can add perenthisis
print(my_name)

Example 2: python send object reference to another function and edit property

class Test:
  a = 1

def f(obj):
  obj.a = 10

t = Test()
f(t)

# t.a should be 10

Example 3: python variable

Python variable

Example 4: how to create a variable in python

variable1 = "value" # string
variable2 = 1000000 # integer
variable3 = 10000.0 # real/float
variable4 = True # boolean: True or False

Example 5: how to define variable in python

saving = 100
print (saving)

Example 6: python pass by reference

# objects are passed by reference, but
# its references are passed by value

myList = ['foo', 'bar']

def modifyList(l):
  l.append('qux') # modifies the reference
  l = ['spam', 'eggs'] # replaces the reference
  l.append('lol') # modifies the new reference

modifiyList(myList)

print(myList) # ['foo', 'bar', 'qux']