Python supports several built-in data types that are integral part of programming language. Everything in Python is object. Built-in data types are object type of data. They are the easiest way to start using Python because of its simplicity. If you need complex types you will use instances of you own class.
Python is dynamically and strongly typed language at the same time. What does that mean? Dynamically corresponds to fact that you don’t need to explicitly defined type of the variable that is assigned to value. Just populate the value with the desired data type. Strong means once the variable is assigned you can only do operations related with data type. For example integer variable can do only numeric tasks and string variable can do only string operations.
Let’s see what kind of data types Python has:
- Numeric data types (int, float, long, complex)
- Sequence types (strings, lists, tuples, ranges, bytes, bytearray)
- Mappings (dictionaries)
- Function, Modules, Classes
Numeric data types
Numeric data types in Python 3.x are divided into three main types: integer, float and complex.
Integer are positive and negative whole numbers including zero. You can store numbers between –9223372036854775808 and 9223372036854775807.
Floats are positive and negative decimal numbers and complex numbers. You can store numbers, ranging from 2.2250738585072014 x 10308 to 1.7976931348623157 x 10308.
Complex numbers include real component and imaginary part j.
Integer numbers can be also represented by hexadecimal (base16), octal (base 8) or binary (base2) format. Hexadecimal numbers are represented by hexadecimal digits (A-F, 0-9). Octal start with 0o or 0O followed by digits (0-7). Binary have 0 and 1.
Sequence types (Strings, Lists, Tuples, Ranges, Bytes, ByteArray)
Sequence types include all data types that are ordered in position in the way they can be indexed and sliced. Indexed means that each element can be accessed by its index. Slicing the sequence takes only part of the sequence.
Text sequence types (Strings)
Strings represent text sequence type and one of the most popular data types to operate. Strings in Python 3.x are array bytes representing Unicode characters. Array of bytes? Yes, they are array of bytes with Unicode representation. It is your choice which type of encoding you will use. Encoding and decoding is done automatically while reading and writing data. Default encoding is UTF-8.
Strings are immutable which means that original string objects cannot be modified in any way. Check later in text details about this feature. Strings can be indexed and sliced which is one of a great way to manipulate strings. Because strings are objects it use their specific built-in method like upper, lowercase, split, encode and many more. Those methods are bound to string type of data only.
Binary sequence types
Bytes and bytearray are part of binary sequence types in Python. Both types of binary representation of string type of data.
Python 3 has a clear difference between the sequence of characters and the sequence of raw data machine format – bytes. Bytes are immutable string representations of data. Bytes can be stored in directly in memory but strings could not, because byte are machine representation of data. String must be encoded first to store its values into memory. The range bytes can display data is ASCII code format which means you can display integers from 0 to 256. If Unicode string is represented by ‘abc’ then bytes are represented by b’abc’. Let’s check the connection in the following figure:
Very similar to string but with one major difference. Bytearrays are mutable but also raw type of data. It can be very handy to use if you need to change a lot of little chunks in memory without a need to copy the data but to replace data in place like database engines and image libraries. The range bytes can display data is ASCII code format which means you can display integers from 0 to 256.
Lists are collections of sequential (ordered) data that do not have a limited number. They are one of the most commonly used data types and, and unlike strings, are mutable. Which means that every element of the list can be changed. List can contain any type of data that is dynamically typed which means that you can create list with different types (strings, integers, bytes, etc). Lists can be nested and can be used to create matrices with duplicated data.
Tuples are similar to lists expect they are immutable and cannot be dynamically changed.
Ranges are unordered sequence of integer numbers. Range has three parameters: start, end and step. For example range (1,10, 2) means even numbers from 1 to 10. Usually used as shortcut in for loops.
Dictionaries are something completely different than lists. While lists are indexed by position, dictionaries are accessed by its key. They are unordered which means that cannot be indexed. Dictionaries always stores two values which are regularly called key-value pair, where all keys have their values. This is the reason why dictionaries reside in mapping category. Dictionaries share some characteristics with lists: they are mutable, dynamic and can be nested. Very useful when you want to describe properties of some objects.
Sets are a collection of unordered collection of data which can grow and shrink on demand. They differ from lists and dictionaries just because of its features: unordered and not mapping type. They hold unique collection of objects and usually used with mathematical operations like union and difference. Because sets are not ordered their elements cannot be accessed with index but major advantage of sets are their easy way to confirm if object is part of set because it maintains hash table of objects. Another type of immutable set is frozen set.
Frozen Sets are immutable types of Sets.
Because everything in Python is object, there are several types included like objects, methods, functions, files and types.
Immutable vs Mutable
All these Python built-in data types are sharing or not sharing some common properties like: mutable, immutable, dynamic, ordered or unordered. Mutability is the reason why we have many more Python types. Built-in types (int, float, bool, str, tuple, unicode) are immutable. Built-in types (list, set, dict) are mutable. Mutable objects can be changed while immutable objects can’t. Best way to identify object in memory is to use id() functions which return integer number of object located in memory.
- Access to Immutable objects is quicker than mutable
- Mutable objects are used when you need to change size of the object
- Immutable objects are used when you want to ensure that object is always the same
- Changing the immutable objects involves much expensive operation because you have to create copy of existing object
|List||Sequence||Collection of things||Yes||Yes||Yes|
|Tuple||Sequence||Collection of things||No||Yes||No|
|Range||Sequence||Collection of numbers||No||Yes||No|
|Set||Sequence||Collection of things||Yes||No||Yes|
|Frozen Set||Sequence||Collection of things||No||No||Yes|
|Dictionary||Mapping||Mapping between things||Yes||No||Yes|
Python operators are divided into several major categories:
- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Assignment operators
- Identity operators
- Membership operators
|+||Addition||x + y|
|–||Subtraction||x – y|
|*||Multiplication||x * y|
|/||Division||x / y|
|%||Modulus||x % y|
|**||Exponentiation||x ** y|
|//||Floor division (integer division)||x // y|
|==||If values of two operands are equal, result is true||a == b is TRUE|
|!=||If values of two operands are not equal, result is true||a != b IS TRUE|
|<>||If values of two operands are not equal, result is true||a <> b IS TRUE|
|>||If the value of left operand is greater than the value of right operand, then condition becomes true.||a>b IS TRUE|
|<||If the value of left operand is less than the value of right operand, then condition becomes true.||a<b IS TRUE|
|>=||If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.||a>=b IS TRUE|
|<=||If the value of left operand is less than or equal to the value of right operand, then condition becomes true.||a<=b IS TRUE|
|AND||If both operands are TRUE, result is TRUE||a AND b|
|OR||If any of operands are TRUE, results is TRUE||a OR b|
|NOT||Used to reverse the logical state of its operands||NOT (a AND b)
NOT (a OR b)
Returns 1 if bits on same position are 1
|x & y|
Returns 0 if bits on same position are 0
|x | y|
Inverts all bits
Returns 1 if any of bits on same position is 1
|x ^ y|
|>>||Bitwise right shift
Shifts 1 bit from left to right
|<<||Bitwise left shift
Shifts 1 bit from right to left
|=||x = 1||x = 1|
|+=||x += 2||x = x + 2|
|-=||x -= 1||x = x – 1|
|*=||x *= 2||x = x * 2|
|/=||x /= 1||x = x / 1|
|%=||x %= 2||x = x % 2|
|//=||x //= 1||x = x // 1|
|**=||x **= 2||x = x ** 2|
|&=||x &= 1||x = x & 1|
||=||x |= 2||x = x | 2|
|^=||x ^= 1||x = x ^ 1|
|>>=||x >>= 2||x = x >> 2|
|//=||x //= 1||x = x // 1|
|IS||TRUE if operands are same||X is TRUE|
|IS NOT||TRUE if operands are not same||X is NOT TRUE|
|IN||Returns TRUE if specified value is present in object||x in y|
|NOT IN||Returns TRUE if specified value is not present in object||x not in y|