type() function
kodingwindow@kw:~$ python3 ... >>> type('Hello, World!') <class 'str'> >>> type(7) <class 'int'> >>> type(7.23) <class 'float'> >>> type(True) <class 'bool'> >>> type(False) <class 'bool'> >>> a=[1, 2, 3, 4, 5] >>> type(a) <class 'list'> >>> a=(1) >>> type(a) <class 'int'> >>> a=(1, 1.1) >>> type(a) <class 'tuple'>
range() function
>>> range(10) range(0, 10) >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(5,10)) [5, 6, 7, 8, 9] >>> list(range(0,10,1)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(0,10,2)) [0, 2, 4, 6, 8] >>> list(range(0,10,3)) [0, 3, 6, 9]
chr() and ord() functions
>>> chr(42) '*' >>> chr(65) 'A' >>> ord('A') 65 >>> ord('*') 42
bytes() and bytearray() functions
>>> a=[1,2,3,4,5,6] >>> a[0] 1 >>> a[-1] 6 >>> a[-1]=-1000 >>> a [1, 2, 3, 4, 5, -1000] >>> exp=bytes(a)Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: bytes must be in range(0, 256) >>> a[-1]=255 >>> a [1, 2, 3, 4, 5, 255] >>> exp=bytes(a) >>> exp[-1]=6Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'bytes' object does not support item assignment
>>> a=[1,2,3,4,5,6] >>> exp=bytearray(a) >>> exp[0] 1 >>> exp[-1] 6 >>> exp[-1]=255 >>> exp bytearray(b'\x01\x02\x03\x04\x05\xff') >>> for i in exp: print(i) ... 1 2 3 4 5 255 >>>
What Next?
Python Mathematical Functions
Python Trigonometric Functions
How to use Python help utility
Advertisement