GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||
GeezerVison is showing jenkov.com — Python Data Types Python Data Types
The values referenced by Python variables all have a data type. The variable itself may not be locked to a specific data type, but the value it points to will have a data type. Python comes with a set of built-in core Python data types. These data types are:
Python data types allow you to perform certain data type specific operations on each data type. Thus, it is good to know the built-in core Python data types. Each of these Python data types are covered in more detail below, and some of them possibly even in more detail in their own tutorials. Python Boolean
The boolean (bool) Python data type represents a value that is either Here are two examples of setting a Python variable to a boolean value: mybool_1 = True mybool_2 = False Python NumericThe numeric Python data type represents a numeric value. A numeric value in Python can be either an integer, a floating point or a complex number. Here are two examples setting a Python variable to an integer value and a floating point value: myint = 123 myfloat = 123.45 Python StringThe string Python data type represents a textual value. A string in Python can contain Unicode characters. Here is an example of setting a Python variable to a string value: mystr = "This is a text in Unicode" Python also supports multi-line strings. Here is an example of setting a Python variable to a multi-line string value: my_ml_str = '''This is
a multi-line
string in Python
'''
Python ListThe list Python data type represents a list of elements, such as a list of values or objects. The elements stored in a Python list are stored in sequential order, and can be accessed in that same sequential order. Each element in a Python list has an index which is the element's position in the sequence of elements in the list. The list element indexing starts from 0. You can access an element in a Python list directly via its index. Here is an example of creating a list in Python, and accessing its elements via their indexes: mylist = ["Joe", "Mehdi", "Miranda"] joe = mylist[0] mehdi = mylist[1] miranda = mylist[2] Python TupleThe tuple Python data type represents an immutable list of values or objects. A Python tuple is thus very similar to a Python list - with the exception that a tuple cannot add, replace or remove elements. Here is an example of creating a Python tuple: mytuple = (1,3,4, "text") Notice the parentheses around the values in the tuple declaration. This is different from list declarations which use square brackets. You index into a Python tuple in the same way you index into a Python list. Here is an example of accessing elements in a Python tuple via their indexes: mytuple = ("Joe", "Mehdi", "Miranda")
joe = mytuple[0]
mehdi = mytuple[1]
miranda = mytuple[2]
Python SetThe set Python data type represents a set of unique elements, meaning each element only occurs once in the set. In a list of tuple, the same element could potentially occur more than once. Here are some examples of creating a Python set: myset1 = set()
myset2 = set("one element")
myset3 = set(["one", "two", "three"])
myset4 = set([1, 2, "three", "four"])
Notice the call to the standard Python method The order of the elements in the set is not guaranteed (undefined), so you cannot index into the set like you can with a tuple or list. Python DictionaryThe dictionary Python data type represents a set of (key,value) pairs. You use the key to both store and retrieve the corresponding value in the dictionary. Here is an example of creating an Python dictionary and inserting values into it: mydict1 = {}
mydict2 = { "key1" : "value1", "key2" : 123 }
You can access the elements of a Python dictionary like this: mydict2 = { "key1" : "value1", "key2" : 123 }
myvalue = mydict2["key2"]
Data Types in Other LanguagesFor the curious reader, here are links to tutorials about data types in all the programming languages covered here at Jenkov.com: Next: Python If
Close TOC
All Trails Trail TOC Page TOC Previous Next | ||||||
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | ||||||