Chapter 2 Built-in Data Types
2.2 Numbers (int/float)
Two types of built-in number type, integer and float.
n = 123 ## integer
type (n)
f = 123.4 ## float
type (f)## <class 'int'>
## <class 'float'>
2.2.1 Number Operators
In general, when the operation potentially return float, the result is float. Otherwise it return integer.
Division always return float
4/2 ## return float
type(4/2) ## 2.0
## <class 'float'>
Integer Division returns int or float
8//3 ## div by int return int
8//3.2 ## div by float return float## 2
## 2.0
Remainder returns int or float.
8%3 ## remainder by int, returns int
8%3.2 ## remainder by float, returns float## 2
## 1.5999999999999996
Power returns int or float.
2**3 ## power of int to int returns int
2**3.2 ## power to float returns float
2.3**2 ## power of float returns float## 8
## 9.18958683997628
## 5.289999999999999
2.3 String (str)
Str is an ordered collection of letters.
s = 'abcde'
type(s) ## class str
type(s[0]) ## class str
len(s) ## number of characters## <class 'str'>
## <class 'str'>
## 5
2.3.1 Constructor
class str(object='')
class str(object=b'', encoding='utf-8', errors='strict')
## these are the same
str() ## empty string
'' ## short hand
## these are the same
str('abc') ## constructor
'abc' ## short hand## ''
## ''
## 'abc'
## 'abc'
Multi-line
Note the result contains \n at the front of the string, due to a return at the beginning of the source text.
## multiline
'''
This is me.
Yong Keh Soon'''## '\nThis is me.\nYong Keh Soon'
2.3.2 Immutable
strobject is immutable. Reassigning a value completely reinitialize it with different memory location.
s = 'abcde'
id(s)
s = '12345' ## Reinitialize it as different object
id(s) ## hence the ID is now different## 1985785465968
## 1985785476976
2.3.3 Index-able
Although String has array like indexable capability, it is not iterateable.
string[start:end:step] # default start:0, end:last, step:1
If step is negative (reverse), end value must be lower than start value
s = 'abcdefghijk'
s[0] # first later
s[:3] # first 3 letters
s[2:8 :2] # stepping
s[-1] # last letter
s[-3:] # last three letters
s[: :-1] # reverse everything
s[8:2 :-1] # till the end
s[8:2] # return NOTHING## 'a'
## 'abc'
## 'ceg'
## 'k'
## 'ijk'
## 'kjihgfedcba'
## 'ihgfed'
## ''
2.3.4 Class Attributes
string.ascii_letters ## for ASCI chars
string.ascii_lowercase ## all lower case
string.ascii_uppercase ## all upper case
string.digits ## 0 - 9
string.whitespace ## all white spaces## 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
## 'abcdefghijklmnopqrstuvwxyz'
## 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
## '0123456789'
## ' \t\n\r\x0b\x0c'
2.3.5 Instance Methods
Substitution (format, fstring)
By Variable Name - supported by fstring()
fstring is useful in a way that you can infer variable name within the text directly.
a = 'a'
b = 'b'
c = 'c'
f"{a} + {b} = {c}" # fstring method## 'a + b = c'
By Positional - supported by .format()
"{} + {} = {}" .format('a', 'b', 'c') # format: auto sequence
"{0} + {1} = {2}".format('a', 'b', 'c') # format: manual sequence## 'a + b = c'
## 'a + b = c'
By Name of parameters or dict - supported by .format()
"Coordinates: {latitude}, {longitude}".format(latitude='37.24N', longitude='-115.81W') ## parameters
## dictionary key/value
coord = {'latitude': '37.24N',
'longitude': '-115.81W'}
'Coordinates: {latitude}, {longitude}'.format(**coord) ## dictionary key/value## 'Coordinates: 37.24N, -115.81W'
## 'Coordinates: 37.24N, -115.81W'
Formatting (format, fstring)
With .format(), format numbers by using {: . , f %}.
'{:+f}; {:+f}'.format(3.14, -3.14) ## show +ve -ve sign
'{: f}; {: f}'.format(3.14, -3.14) ## show only -ve sign
'{:.1f}; {:.1f}'.format(3.15, -3.16) ## round down/up
'{0:,}'.format(1234567890.4455) ## show decimal and %
'{0:,.2f}'.format(1234567890.4455) ## show comma, float decimal
'{0:,.2%}'.format(1234567890.4455) ## show comma, % decimal## '+3.140000; -3.140000'
## ' 3.140000; -3.140000'
## '3.1; -3.2'
## '1,234,567,890.4455'
## '1,234,567,890.45'
## '123,456,789,044.55%'
Similar result can be achieved using fstring, by just replacing by putting variable names inline.
a = 3.14
b =-3.14
c = 1234567890.4455
f'{a:+f}; {b:+f}' ## show +ve -ve sign
f'{a: f}; {b: f}' ## show only -ve sign
f'{a:.1f}; {b:.1f}' ## round down/up
f'{c:,}' ## show decimal and %
f'{c:,.2f}' ## show comma, float decimal
f'{c:,.2%}' ## show comma, % decimal## '+3.140000; -3.140000'
## ' 3.140000; -3.140000'
## '3.1; -3.1'
## '1,234,567,890.4455'
## '1,234,567,890.45'
## '123,456,789,044.55%'
Text Alignment
'{:*^43}'.format('centered') ## '*' filler applied
'{0:<20} {0:*<20}'.format('left aligned') ## '*' filler applied
'{0:*>20} {0:>20}'.format('right aligned') ## '*' filler applied
''
'{:^43}'.format('centered') ## no filler
'{0:<20} {0:<20}'.format('left aligned') ## no filler
'{0:>20} {0:>20}'.format('right aligned') ## no filler## '*****************centered******************'
## 'left aligned left aligned********'
## '*******right aligned right aligned'
## ''
## ' centered '
## 'left aligned left aligned '
## ' right aligned right aligned'
Similar result can be achieved using fstring, which is more concised.
center = 'centered'
left = 'left aligned'
right = 'right aligned'
f'{center:*^43}' ## '*' filler applied
f'{left:<20} {left:*<20}' ## '*' filler applied
f'{right:*>20} {right:>20}' ## '*' filler applied
f''
f'{center:^43}' ## no filler
f'{left:<20} {left:<20}' ## no filler
f'{right:>20} {right:>20}' ## no filler## '*****************centered******************'
## 'left aligned left aligned********'
## '*******right aligned right aligned'
## ''
## ' centered '
## 'left aligned left aligned '
## ' right aligned right aligned'
Case Conversion (upper/lower)
'myEXEel.xls'.upper()
'myEXEel.xls'.lower()## 'MYEXEEL.XLS'
## 'myexeel.xls'
Searh Location (find)
.find() returns position of first occurrence, -1 if not found, and it is case sensitive.
s='I love karaoke, I know you love it too'
s.find('lov') ## found
s.find('Lov1') ## not found, return -1## 2
## -1
Strip Blank Spaces (strip.lstrip,rstip)
Leading and trailing blank spaces (newline, tab, cr) will be removed.
'\n myexce l. xls \r\t\n'.strip() ## strip both leading and trailing
'\n myexce l. xls \r\t\n'.lstrip() ## strip leading only
'\n myexce l. xls \r\t\n'.rstrip() ## strip trailing only## 'myexce l. xls'
## 'myexce l. xls \r\t\n'
## '\n myexce l. xls'
Split & Join (split, join)
split('<delimiter>') breaks down string into a list by delimiter. Observe the empty spaces were conserved in result array
animals = 'a1,a2 , a3 , a4'
animals.split(',')## ['a1', 'a2 ', ' a3 ', ' a4']
'<delimiter>.join() is the reverse of split.
'-'.join(['1', '2', '3', '4'])## '1-2-3-4'
Replacement (repalce)
replace`(old, new, count=-1) is case sensitive. By default, it replaces all occurances (count=-1).
s = "geeks for geeks geeks geeks geeks"
s.replace("geeks", "Geeks") ## default replace all
s.replace("geeks", "Geeks", 3) ## replace 3 times## 'Geeks for Geeks Geeks Geeks Geeks'
## 'Geeks for Geeks Geeks geeks geeks'
2.3.6 Operators
printf Style Substitution (%)
This is an old style string substitution. Whenever possible, use fstring or .format() instead. They have better syntax and comes with richer formating/alignment capabilities.
my_name = 'Yong Keh Soon'
salary = 11123.346
'Hello, %s, your salary is %.2f.' % (my_name, salary)## 'Hello, Yong Keh Soon, your salary is 11123.35.'
Is Inside (in)
<substr> in <string> ## any partial match
<substr> in [ <string>, <string>, <string> ] ## any element with exact match
This is a boolean test:
- For string, any partial match to substring will return True
- For list of string, any element with Exact match will return True
'abc' in '123abcdefg' # true## True
'abc' in ['abcdefg'] # false## False
'abc' in ['abcdefg','123'] # false## False
'abc' in ['123','abc','def'] # true## True
'abc' in str(['123','abcdefg']) # true## True
2.4 Boolean
b = False
if (b):
print ('It is true')
else:
print ('It is fake')## It is fake
2.4.1 What is False ?
Everything below are false, anything else are true
print ( bool(0), # zero
bool(None), # none
bool(''), # empty string
bool([]), # empty list
bool(()), # empty tupple
bool(False), # False
bool(2-2)) # expression that return any value above## False False False False False False False
2.4.2 Operators
and
BEWARE !
- and can return different data types
- If evaluated result is True, the last True Value is returned (because python need to evaluate up to the last value)
- If evaluated result is False, the first False Value will be returned (because python return it immediately when detecting False value)
print (123 and 2 and 1,
123 and [] and 2)## 1 []
not
not (True)## False
not (True or False)## False
not (False)## True
not (True and False)## True
~(False)## -1
or
- or can return different data type
- If evaluated result is True, first True Value will be returned (right hand side value need not be evaluated)
- If evaluated result is False, last Fasle Value will be returned (need to evalute all items before concluding False)
print (1 or 2)## 1
print (0 or 1 or 1)## 1
print (0 or () or [])## []