Data type
- bool
- int/float
- string
transforming
- int/float->string:
str()
- string/float->int:
int()
- int/string->float:
str()
find type
type()
-
isinstance(a,b)
:True if same type
Calculation
+
-
*
/
//
%
** power (A ** B) priority: B>**
>A
and
or
not
priority
Branch
if else
elif
A = x if true else y
Assert
assert condition (check point)
Loop
while condition: loop #test
for i in string/list/tupple: loop
range([start,]stop[,step])
break vs. continue
break
: jump out of current loop
continue
: back to the condition judgement
List
[A,B,C,D]
mixed data type
option
list.append()
attach ONE element to the tail (list's method)
list.extend()
attach ONE list to the tail
list.insert(position,x)
list.remove()
del element/list
NOT method!
list.pop(position,default=len(list)-1)
slice
list[start(included):stop(not included)]
list[:]
used for copy)
list1 = list
vs.list2 = list[:]
indexing
list[]
list[][]
list.index(target,start,stop)
first occurance
count
list.count()
compare
compare first element
reverse order
list.reverse()
sort order
list.sort(reverse=True)
Tuple
(A,B,C,D)
NOT applied for modifying!
(1)
vs(1,)
String
slice: str[start:stop]
like list
insert: str = str[:x] + str1 + str[x:]
capitalzie: str.capitalize()
capitalize 1st letter
casefold: str.casefold()
casefold all letters
str.center(width)
center
str.count(sub[,star[,end]])
count
str.endswith(sub[,star[,end]])
end with certain string
str.expandtabs(tabsize = n)
transform tabs into n spaces
str.find(sub[,star[,end]])
return index or -1
str.index(sub[,star[,end]])
if sub not in str: error
str.isalnum()
return True if str contains at least 1 char and only numbers/chars
str.isalpha()
return True if str contains at least 1 char and only chars
str.isnumeric()
return True if str contains only numberic chars
str.isdecimal()
return True if str contains only decimal numbers
str.isdigit()
return True if str contains only numbers
str.islower()
return True if str contains at all lower case letters
str.isspace()
return True if str contains only space
str.istitle()
return True if all words in str start with upper case letters, and no other upper case letters eg. 'All Star'
str.isupper()
str.join(sub)
cut str with sub
eg.
str = '123'
sub = 'd'
'1d2d3'
str.ljust()
/ str.rjust()
adjust to left/right side
str.lower()
convert to lower case
str.lstrip()
/ str.rstrip()
remove all spaces in front of/at the end of the string
str.partition(sub)
cut str with sub into tuple ONCE
str.rpartition(sub)
cut str with sub into tuple ONCE from the right side
str.repalce(old,new[,count])
replace old with new (no more than count times)
str.rfind(sub[,start[,end]])
find sub from right side, return same as str.find
str.rindex(sub[,star[,end]])
search from right side
str.split(sep=None,maxsplit=-1)
cut str with sep(space default) into list (maxsplit times at most)
str.splitlines(keepends)
str.startswith(prefix[,start[,end]])
check wether start with certain prefix
str.strip([chars])
remove chars (space default)
str.swapcase()
swap upper & lower case
str.title()
turn all 1st letter into upper case
str.translate(table)
str.upper
convert to upper case
str.zfill(width)
return width long str (fill empty with '0')
format
"{0} {1} {a}".format("a","b",a = "c")
'{0:.1f}{1}'.format(1.234,'GB')
'%c*%c' % (62,63)
'%d=%d' % (10,10)
'%o' % 12
8
'%x' % 255
16 (lower case)
'%X' % 255
16 (upper case)
'%.9f' % 1
/ '%9f' % 1
(default 6 long)/ '%09f' % 1
(fill left with 0) / '%-.9f' % 1
align to left
'%e' % 19000
/ '%E' % 19000
'%g' % 19000
/ '%G' % 19000
automatically choose f/e
'%#o' % 100
>>>'0o144'
Data Link Escape Character
'\''
: '
'\"'
; "
'\a'
: alert?
'\b'
: backspace
'\n'
: enter
'\t'
: TAB
'\v'
: vertical TAB
'\r'
:
'\f'
: turn page?
'\o'
: 8 '\x'
: 16
'\0'
: none
'\\'
: \