def happyBirthday(FirstName="FN", LastName="LN"):
print("Happy Birthday, dear " + FirstName + " " + LastName + ".")
happyBirthday()
Will print: Happy Birthday, dear FN LN.
happyBirthday("FN1"): will print Will print: Happy Birthday, dear FN1 LN.
happyBirthday("FN1", "LN1"): will print Will print: Happy Birthday, dear FN1 LN1.
Wednesday, August 12, 2015
Function in python
def happyBirthday(person): print("Happy Birthday, dear " + person + ".")
Note: parenthesis is must to : A. define a function, B. use a function
def a():
print("a")
a()
variable length argument in Python: **kw
http://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/
http://stackoverflow.com/questions/287085/what-do-args-and-kwargs-mean
Old Python : it is not necessary to write *args or **kwargs. Only the * (aesteric) is necessary. You could have also written *var and **vars. Writing *args and **kwargs is just a convention. So now lets take a look at *args first.
New Python: * and ** are different
Usage of *args
*args and **kwargs are mostly used in function definitions. *args and **kwargs allow you to pass a variable number of arguments to a function. What does variable mean here is that you do not know before hand that how many arguments can be passed to your function by the user so in this case you use these two keywords. *args is used to send a non-keyworded variable length argument list to the function.
http://stackoverflow.com/questions/287085/what-do-args-and-kwargs-mean
Old Python : it is not necessary to write *args or **kwargs. Only the * (aesteric) is necessary. You could have also written *var and **vars. Writing *args and **kwargs is just a convention. So now lets take a look at *args first.
New Python: * and ** are different
Usage of *args
*args and **kwargs are mostly used in function definitions. *args and **kwargs allow you to pass a variable number of arguments to a function. What does variable mean here is that you do not know before hand that how many arguments can be passed to your function by the user so in this case you use these two keywords. *args is used to send a non-keyworded variable length argument list to the function.
if __name__ == "__main__" : Usage
http://stackoverflow.com/questions/419163/what-does-if-name-main-do
__name__ == "__main__" is set when the module runs directly and is set to module name when an imported module is running at the time of call.
__name__ == "__main__" is set when the module runs directly and is set to module name when an imported module is running at the time of call.
Thursday, July 30, 2015
Friday, July 10, 2015
date time in python
>>> datetime.datetime.now().time()
http://stackoverflow.com/questions/415511/how-to-get-current-time-in-python
from datetime import datetime
print("time=" + str(datetime.now()))
Wednesday, July 8, 2015
Create/Overwrite file
#providing name for the file to be created
filename = raw_input("Give name for the file: ")
target = open (filename, 'a') ## a will append, w will over-write
#providing the content for the file
print "provide three lines of content for the file:"
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
#writing the entered content to the file we just created
print "entered three lines are written to the file"
target.write(line1)
target.write("n")
target.write(line2)
target.write("n")
target.write(line3)
target.write("n")
#providing information that writing task is completed
print "we have added those text to the file"
target.close()
Subscribe to:
Posts (Atom)