Vous êtes sur la page 1sur 2

Alvaro Sebastian

Python 3 Beginner's Reference Cheat Sheet http://www.sixthresearcher.com

Main data types List operations List methods


boolean = True / False list = [] defines an empty list list.append(x) adds x to the end of the list
integer = 10
list[i] = x stores x with index i list.extend(L) appends L to the end of the list
float = 10.01
list[i] retrieves the item with index I list.insert(i,x) inserts x at i position
string = 123abc
list[-1] retrieves last item list.remove(x) removes the first list item whose
list = [ value1, value2, ]
list[i:j] retrieves items in the range i to j value is x
dictionary = { key1:value1, key2:value2, }
del list[i] removes the item with index i list.pop(i) removes the item at position i and
returns its value
Numeric Comparison list.clear() removes all items from the list
operators operators Dictionary operations
list.index(x) returns a list of values delimited
+ addition == equal dict = {} defines an empty dictionary by x
- subtraction != different list.count(x) returns a string with list values
dict[k] = x stores x associated to key k
* multiplication joined by S
> higher dict[k] retrieves the item with key k
/ division list.sort() sorts list items
< lower del dict[k] removes the item with key k
** exponent list.reverse() reverses list elements
% modulus >= higher or equal
list.copy() returns a copy of the list
// floor division <= lower or equal String methods

Boolean Special string.upper() converts to uppercase Dictionary methods


operators characters string.lower() converts to lowercase
string.count(x) counts how many dict.keys() returns a list of keys
and logical AND # coment dict.values() returns a list of values
times x appears
or logical OR \n new line string.find(x) position of the x first dict.items() returns a list of pairs (key,value)
not logical NOT \<char> scape char occurrence dict.get(k) returns the value associtated to
string.replace(x,y) replaces x for y the key k
string.strip(x) returns a list of values dict.pop() removes the item associated to
String operations the key and returns its value
delimited by x
string.join(L) returns a string with L dict.update(D) adds keys-values (D) to dictionary
string[i] retrieves character at position i dict.clear() removes all keys-values from the
values joined by string
string[-1] retrieves last character dictionary
string.format(x) returns a string that
string[i:j] retrieves characters in range i to j includes formatted x dict.copy() returns a copy of the dictionary

Legend: x,y stand for any kind of data values, s for a string, n for a number, L for a list where i,j are list indexes, D stands for a dictionary and k is a dictionary key.
Alvaro Sebastian
Python 3 Beginner's Reference Cheat Sheet http://www.sixthresearcher.com

Built-in functions Conditional


Loops Functions
statements
print(x, sep='y') prints x objects separated by y if <condition> : while <condition>: def function(<params>):
<code> <code> <code>
input(s) prints s and waits for an input else if <condition> :
that will be returned return <data>
<code> for <variable> in <list>:
len(x) returns the length of x (s, L or D) <code>
else: Modules
min(L) returns the minimum value in L <code> for <variable> in
max(L) returns the maximum value in L range(start,stop,step): import module
if <value> in <list>: <code> module.function()
sum(L) returns the sum of the values in L
for key, value in from module import *
range(n1,n2,n) returns a sequence of numbers
from n1 to n2 in steps of n Data validation dict.items(): function()
<code>
abs(n) returns the absolute value of n try:
Reading and
<code>
round(n1,n) returns the n1 number rounded Loop control writing files
to n digits except <error>:
statements
<code> f = open(<path>,r')
type(x) returns the type of x (string, float, else: break finishes loop f.read(<size>)
list, dict ) <code> execution f.readline(<size>)
continue jumps to next f.close()
str(x) converts x to string iteration
Working with files pass does nothing
list(x) converts x to a list f = open(<path>,r)
and folders
for line in f:
int(x) converts x to a integer number
Running external <code>
import os
float(x) converts x to a float number programs f.close()
os.getcwd()
os.makedirs(<path>) f = open(<path>,'w')
help(s) prints help about x
os.chdir(<path>) import os
f.write(<str>)
map(function, L) Applies function to values in L os.listdir(<path>) os.system(<command>)
f.close()

Legend: x,y stand for any kind of data values, s for a string, n for a number, L for a list where i,j are list indexes, D stands for a dictionary and k is a dictionary key.

Vous aimerez peut-être aussi