by Fedor Iskhakov, ANU
Description: Variables and memory, binary operations, logical expressions, composite variables types.
📖 Kevin Sheppard “Introduction to Python for Econometrics, Statistics and Data Analysis.” Chapters: 3, 5, 10, 11, 13, 18, 22
Today: talking about core Python
Python Enhancement Proposal 0008
https://www.python.org/dev/peps/pep-0008/
Names of variables $ \exists $ certain rules: can only contain numbers, letters (both upper and lower) and underscores (_)
Reserved words: and as assert break class continue def del elif else except exec finally for from global if import in is lambda not or pass print raise return try while with yield
Comments made with # at any location of a line
# This is a comment
x = False
y = True
x and y
False
y = 1 < 5
y = y and (4 < 8)
y
True
# Modify the code appropriately
z = (1 < 0)
z = (2 >= 4)
z = (5 <= 5)
z = (2 > 4)
z
False
import sys
x = 1
print("x is %r" % x)
print("Type of x is %s" % type(x))
y = 0b1011 #binary (base=2)
# y = 0x10f #hex (base=16)
print("y is %r" % y)
print("Type of y is %s" % type(y))
print("y takes %d bytes in memory" % sys.getsizeof(y))
x is 1 Type of x is <class 'int'> y is 11 Type of y is <class 'int'> y takes 28 bytes in memory
y = 0b1111111111111111111111111111110
# 1234567890123456789012345678901234567890
y = 0x1afe
print("y is %r" % y)
print("Type of y is %s" % type(y))
print("y takes %d bytes in memory" % sys.getsizeof(y))
y is 6910 Type of y is <class 'int'> y takes 28 bytes in memory
Python adjusts the memory for integers as to fit large numbers!
a = 155
b = 7
c=a+b
print("%d + %d = %r (%s)" % (a,b,c,type(c)))
155 + 7 = 162 (<class 'int'>)
c=a-50*b
print("%d - 50*%d = %r (%s)" % (a,b,c,type(c)))
155 - 50*7 = -195 (<class 'int'>)
a = 155
b = 7
c = a**b
print("%d ^ %d = %r (%s)" % (a,b,c,type(c)))
c = a/b
print("%d / %d = %r (%s)" % (a,b,c,type(c)))
155 ^ 7 = 2149422977421875 (<class 'int'>) 155 / 7 = 22.142857142857142 (<class 'float'>)
Python adjusts the type of integers as to fit the result of arithmetic operation!
a = 155
b = 7
c = a//b
print("%d // %d = %r (%s)" % (a,b,c,type(c)))
c = a%b
print("%d %% %d = %r (%s)" % (a,b,c,type(c)))
155 // 7 = 22 (<class 'int'>) 155 % 7 = 1 (<class 'int'>)
import sys
x = 15 > 10
print("x is %r" % x)
x is True
print("Type of x is %s" % type(x))
print("x takes %d bytes in memory" % sys.getsizeof(x))
Type of x is <class 'bool'> x takes 28 bytes in memory
y = 36
print("Type of y is %s" % type(y))
print("y takes %d bytes in memory" % sys.getsizeof(y))
Type of y is <class 'int'> y takes 28 bytes in memory
print(bool.__bases__) #see that boolean is a subclass of integer
(<class 'int'>,)
z = (1 < 5) - 5
z
-4
x = 15
z = x * (x > 10) + x**2 * (x < 10)
z
15
What if $ x=10 $ in the last example?
y = 5 - 4 > True
y
False
y = 6 < 10 <= 15
y
True
y = 5 / 2**4 < 10 or 15
y
True
Use brackets to avoid ambiguity!
Representation for real numbers
x = 183.0
print("x is %r" % x)
print("Type of x is %s" % type(x))
x is 183.0 Type of x is <class 'float'>
print("x takes %d bytes in memory" % sys.getsizeof(x))
sys.float_info
x takes 24 bytes in memory
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
x = 1.79769319e+308
print("x is %r" % x)
print("Type of x is %s" % type(x))
print("x takes %d bytes in memory" % sys.getsizeof(x))
x is inf Type of x is <class 'float'> x takes 24 bytes in memory
import math as m
# import numpy as m
x = m.log(0) # implementations may vary in different packages/libraries!
print("x is %r" % x)
print("Type of x is %s" % type(x))
print("x takes %d bytes in memory" % sys.getsizeof(x))
Representation for imaginary numbers
x = 1j+5
print("x is %r" % x)
print("Type of x is %s" % type(x))
print("x takes %d bytes in memory" % sys.getsizeof(x))
x is (5+1j) Type of x is <class 'complex'> x takes 32 bytes in memory
from cmath import exp, pi
x=1j
x = exp(x*pi)+1
print("x is %r" % x)
print("|x| is %1.20f" % abs(x))
print("Type of x is %s" % type(x))
print("x takes %d bytes in memory" % sys.getsizeof(x))
x is 1.2246467991473532e-16j |x| is 0.00000000000000012246 Type of x is <class 'complex'> x takes 32 bytes in memory
s='Hellow world'
print("s is %r" % s)
print("Type of s is %s" % type(s))
print("Length of \"%s\" is %d" %(s,len(s)))
print("s takes %d bytes in memory" % sys.getsizeof(s))
s is 'Hellow world' Type of s is <class 'str'> Length of "Hellow world" is 12 s takes 61 bytes in memory
s='Australian National University'
# 012345678901234567890123456789
print("s[0:9] is %r" % s[0:9]) # from 1st up to and excluing 9th
print("s[:9] is %r" % s[:9]) # the same
print("s[11:] is %r" % s[11:]) # from 11th till the end
print("s[-10:] is %r" % s[-10:]) # 10th last till the end
print("s[::3] is %r" % s[::3]) # from beginning to end with step 3
print("s[0:0:-1] is %r" % s[::-1]) # from end till beginning with step -1 (reverse order)
s[0:9] is 'Australia' s[:9] is 'Australia' s[11:] is 'National University' s[-10:] is 'University' s[::3] is 'Atlnaolnei' s[0:0:-1] is 'ytisrevinU lanoitaN nailartsuA'
s='jde4jecc doij naajo rdmp hin0icbdrs1cgdhttuif 7gjxm'
# 012345678901234567890123456789012345678901234567890
print("s[0:9] is %r" % s[::]) # output "economics"
s[0:9] is 'jde4jecc doij naajo rdmp hin0icbdrs1cgdhttuif 7gjxm'
s=''
for i in range(10):
s=s + 'a'
print("Memory(\"%s\", %d symbols) = %d bytes"%(s,len(s),sys.getsizeof(s)))
Memory("a", 1 symbols) = 50 bytes Memory("aa", 2 symbols) = 51 bytes Memory("aaa", 3 symbols) = 52 bytes Memory("aaaa", 4 symbols) = 53 bytes Memory("aaaaa", 5 symbols) = 54 bytes Memory("aaaaaa", 6 symbols) = 55 bytes Memory("aaaaaaa", 7 symbols) = 56 bytes Memory("aaaaaaaa", 8 symbols) = 57 bytes Memory("aaaaaaaaa", 9 symbols) = 58 bytes Memory("aaaaaaaaaa", 10 symbols) = 59 bytes
x=2
for i in range(10):
x**=2
print("Memory(\"%d\") = %d bytes"%(x,sys.getsizeof(x)))
Memory("4") = 28 bytes Memory("16") = 28 bytes Memory("256") = 28 bytes Memory("65536") = 28 bytes Memory("4294967296") = 32 bytes Memory("18446744073709551616") = 36 bytes Memory("340282366920938463463374607431768211456") = 44 bytes Memory("115792089237316195423570985008687907853269984665640564039457584007913129639936") = 60 bytes Memory("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096") = 96 bytes Memory("179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216") = 164 bytes
x=2.0
for i in range(10):
x**=2
print("Memory(\"%e\") = %d bytes"%(x,sys.getsizeof(x)))
Memory("4.000000e+00") = 24 bytes Memory("1.600000e+01") = 24 bytes Memory("2.560000e+02") = 24 bytes Memory("6.553600e+04") = 24 bytes Memory("4.294967e+09") = 24 bytes Memory("1.844674e+19") = 24 bytes Memory("3.402824e+38") = 24 bytes Memory("1.157921e+77") = 24 bytes Memory("1.340781e+154") = 24 bytes
--------------------------------------------------------------------------- OverflowError Traceback (most recent call last) <ipython-input-30-83fdb77b502f> in <module> 1 x=2.0 2 for i in range(10): ----> 3 x**=2 4 print("Memory(\"%e\") = %d bytes"%(x,sys.getsizeof(x))) OverflowError: (34, 'Result too large')
a = 21
b = 10
c = 2
b **= c
print ("Line 1 - Value of b is %d" % b)
c *= a
print ("Line 2 - Value of c is %d" % c)
c -= a
print ("Line 3 - Value of c is %d" % c)
Line 1 - Value of b is 100 Line 2 - Value of c is 42 Line 3 - Value of c is 21
Python implements complex memory management system to avoid unnecessary memory allocation
x = 10
print("Initial id(x) is %s" % id(x))
y = x
print(" id(y) is %s" % id(y))
y +=x
print(" Now id(y) is %s" % id(y))
Initial id(x) is 4469753248 id(y) is 4469753248 Now id(y) is 4469753568
x = [True, 5, 5.2, 'string',] # trailing comma is ignored
y = (True, 5, 5.2, 'string',)
print("x is %r" % x)
print("Type of x is %s" % type(x))
print("x takes %d bytes in memory" % sys.getsizeof(x))
print()
print("y is (%r,%r,%r,%r)" % y)
print("Type of y is %s" % type(y))
print("Y takes %d bytes in memory" % sys.getsizeof(y))
x is [True, 5, 5.2, 'string'] Type of x is <class 'list'> x takes 104 bytes in memory y is (True,5,5.2,'string') Type of y is <class 'tuple'> Y takes 88 bytes in memory
x = [True, 5, 5.2, 'string',] # last comma is ignored
y = (True, 5, 5.2, 'string',)
x[0] = 567; # lists are mutable
y[0] = 567; # tuples are immutable -> ERROR
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-34-afe959f6c3e1> in <module> 3 4 x[0] = 567; # lists are mutable ----> 5 y[0] = 567; # tuples are immutable -> ERROR TypeError: 'tuple' object does not support item assignment
x = [True, 5, 5.2, 'string', 4, 4+2j, 'again']
print( x[0] ) # first element
print( x[1:-1] ) # slicing as with strings
print( len(x) ) # length of the list
x.append(586) # add a value at the end
print(x)
x.pop(3) # remove fourth element
print(x)
True [5, 5.2, 'string', 4, (4+2j)] 7 [True, 5, 5.2, 'string', 4, (4+2j), 'again', 586] [True, 5, 5.2, 4, (4+2j), 'again', 586]