## helloworld.py
import sys
def hello(name):
name = "hello, " + name
print name
def main():
if len( sys.argv ) > 1 :
hello(sys.argv[1])
else:
hello('world')
if __name__ == "__main__":
main()
## end of helloworld.py
Functions:
here is how you define a function in python
def functionname (arg_list):
code
code
they start with word def followed by function name, argument list in parentheses and colon ":" in the end
Tabs / indentation:
Indentation in python sort of works as open and close braces in C
if statements:
if (condition):
else :
just like function name they have colon in the end and tabs as braces
Modules :
Python has something called modules, these are python programs that you can import. Any python program can be imported. When its run as a standalone program, it has __name__ variable set to "__main__" . Otherwise, there is no difference. You do not have to have a function called "main" as a language requirement. I am calling main if __name__ variable is set. Otherwise, other programs importing my python file can invoke hello function on their own.
e.g.
I have imported sys module in this example. sys has a few useful functions or data in it. commandline arguments are available as argv[]
dir()
There is something called dir(). if you do dir(sys) you will see what all is available as a part of sys module
So, if I run dir(helloworld) from command line python, here is what I get:
python
>>> import helloworld
>>> dir(helloworld)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'hello', 'main', 'sys']
I do not know yet what all this means, so thats coming soon.
help()
There is also something called help(). e.g. help(len) can show you what len can do.
Need to write more programs to find more language features.
import sys
def hello(name):
name = "hello, " + name
print name
def main():
if len( sys.argv ) > 1 :
hello(sys.argv[1])
else:
hello('world')
if __name__ == "__main__":
main()
## end of helloworld.py
Functions:
here is how you define a function in python
def functionname (arg_list):
they start with word def followed by function name, argument list in parentheses and colon ":" in the end
Tabs / indentation:
Indentation in python sort of works as open and close braces in C
if statements:
if (condition):
else :
just like function name they have colon in the end and tabs as braces
Modules :
Python has something called modules, these are python programs that you can import. Any python program can be imported. When its run as a standalone program, it has __name__ variable set to "__main__" . Otherwise, there is no difference. You do not have to have a function called "main" as a language requirement. I am calling main if __name__ variable is set. Otherwise, other programs importing my python file can invoke hello function on their own.
e.g.
I have imported sys module in this example. sys has a few useful functions or data in it. commandline arguments are available as argv[]
dir()
There is something called dir(). if you do dir(sys) you will see what all is available as a part of sys module
So, if I run dir(helloworld) from command line python, here is what I get:
python
>>> import helloworld
>>> dir(helloworld)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'hello', 'main', 'sys']
I do not know yet what all this means, so thats coming soon.
help()
There is also something called help(). e.g. help(len) can show you what len can do.
Need to write more programs to find more language features.
No comments:
Post a Comment