Data Types in Python 3
I just realised that we're putting off an important discussion: data types. This determines what types of variables we'll have and how data interacts with each other inside python.
Now usually basic data is grouped into Numbers, Text, Truth value (Boolean) and Lists/Collections.
I'm listing the official python names for the data types which are supported by python core. All data types will be stored as objects unlike C or Java, there are no primitive data types.
- Boolean - represented by
True
orFalse
- Numbers (int, float and complex) - integers and decimal numbers
- Strings (str) - Sequence of unicode characters in python 3, behaviour changes for 2.x
- Bytes (bytes, byte array) - Raw binary data, like images.
- Lists & Tuples (list, tuple and range) - list of other data types, full chapter is written for it.
- Sets (set) - list of distinct values, see specific chapter for more
- Dictionaries (dict) - list of key:value pairs; where keys in a dict should be distinct and made of numbers or strings and not lists or sets
Boolean:
The truth of any statement can be verified into True
or False
, these are constants in Python which can be used where needed. Booleans also result in any comparison or any other expressions. Booleans are constructed as a subtype of integer and True is 1 and False is 0.
>>>print(3>4)
False
>>>print(5>4)
True
Numbers:
int is an integral number with no decimal point. Python 3 doesn't differentiate integers into many data types based on size, there are no long and int. Just int and int is limitless.
float is a real number with a decimal point, the precision and limit of float is device based and the information can be obtained using sys.float_info.
>>> myInt = 21
>>> myInt
21
>>> myFloat = 2.1
>>> myFloat
2.1
Complex numbers are numbers which have both real and imaginary components, You can go here & here for explanation on what is real and what is imaginary but I don't think we'll ever encounter complex arithmetic in any real scenario. In any case, read about complex numbers when you have ton of time to burn. When you're sufficiently confused, let's take a break and move on to Strings.
Strings:
Strings are immutable array of unicode characters. They can be enclosed using single or double quotes.
firstName = "Alan"
lastName = 'Francis'
You can access specific characters by using
>>>firstName[3]
n
You can concat strings using the + operator
>> firstName+' '+lastName
Alan Francis
We'll see Bytes, Lists, Sets and Dictionaries later on in their own chapters.
References:
- https://docs.python.org/3.4/library/stdtypes.html#built-in-types
- https://docs.python.org/3.4/library/stdtypes.html#boolean-values
- https://docs.python.org/3.4/library/stdtypes.html#numeric-types-int-float-complex
- https://docs.python.org/3.4/library/stdtypes.html#text-sequence-type-str
- https://docs.python.org/3.4/library/stdtypes.html#binary-sequence-types-bytes-bytearray-memoryview
- https://docs.python.org/3.4/library/stdtypes.html#sequence-types-list-tuple-range
- https://docs.python.org/3.4/library/stdtypes.html#set-types-set-frozenset
- https://docs.python.org/3.4/library/stdtypes.html#mapping-types-dict
- https://en.wikipedia.org/wiki/Complex_number
- https://en.wikiversity.org/wiki/Complex_Numbers
- http://www.diveintopython3.net/strings.html
- https://en.wikibooks.org/wiki/Python_Programming/Data_Types
- http://www.w3resource.com/python/python-data-type.php
No comments:
Post a Comment