myfunc.py
#!/usr/bin/python3 s = "hello world" # s is declared as a string i = 42 # i is an int def func (j): i = 1 # another variable, i, local print(j * 2) # to func func (2) print(i)
$ python3 myfunc.py 4 42
myfunc2.py
#!/usr/bin/python3 s = "hello world" # s is declared as a string i = 42 # i is an int def func (j): global i # reference global scope i i = 1 # assign to global variable print(j * 2) func (2) print(i)
#!/usr/bin/python3 d = {"spam":2, "ham":1, "eggs":10} print(d["eggs"])
$ python3 py13.py 10
#!/usr/bin/python3 d = {"spam":2, "ham":1, "eggs":10} print(d["eggs"]) d["eggs"]=12 d["bacon"]=1 print(d["bacon"]) print(d)
$ python3 py14.py 10 1 {"spam": 2, "ham": 1, "bacon": 1, "eggs": 12}
d = {}
d["foobar"] = 4
del d["eggs"]
#!/usr/bin/python3 d = {"spam":2, "ham":1, "eggs":10} if "eggs" in d: print("we have some eggs") else: print("we do not have any eggs") if "flour" in d: print("we have some flour") else: print("we do not have any flour")
This document was produced using groff-1.22.