Difference between revisions of "Python:DataTypes"

From wiki
Jump to navigation Jump to search
(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,...")
 
Line 1: Line 1:
 
=Object types=
 
=Object types=
Objects are iterable if they can contain more than 1 ordered objects (string, list, tuple, dict)
+
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)
 
Objects are mutable if their content can be changed (list, set, dict)
  
Line 6: Line 6:
  
 
==list==
 
==list==
Class of iterable, mutable objects. Lists can be compared to arrays in other languages.
+
Class of iterable, mutable objects. Lists can be compared to arrays in other languages. Lists can contain a mixture of all kind of objects.
  
 
;lst1 = []
 
;lst1 = []
Line 15: Line 15:
  
 
==set==
 
==set==
Class of non iterable, mutable objects. Sets cannot hold duplicate objects. Checking if a set holds an object is very fast.
+
Class of non iterable, mutable objects. Objects added to sets are hashed. Therefor:
 +
* Only immutable objects can be added to a set.
 +
* Sets cannot hold duplicate objects (adding an objects again does not change the set.
 +
* Checking if a set holds an object is very fast.
  
 
;set1 = set()
 
;set1 = set()
Line 35: Line 38:
 
:Initialize an empty dictionary.
 
:Initialize an empty dictionary.
  
 
+
Code example:
 +
<syntaxhighlight lang=python>
 +
dict = {}
 +
dict["name1"] = {}
 +
dict["name1"]["street"] = "mystreet"
 +
 +
for name in dict:
 +
  print name
 +
  for key2 in dict[name]:
 +
      print key2,dict[name][key2]
 +
 +
for name in dict:
 +
  print name
 +
  for key2 in sorted(dict[name].keys()):
 +
      print key2,dict[name][key2]
 +
</syntaxhighlight>
  
 
=Slicing=
 
=Slicing=

Revision as of 23:31, 31 December 2017

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. Lists can contain a mixture of all kind of objects.

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

set

Class of non iterable, mutable objects. Objects added to sets are hashed. Therefor:

  • Only immutable objects can be added to a set.
  • Sets cannot hold duplicate objects (adding an objects again does not change the set.
  • 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.

Code example:

dict = {}
dict["name1"] = {}
dict["name1"]["street"] = "mystreet"
 
for name in dict:
   print name
   for key2 in dict[name]:
      print key2,dict[name][key2]
 
for name in dict:
   print name
   for key2 in sorted(dict[name].keys()):
      print key2,dict[name][key2]

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)