Professional Documents
Culture Documents
Primitive Datatypes in Python
Primitive Datatypes in Python
Primitive Datatypes in Python
Introduction
Python Python
Datatypes Datatypes
Slide 1 of 22 Slide 2 of 22
Python Python
Datatypes Datatypes Two kinds of numbers: integers and floats
Walter Cazzola
Python provides two constants Walter Cazzola – no class declaration to distinguish them
– True and False
– they can be distinguished by the presence/absence of the decimal
Primitive Type Primitive Type point.
Boolean Operations on Booleans Boolean
Numbers Numbers
logic operators 1 [15:26]cazzola@ulik:~/esercizi-pa>python3
Collections Collections 2 >>> type(1)
Lists and or not Lists
3 <class ’int’>
Tuples logical and, or and negation respectively Tuples
4 >>> isinstance(1, int)
Sets relational operators Sets
5 True
Dictionaries Dictionaries
== != 6 >>> 1+1
Strings equal and not equal to operators Strings 7 2
Bytes < > <= >= Bytes 8 >>> 1+1.0
9 2.0
References less than, greater than, less than or equal to and greater References 10 >>> type(2.0)
than or equal to operators 11 <class ’float’>
12 >>>
13 [15:27]cazzola@ulik:~/esercizi-pa>
Note that python allows chains of comparisons
[17:42]cazzola@ulik:~/esercizi-pa>python3
>>> x=3
– type() function provides the type of any value or variable;
>>> 1<x<=5 – isinstance() checks if a value or variable is of a given type;
True
– adding an int to an int yields another int but adding it to a float
yields a float.
Slide 3 of 22 Slide 4 of 22
Python’s Native Datatypes Python’s Native Datatypes
Operations on Numbers Lists
Python Python
Datatypes Datatypes
Walter Cazzola Walter Cazzola A python list looks very closely to an array
Coercion & Size – direct access to the members through [];
Primitive Type Primitive Type
Boolean – int() function truncates a float to an integer; Boolean
[12:29]cazzola@ulik:~/esercizi-pa>python3
Numbers Numbers
– float() function promotes an integer to a float; >>> a_list = [’1’, 1, ’a’, ’example’]
Collections Collections >>> type(a_list)
Lists – integers can be arbitrarily large; Lists <class ’list’>
Tuples Tuples >>> a_list
Sets – floats are accurate to 15 decimal places. Sets [’1’, 1, ’a’, ’example’]
Dictionaries Dictionaries >>> a_list[0]
Strings
Operators (just a few) Strings
’1’
>>> a_list[-2]
Bytes Bytes
operators ’a’
References References [12:30]cazzola@ulik:~/esercizi-pa>
+ -
sum and subtraction operators
* ** But
product and power of operators, e.g., 2 ** 5 = 32 – negative numbers give access to the members backwards, i.e.,
/ // %
a_list[-2] == a_list[4-2] == a_list[2];
floating point and integer division and remainder opera-
tors respectively – the list is not fixed in size;
– the members are not homogeneous
Slide 5 of 22 Slide 6 of 22
Python Python
Datatypes Datatypes [14:13]cazzola@ulik:~/esercizi-pa>python3
>>> a_list = [’a’]
Walter Cazzola Walter Cazzola >>> a_list = a_list+[2.0, 3]
>>> a_list
[’a’, 2.0, 3]
Primitive Type A slice of a list can be yield by the [:] operator and specifying Primitive Type >>> a_list.append(True)
Boolean
the position of the first item you want in the slice and of the Boolean >>> a_list
Numbers Numbers [’a’, 2.0, 3, True]
Collections
first you want to exclude Collections
Lists Lists
[13:02]cazzola@ulik:~/esercizi-pa>python3 [’a’, 2.0, 3, True, ’four’, ’ ’]
Tuples Tuples
Sets >>> a_list=[1, 2, 3, 4, 5] Sets >>> a_list.insert(0, ’ ’)
>>> a_list[1:3] >>> a_list
Dictionaries Dictionaries
[2, 3] [’ ’, ’a’, 2.0, 3, True, ’four’, ’ ’]
Strings >>> a_list[:-2] Strings
Bytes [1, 2, 3] Bytes
Slide 7 of 22 Slide 8 of 22
Python’s Native Datatypes Python’s Native Datatypes
Lists: Introspecting on the List Lists: Removing Items from the List
Python Python
Datatypes Datatypes
Walter Cazzola You can check if an element is in the list Walter Cazzola
Strings Strings
Bytes
Look for a item position Bytes – value
References >>> a_list.index(3.14) References
>>> a_list.remove(3.14)
3 >>> a_list
[1, ’c’, ’cazzolaw’, 3.14]
Note that
>>> a_list.index(’walter’)
In both cases the list is compacted to fill the gap.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.index(x): x not in list
Slide 9 of 22 Slide 10 of 22
Python Python
Datatypes Datatypes
Slide 11 of 22 Slide 12 of 22
Python’s Native Datatypes Python’s Native Datatypes
Sets Sets: Modifying a Set
Python Python
Datatypes Datatypes
Slide 13 of 22 Slide 14 of 22
Python Python
Datatypes Datatypes
Slide 15 of 22 Slide 16 of 22
Python’s Native Datatypes Python’s Native Datatypes
Dictionaries Strings
Python
Datatypes
A dictionary is an unordered set of key-value pairs Python
Datatypes
Walter Cazzola
– when you add a key to the dictionary you must also add a value for Walter Cazzola
that key Python’s strings are a sequence of unicode characters.
Primitive Type – a value for a key can be changed at any time. Primitive Type
>>> s = ’The Russian for «Hello World» is «Привет мир»’
Boolean Boolean
>>> s
Numbers Numbers
>>> SUFFIXES = {1000: [’KB’, ’MB’, ’GB’, ’TB’, ’PB’, ’EB’, ’ZB’, ’YB’], ’The Russian for «Hello World» is «Привет мир»’
Collections ... 1024: [’KiB’, ’MiB’, ’GiB’, ’TiB’, ’PiB’, ’EiB’, ’ZiB’, ’YiB’]} Collections >>> s[34]
Lists >>> type(SUFFIXES) Lists ’П’
Tuples <class ’dict’> Tuples >>> s+’!!!’
Sets >>> SUFFIXES[1024] Sets ’The Russian for «Hello World» is «Привет мир»!!!’
Dictionaries [’KiB’, ’MiB’, ’GiB’, ’TiB’, ’PiB’, ’EiB’, ’ZiB’, ’YiB’] Dictionaries >>> s[34:44]
>>> SUFFIXES ’Привет мир’
Strings Strings
{1000: [’KB’, ’MB’, ’GB’, ’TB’, ’PB’, ’EB’, ’ZB’, ’YB’],
Bytes Bytes
1024: [’KiB’, ’MiB’, ’GiB’, ’TiB’, ’PiB’, ’EiB’, ’ZiB’, ’YiB’]}
References >>> SUFFIXES[1000] = [’kilo’, ’mega’, ’giga’, ’tera’, ’peta’, ’exa’, ’zetta’, ’yotta’] References
Strings behave as lists: you can:
>>> SUFFIXES – get the string length with the len function;
{1000: [’kilo’, ’mega’, ’giga’, ’tera’, ’peta’, ’exa’, ’zetta’, ’yotta’],
1024: [’KiB’, ’MiB’, ’GiB’, ’TiB’, ’PiB’, ’EiB’, ’ZiB’, ’YiB’]} – concatenate strings with the + operator;
– slicing works as well.
The syntax is similar to sets, but
– you list comma separate couples of key/value; Note that ", ’ and ’’’ (three-in-a-row quotes) can be used to
define a string constant.
– {} is the empty dictionary
Note that you cannot have more than one entry with the same
key.
Slide 17 of 22 Slide 18 of 22
Python Python
Datatypes Datatypes Split multi-line strings on the carriage return symbol.
Walter Cazzola Python 3 supports formatting values into strings. Walter Cazzola
>>> s = ’’’To be, or not to be: that is the question:
– that is, to insert a value into a string with a placeholder. ... Whether ’tis nobler in the mind to suffer
Primitive Type Primitive Type ... The slings and arrows of outrageous fortune,
Boolean Boolean ... Or to take arms against a sea of troubles,
Numbers
Looking back at the humanise.py example. Numbers ... And by opposing end them?’’’
>>> s.split(’\n’)
Collections Collections [’To be, or not to be: that is the question:’, "Whether ’tis nobler in the mind to suffer",
for suffix in SUFFIXES[multiple]:
Lists Lists ’The slings and arrows of outrageous fortune,’, ’Or to take arms against a sea of troubles,’,
size /= multiple
Tuples Tuples ’And by opposing end them?’]
if size < multiple:
Sets Sets
return ’{0:.1f} {1}’.format(size, suffix)
Dictionaries Dictionaries
Slide 19 of 22 Slide 20 of 22
Python’s Native Datatypes References
Bytes
Walter Cazzola The byte literal syntax (b’’) is used to define a bytes object. Walter Cazzola
– each byte within the byte literal can an ascii character or an encoded
hexadecimal number from \x00 to \xff.
Primitive Type
Boolean
Primitive Type
Boolean
I Jennifer Campbell, Paul Gries, Jason Montojo, and Greg Wilson.
Numbers >>> by = b’abcd\x65’ Numbers
Practical Programming: An Introduction to Computer Science Using
>>> by += b’\xff’
Collections
>>> by
Collections Python.
Lists Lists
Tuples
b’abcde\xff’
Tuples The Pragmatic Bookshelf, second edition, 2009.
>>> len(by)
I
Sets Sets
6
Dictionaries
>>> by[5]
Dictionaries Mark Lutz.
Strings 255 Strings Learning Python.
Bytes >>> by[0]=102 Bytes
Traceback (most recent call last): O’Reilly, third edition, November 2007.
References References
File "<stdin>", line 1, in <module>
TypeError: ’bytes’ object does not support item assignment I Mark Pilgrim.
bytearray(b’fbcde\xff’)
Dive into Python 3.
Bytes objects are immutable! Byte arrays can be changed. Apress*, 2009.
Slide 21 of 22 Slide 22 of 22