Vous êtes sur la page 1sur 4

________________________________________________________________________________

_______________________
**OPERATORS**
+
-
*
/
& (MOD)
<
>
== same
<= less or same as
>= more or same as
!= or <> not equal
and
or
+= add
________________________________________________________________________________
_______________________
**STATEMENTS**
IF
if x == y: -- if x is y will execute z
z
ELSE
if x == y:
z
else: -- if x is y will execute z, if not will execute a
a
ELSE IF (ELIF)
if x == y:
z
elif x == p: -- if x is y will execute z, else will test if x is p, if it is, wi
ll execute o, else o will execute a
else:
a
________________________________________________________________________________
_______________________
**VARIABLES**
x = 5
bucky = math.sqrt -- you can asign a function to a variable ( x = h.f)
________________________________________________________________________________
_______________________
**FUNCTIONS**
# import h -- for import a module, h being the name of module #
# \' or \ " won't take ' or " as start or end of quotes of print, input... etc #
--defaults--
print("x") -- print a message in screen
input("") -- g = input("") will save on g,!as an expression! what the user write
after x
raw_input("") -- g = raw_input("") same as above but saves !as an string!
pow(x, y) -- power x ^ y
abs(x) -- how much number left to 0 (ex: abs(-5) = 5)
str(x) -- converts to string (ex: x = str(x)), when printing add `x` to convert
to str [rpr(x)]
int(x) -- converts to integer (ec: x = int(x))
len(x) -- x being the name of the list, will return the number of elements of a
list
max(x) -- x being the name of the list, will return the max value of the list
min(x) -- x being the name of the list, will return the min value of the list
list('x') -- x string, will convert each character of x in a element of a list
sorted('x') -- will sort x and creat a list of it sorted elements
--math module--
math.floor(x.y) -- round the number down (ex: math.floor(18.7) = 18.0)
math.ceil(x.y) -- round the number up (ex: math.floor(18.7) = 19.0)
________________________________________________________________________________
_______________________
**SEQUENCES**
You can multiply, add, rest, etc... sequences
family = ["mom", "dad", "bro", "sis", "dog"] -- create a list
family[x] -- for recall a element of list, x being the position of the element,
position is count from the first from left as 0 (mom = 0, dad = 1, bro = 2, sis
= 3, dog = 4) or if you start from the back of the list it will start -1 for the
first element (dog = -1, sis = -2, bro = -3...)
'bucky'[3] = k -- index also work for strings in this case the third letter of '
bucky' will be 'k'
example = [0,1,2,3,4,5,6,7,8,9]
example[4:8] -- extraction, this will take numbers from 4 to the one before 8 gi
ving: [4, 5, 6, 7]
if you leave [4:] it will go from 4 to the end of the list givin
g: [4,5,6,7,8,9]
if you leave [:8] it will go from the begining of the list to 8
giving: [0,1,2,3,4,5,6,7]
if you leave [:] it will go from the begining to the end of the
list: [0,1,2,3,4,5,6,7,8,9]
if you do [1:8:2] it will go from 1 to 7 by 2 giving: [1,3,5,7]
if you do [10:0:-2]or[::-2] it will go from 9 to 0 by -2 giving:
[9,7,5,3,1]
a[b]=c -- will overwrite whatever is on the b position in the list a, for the va
lue c
________________________________________________________________________________
_______________________
**BUILT IN**
IN - 'x' in h -- will return False or True, it will check if 'x' is inside the v
ariable h
DEL - del x -- will delete de x variable
- del x[c] -- will delete the value in c's position of the list x
IS -a is b-- false -a is a-- true
________________________________________________________________________________
_______________________
**METHODS**
DO something to an OBJECT
ex: yourface.punch() / object.action()
--for lists--
x.append(y) -- add the object y to the end of the x list
x.count(y) -- count how many times the object y exist in the list x
x.extend(y) -- same as append, but can also add a y list to x list, append can't
x.index(y) -- search for y in the x list and will return its position
x.insert.(y,z) -- will insert y object in the z position of x list
x.pop(y) -- y being the position of the object you want to delete from x list, i
t will also return what was in the y position
x.remove(y) -- delete y object on the x list
x.reverse() -- reverse all the elements of x list ([1,2,3,4,5] to [5,4,3,2,1])
x.sort(y) -- sort the elements of x list with y parameters, default sort from mi
n to max
x.join.(y) -- make all the elements of y one, joining them with x
--for strings--
x.find(y) -- return the position of the y element in x
x.lower() -- make all elements of x lowercase
x.upper() -- make all elements of x uppercase
x.replace(y,z) -- replace y for z in x
________________________________________________________________________________
_______________________
**DICTIONARY**
-Store values with a reference value, example:
book = {'Dad':'Bob','Mom':'Lisa':'Bro':'Joe'}
^ ^
A B
A will be the reference value for B
-for search a value use:
book['Dad'] -- will return 'Bob'
x.clear() -- will clear x
x=y.copy() -- will copy the content y to x
________________________________________________________________________________
_______________________
**TUPLE**
like a list, but cant be modified, just read, for create one:
x=(y) -- create y elements in the x tuple
x=[y] -- will view the elements in the y position of the x list
________________________________________________________________________________
_______________________
**LOOPS**
WHILE -- loops while statemente is true, ex:
b=1
while b <= 10:
print b -- while b will be lower or same as 10, it will execute print
b and b += 1
b += 1
FOR --
gl = ['bread','milk','meat','beef']
for food in gl:
print 'I want ' + food -- pass gl list to food and execute the code p
rint 'I want ' + food
I want bread for each element of food
I want milk
I want meat
I want beef
EXAMPLES
ages = {'dad':42, 'mom':48, 'lisa':7}
for name in ages:
print name, ages[name]
dad 42
lisa 7
mom 48
--
while 1:
name = raw_input('enter name: ')
if name == 'quit': break
enter name: makr
enter name: kosdoia
enter name: sijaso
enter name: dsikdsaaosad
enter name: ifa
enter name: quit
END
________________________________________________________________________________
_______________________

Vous aimerez peut-être aussi