python_simple_cheatsheet.pdf
(
31 KB
)
Pobierz
3 * 4, 3 + 4, 3 - 4, 3 / 4
#==> 12, 7, -1, 0.75
3 ** 4, 3 // 4, 3 % 4
#==> 81, 0, 3
4 > 3, 4 >= 3, 3 == 3.0, 3 != 4, 3 <= 4
#==> True, True, True, True, True
# order of operations: brackets, **, {* / // %}, {+ -}, {== != <= < > >=}
min(3, 4), max(3, 4), abs(-10)
#==> 3, 4, 10
sum([1, 2, 3]) # [1, 2, 3] is a list
#==> 6
type(3), type(3.0), type("myVariable")
int("4"+"0"), float(3), str(1 / 2)
"double quotes: ’, escaped \" \\ \’"
’it\’s "similar" in single quotes ’
#==> class ’int’, class ’float’,
#
class ’str’
#==> 40, 3.0, ’0.5’
#==> double quotes: ’, escaped " \ ’
#==> it’s "similar" in single quotes
65, ’B’
ord("A"), chr(66)
#==>
string = "hello"
# the following statements work for lists too
len(string)
#==>
string[0], string[4]
# get characters #==>
string[1:3]
# get a substring #==>
string[:2], string[2:] # l/r substrings #==>
string[-1], string[-2:] # negative indices#==>
"con" + "cat" + "enation " + str(123)
#==>
"boo" * 2
#==>
getLineOfInputAsString = input()
print("takes", 0, "or more arguments")
print("using", "custom", "sep", sep=".")
print("no", "newline", end="bye")
#==>
#==>
#==>
#==>
5
"h", "o"
"el"
"he", "llo"
"o", "lo"
"concatenation 123"
"booboo"
read input (or EOF error)
takes 0 or more arguments
using.custom.sep
no newlinebye
not True, False or True, False and True
#==> False, True, False
# order of operations: brackets, {== !=}, not, and, or
if booleanCondition:
x
x
elif anotherCondition:
x
else:
x
while booleanCondition:
x
break
continue
#
#
#
#
#
#
indent the body block
every line by the same amount
can do zero, one, or several elif blocks
multi-line block
optional
multi-line block
# the body block
# jump out (optional)
# restart from top of next iteration (optional)
for indexVariable in range(low, highPlus):
print(indexVariable)
#==> low, low+1, ..., highPlus-1
# "for item in listOrString:" forall/foreach loops
# break, continue can be used in for loops
1
def nameOfNewFunction(argument1, argument2):
x
# the body block
return y
# (optional; if you don’t return, it returns None)
def remember(bar):
global saveBar
saveBar = bar
# writing to global variables
# after calling foo(3), saveBar = 3
# even outside of the function’s scope
# these ’slice’ commands have analogues for lists and range()
"0123456789"[::2]
# slices
#==> "02468"
"0123456789"[::-1]
# descending
#==> "9876543210"
"0123456789"[6:3:-1]
#==> "654"
x += 1
x, y = y, x
3 < x < 5
# also -=, /=, *=, %=, **=, //=. Python has no C++-style "x++"
# multiple assignment
# same as "(3 < x) and (x < 5)". can chain {< <= > >= == != is}
import math
# import, to get everything with period
print(math.sqrt(2))
from math import sqrt
# import, to get one thing without period
print(sqrt(2))
# also in math module: pi, exp, log, sin, cos, tan, ceil, floor, and more
list = [’zero’, ’one’, ’two’]
list.index(’one’)
#==> 1
list.index(’three’)
#==> causes an error
’three’ in list, ’zero’ in list
#==> False, True
list.count(’two’)
#==> 1
del list[1]
# list becomes [’zero’, ’two’]
"string" in "superstring"
#==> True
"superstring".index("string")
#==> 5
# more list methods: append(item), insert(item, index), extend(list),
# remove(value), pop(), pop(index), reverse(), sort(), and more
#
#
#
#
some string methods: capitalize(), lower/upper(), islower/isupper(),
isalpha/isdigit(), center/ljust/rjust(width, fillChar), strip(), split(),
splitlines(), endswith/startswith(string), find(string), replace(old, new),
and more
myList = [11, 99]
actuallyTheSameList = myList # not a true copy! just copies the reference
myList is actuallyTheSameList
#==> True
realCopy = myList[:]
# or list(myList), copy.copy(myList), deepcopy
realCopy is myList
#==> False
2
Plik z chomika:
Expearre4
Inne pliki z tego folderu:
Expert Python Programming (2008).pdf
(10481 KB)
think_python.pdf
(12457 KB)
Python Data Visualization Cookbook - Milovanovic, Igor-signed.pdf
(14987 KB)
Python Cookbook, 3rd Edition.pdf
(10240 KB)
Black Hat Python.pdf
(7056 KB)
Inne foldery tego chomika:
Algo
C++
HTML
JavaScript
Lua
Zgłoś jeśli
naruszono regulamin