Integral Types
Integers
Syntax | Description |
x+y
|
Add
|
x-y
|
Subtract
|
x*y
|
Multiply
|
x/y
|
Divide
|
x//y
|
Integer Division
|
x%y
|
Modulus
|
x**y
|
Exponentiation
|
-x
|
Negation
|
abs(x)
|
Absolute
|
divmod(x,y)
|
Return quotient and remainder in tuple () unchangeable
|
pow(x,y,z)
|
(x**y)%z
|
round(x,n)
| |
bin(i)
| |
hex(i)
| |
int(i,base)
| |
oct(i)
| |
i|j
|
or
|
i^j
|
xor
|
i&j
|
and
|
i<<j
|
shift i left by j bits
|
i>>j
|
Shifts i right by j bits
|
~i
|
Boolean
t = True
f = False
t and f
t or f
not f
not t
Floating-point Types
Syntax | Description | Syntax | Description |
math.acos(x)
|
radians input inverse cosine
|
math.floor(x)
| |
math.acosh(x)
|
hyperbolic cosine
|
math.fmod(x,y)
| |
math.asin(x)
|
math.frexp(x)
| ||
math.asinh(x)
|
math.fsum(i)
| ||
math.atan(x)
|
math.hypot(x,y)
| ||
math.atan2(y,x)
|
arc tangent y/x
|
math.isinf(x)
| |
math.atanh(x)
|
math.isnan(x)
| ||
math.ceil(x)
|
math.ldexp(m,e)
| ||
math.copysign(x,y)
|
math.log(x,b)
| ||
math.cos(x)
|
math.log10(x,b)
|
base b
| |
math.cosh(x)
|
math.loglp(x)
|
loge(1+x)
| |
math.degrees(r)
|
radian-> degrees
|
math.modf(x)
|
math.modf(1.5)-<(.5,1.0)
|
math.e
|
value of e
|
math.pi
| |
math.exp(x)
|
math.pow(x,y)
| ||
math.fabs(x)
|
abs as float
|
math.radians(d)
|
degree->radians
|
math.factorial(x)
|
x!
|
math.sin(x)
| |
Syntax | Description | Syntax | Description |
math.sinh(x)
|
math.tanh(x)
| ||
math.sqrt(x)
|
math.trunc(x)
|
returns integer
| |
math.tan(x)
|
Complex Numbers
It is a complex number.
Declared as: a = 1 +2j
Syntaxes:
a.real = 1
a.imag = 2
a.conjugate() = 1+ 2j
Strings
Escape
|
Meaning
|
Escape
|
Meaning
|
\n
|
\ooo
| ||
\’
|
\r
| ||
\”
|
\t
| ||
\a
|
\uhhhh
|
“\u221e” “\u23b7”
| |
\b
|
\Uhhhhhhhh
| ||
\f
|
\v
| ||
\n
|
\xhh
| ||
\N{name}
|
“\N{euro sign}”
|
Comparing string by <, <=, ==, !=, > and >= will compare strings byte by byte memory.
Slicing and Striding Strings
S = “Light ray”
S[0] = S[-9] = ‘L’
S[-1] = S[8] = ‘y’
S[0:3] = ‘Lig’
S[start:end:step] i.e. S[2:7:2] = ‘gtr’
s = “The waxwork man”
s[-3:] = “man”
s[4:11] = “waxwork”
s = s[:12]+”wo”+s[12:] = “The waxwork woman”
t = [“Arithmetica” “Conics” “Elements”]
“ “.join(t) = “Arithmetica Conics Elements”
“_”.join(t) = “Arithmetica_Conics_Elements”
“”.join(t) + “ArithmeticaConicsElements”
“*”*10 = “**********”
“hello world”.capitalize() -> “Hello world”
|
“hello”.center(10,*) -> “**hello***”
|
s.count(char,start,end)
“hello”.count(“l”,0,-1) -> 2 counts
|
s.encode(encoding,err)
|
s.endswith(x,start,end)
|
s.expandtabs(size)
|
s.find(t,start,end) ->first position of “t”
|
s.format(...)
|
s.index(t,start,end)
| |
s.isalnum()
|
s.isalpha()
|
s.isdecimal()
|
s.isdigit()
|
s.isidentifier() -> True if not empty and all character are ASCII digit
“hello world”.isidentifier() -> False because ‘ ‘
“hello”.isidentifier(0->True
|
s.islower()
|
s.isnumeric()
|
s.isprintable() -> not special character
|
s.isspace()
|
s.istitle()
“Hello world”.istitle()->False
“Hello World”.istitle()->True
|
s.join(seq)
"Hello World".join('yo')->'yHello Worldo'
|
s.ljust(width,char)
|
s.lower()
|
s.maketrans() ->translate?
|
s.partition(t)
"hello world said I".partition(" ") ->
('hello', ' ', 'world said I')
s.split(t,n)
"hello world said I".split(" ") ->
['hello', 'world', 'said', 'I']
|
s.replace(t,u,n)
"aabbaaccaabb".replace('aa','xx',2) ->
'xxbbxxccaabb'
|
s.splitlines(f)
|
s.startwith(x,start,end)
|
s.strip(chars)
|
s.swapcase()
|
s.title()
|
s.translate()
|
s.upper()
|
s.zfill(w)
"12345".zfill(10) ->'0000012345'
|
String Formatting
i/p:“The novel ‘{0}’ was published in {1}”.format(“Hard Times”, 1854)
o/p:“The novel ‘Hard Times’ was published in 1854”
"{{{0}}}{1} :-}}".format("I'm in braces","I'm not")
"{I'm in braces}I'm not :-}"
x = ‘three”
s = “{0} {1} {2}”
s = s.format(“The”,x,”tops”)
‘The three tops’
Field Names
“{who} turned {age} this year year”.format(who=”she”,age=88)
No comments:
Post a Comment