cs_lang:python:things-to-know:native-datatypes
This is an old revision of the document!
Table of Contents
- Booleans
- Numbers
- Strings
- Bytes
- Lists
- Tuples
- Sets
- Dictionaries
Booleans
>>> 0 == False True >>> a = 1 >>> a < 0 False >>> a > 0 True >>> >>> True + True 2 >>> True + False 1 >>> True * True 1 >>> True / False Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: int division or modulo by zero
Numbers
>>> type(42) <class 'int'> >>> isinstance(42, int) True >>> >>> type(1 + 1.0) <class 'float'> >>> >>> float(2) 2.0 >>> int(2.5) 2 >>> int(-2.5) -2 >>> >>> 1.12345678901234567890 1.1234567890123457 >>> type(1000000000000000000000) <class 'int'>
>>> 11 /2 5.5 >>> 1 // 2 0 >>> -11 // 2 -6 >>> 11.0 // 2 5.0 >>> >>> 11 ** 2 121 >>> 11 % 2 1
Lists
>>> a_list = ["a", 1, True, "Python", 3.6465] >>> a_list[0] 'a' >>> a_list[-1] 3.6465 >>> >>> a_list[:2] ['a', 1] >>> a_list[2:] [True, 'Python', 3.6465] >>> a_list[1:4] [1, True, 'Python'] >>> >>> >>> c = a_list[:2] + a_list[2:] >>> c ['a', 1, True, 'Python', 3.6465] >>> >>> d = a_list[:] >>> d ['a', 1, True, 'Python', 3.6465]
>>> a_list = ["a", 1, True, "Python", 3.6465] >>> a_list.append("Toto") >>> a_list ['a', 1, True, 'Python', 3.6465, 'Toto'] >>> >>> a_list.extend([42, False]) >>> a_list ['a', 1, True, 'Python', 3.6465, 'Toto', 42, False] >>> >>> a_list.insert(0, 'Ω') >>> a_list ['Ω', 'a', 1, True, 'Python', 3.6465, 'Toto', 42, False] >>> >>> a_list.append((42, False)) >>> a_list ['Ω', 'a', 1, True, 'Python', 3.6465, 'Toto', 42, False, (42, False)] >>> >>> len(a_list) 10 >>> a_list[-1] (42, False)
List comprehension
>>> [a for a in range(0,99) if a % 2 == 0] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98] >>> >>> [a % 2 == 0 for a in range(0,99)] [True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True]
Dictionaries
>>> a_dict = {'server': 'cedricbonhomme.org', 'database':'mysql'} >>> >>> a_dict['blog'] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'blog' >>> a_dict['blog'] = 'dotclear' >>> a_dict {'blog': 'dotclear', 'database': 'mysql', 'server': 'cedricbonhomme.org'} >>> >>> >>> a_dict.keys() dict_keys(['blog', 'database', 'server']) >>> a_dict.values() dict_values(['dotclear', 'mysql', 'cedricbonhomme.org'])
cs_lang/python/things-to-know/native-datatypes.1292173889.txt.gz · Last modified: 2010/12/12 18:11 by cedric