tutorial python
This tutorial is for thous who can do programing but don't know or don't remember Python language (like me few months from now).
I will show how to use variables, loops, arrays, functions, and how to write scripts.
I will show how to use variables, loops, arrays, functions, and how to write scripts.
Installation
You can download python from www.python.org. After installing it you can turn on IDLE (python GUI) and executing python commands one by one, seeing results immediately.Variables
In python you don't need to declare variables. You just use them.i = 'hello world'
print i
out
hello worldyou can start using that variable as numeric in any time
i= 12
print i
i += 4
print i
out:
12 4
Arrays
program:l = [1,2,3,4]
print l
print l[0]
print l[2]
out:
[1, 2, 3, 4] 1 3Function range generate array with numebrs from one numebr to another.
l = range(3,8)
print l
out:
[3, 4, 5, 6, 7]
In pythons tabs are important. If you don't put that tab it won't work.
l = range(3,8)
for i in l:
print i
out:
3 4 5 6 7
Functions
program:def foo(ii):
print 'foo function'
print ii
return
foo(123)
out:
foo function 123
Scripts on Windows
You can also write python script (*.py) and execute it by double clicking on it. But if you wish to execute that scpript from command line put it to C:\windows or other direcori from path. Next go to (in case of Windows7) My Computer / Advanced system settings / Environment Variables, now edit PATHEXT by adding ;.py to it. Restart command line and you will be able to run your script from any direcory. C:\windows\hello.pyprint 'hello world'
cmd
C:\>hello hello world
Working and useful example
Ane here is more advenced working example of longlist (like in Linux). It show how to read command line arguments and read files from directory. C:\windows\ll.pyimport os
import sys
def listDir(dir):
filesList= os.listdir(dir)
for f in filesList:
print f
return
args= sys.argv
args.pop(0)
if( len(args)==0 ):
listDir('.')
elif( len(args)==1 ):
listDir( args[0] )
else:
for d in args:
print ''
print d+':'
listDir(d)
cmd
C:\Windows\System32\drivers\etc>ll hosts lmhosts.sam networks protocol services C:\Windows\System32\drivers\etc>