Python:DataTypes

From wiki
Revision as of 23:11, 31 December 2017 by Hdridder (talk | contribs) (Created page with "=Object types= Objects are iterable if they can contain more than 1 ordered objects (string, list, tuple, dict) Objects are mutable if their content can be changed (list, set,...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Object types

Objects are iterable if they can contain more than 1 ordered objects (string, list, tuple, dict) Objects are mutable if their content can be changed (list, set, dict)

Note: Variables are pointers to objects, not the object itself.

list

Class of iterable, mutable objects. Lists can be compared to arrays in other languages.

lst1 = []
Initialize an empty list
lst1.append(2)
Add the '2' object to the end of lst1

set

Class of non iterable, mutable objects. Sets cannot hold duplicate objects. Checking if a set holds an object is very fast.

set1 = set()
Initialize an empty set
set1.add(2)
Add the '2' object to set1

Tuple

Class of iterable, immutable objects. Results from database queries are by default returned as tuple.

tpl1 = ()
Initialize an empty tuple

Dictionary or dict

Class of iterable, mutable objects. Dictionary's can be compared to perl hashes.

dict1 = {}
Initialize an empty dictionary.


Slicing

You can address all iterable datatypes partly or in a difference sequence.

object[b:e:s]
Generic format where b=Begin (counting starts at 0), e=End, s=Stepsize (negative stepsize starts counting at the end)