Professional Documents
Culture Documents
# <python ignores everything you put after it in a single line, this is a commen
t
, <putting a comma at the end of something makes sure that it will not go to a n
ew line after the thing is done
"variables" <variables can be numbers or names etc that can be assigned a value
that can a number a string a function a command etc..
% <CANNOT REMEMBER NAME, these have a few varations discussed below, these could
be use to represnt data in a string
and later assigned a value in variable form or other
%s string, what the user can see
%r string, raw, what the programmer put in
%d decimal, an integer
you coud use
ex:
var1
var2
var3
y
takes the filename and whatever else the user typed to execute the code and stor
es it,
and then later in: ex: script, x, y = argv, the stored information is unpacked i
nto
the script, x and y variables.
open() <open is a built in function that accepts parameters, the parameters defi
ned should be
the name of the file and depending on what you want to do with it the mode ie. '
w' for writing, which
will also truncate the file if it is available, 'r' for reading and 'a' for appe
nding the file,
if you dont specify a mode then the default is selected which is 'r'.
.read() <is a command which takes parameters, read is used to read the content o
f a file
.truncate() <is also a comman, command are also called functions and modules by
the way,
truncate pretty much deletes the whole content of the file.
.close() <is a command, which saves and then closses a file, when you write a fi
le
it is necessary to close it.
.write() <is a command used to write thing into a file, it writes whatever is in
the parameter.
>>heres a small prgramme that uses most of the commands.
name = raw_input('>')
file = open(name, 'w')
data = raw_input('>')
file.write(data)
file.close()
open = open(name)
print open.read()
exists() <is a function thal looks for the file defined in the parameter to see
if it is in the
drive, then it returnsa value (false or true) depending on the finding
len() <returns the length in bits of a certain file
def < defines a function
functions <functions are mini codes that are difined by a single name, much like
a variable but
is allocated an entire code rather than just a value or a string, also they take
in values as parameters
like argv does, therefore a function is like a variable that takes in arguments
and has codes
so its like a mini programme inside your programme.
A function is defined with def as said before, the parameter argument thingy goe
s within brackets, you can have
as many thingies as you want as long as they are separated by a comma, the funct
ion difinition thing ends in a ':'.
And then below that exactly 4 spaces under the difinition of the function you ca
n put the code that you want the
ficntion to hold.
return <return is usefull when functions are assigned to variables, thats right
you can assign a function to a
variable, return is like print except it doesnt print inside the function but wi
ll print when the function is called
by a varaible. Look at this example:
def f(x):
print 'will only print this'
return x + 4 #but not this
var = f(3)
print 'this %d comes from var' % var