Home
Locate
CS 101
CS 496
Login
Tutors
Marks
About
Send
Close
Add comments:
(status displays here)
Got it!
This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.
nbsp; Note: This appears on each machine/browser from which this site is accessed.
Python: Function arguments
by RS
admin@ycp.powersoftwo.org
c
go
java
js
lua
py
vbs
rkt
scm
pro
1. Python: Function arguments
2. Parameter passing
Here is the Python code.
def myFunction1(i1, x1, s1): print("") print("myFunction1: i1={0:d} x1={1:0.3f} s1=\"{2:s}\"".format(i1,x1,s1)) def myFunction2(i1=78, x1=99.999, s1="default"): print("") print("myFunction2: i1={0:d} x1={1:0.3f} s1=\"{2:s}\"".format(i1,x1,s1)) def myFunction3(*argv): print("") print("myFunction3: *argv") for arg1 in argv: print(arg1) i1 = argv[0] x1 = argv[1] s1 = argv[2] print("myFunction3: i1={0:d} x1={1:0.3f} s1=\"{2:s}\"".format(i1,x1,s1)) def myFunction4(**kwargs): print("") print("myFunction4: **kwargs") for key1, value1 in kwargs.items(): print("\t%s=[%s]" %(key1,value1)) i1 = kwargs["i1"] x1 = kwargs["x1"] s1 = kwargs["s1"] print("myFunction4: i1={0:d} x1={1:0.3f} s1=\"{2:s}\"".format(i1,x1,s1)) myFunction1(123,3.1415,"hello") myFunction2() myFunction3(123,3.1415,"hello") myFunction4(s1="Hello",x1=3.3415,i1=123)
Here is the output of the Python code.
myFunction1: i1=123 x1=3.142 s1="hello" myFunction2: i1=78 x1=99.999 s1="default" myFunction3: *argv 123 3.1415 hello myFunction3: i1=123 x1=3.142 s1="hello" myFunction4: **kwargs s1=[Hello] x1=[3.3415] i1=[123] myFunction4: i1=123 x1=3.341 s1="Hello"
3. End of page
4. Multiple choice questions for this page
10
questions omitted (login required)
by RS
admin@ycp.powersoftwo.org