A data type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data.
Python is both compiled as well as an interpreted language. This means when we run a python code, it is first compiled and then interpreted line by line. The compilation part is mostly hidden from the user. While running the code, Python generates a byte code internally, this byte code is then converted using a python virtual machine to generate the output.
Python is a dynamically typed language, meaning Python recognizes the type of variable automatically. Therefore, the programmer does not need to define the data type when defining the variable. Python discerns the type of the variable from the value it is assigned. It is opposed to statically typed languages like C++ or JavaScript, which require the programmer to declare the type of the variable while defining it.
Variables in Python
- A variable name must start with a letter or the underscore character.
- A variable name can only contain alpha-numeric characters and underscores.
- Variable names are case-sensitive (meaning lcmcoding, and LCMcoding would both be different).
- A variable name cannot be any of the Python keywords.
Data Types in Python
- Numeric data types
It is used for storing numeric values. It is of following types:-
i. int -> holds integers. Eg: count = 5
ii. float -> holds floating precision numbers. Eg: temperature = 98.6
iii. complex -> holds complex numbers. Eg: current = 10+3j - Sequence data types
The sequence Data Type in Python is the ordered collection of similar or different data types. Sequences allow storing of multiple values in an organized and efficient fashion.
i. list -> ordered and mutable sequence. Eg: chapters = ["hcf", "lcm", "ratio", 99]
ii. tuple -> ordered and immutable sequence. Eg: chapters = ("hcf", "lcm", "ratio", 99)
iii. range -> returns a sequence of numbers from start to end position with a defined step. Eg: odd_naturals = range(1,10,2) {the values are 1, 3, 5, 7, 9}
iv. str -> a sequence of characters represented by either single or double quotes. Eg: name = "lcmcoding" - Mapping data types --> dict
A mapping type is a data type comprised of a collection of keys and associated values. Python’s only built-in mapping type is the dictionary. Dictionary is an unordered set of a key-value pair of items. It is like an associative array or a hash table where each key stores a specific value. Key can hold any primitive data type, whereas value is an arbitrary Python object. Eg: book_details = {"name": "Python Programming", "publisher": "lcmcoding publishers", "pages": 387} - Boolean data types --> bool
Bool holds either True or False. They are used to represent truth values. In numeric contexts, they behave like the integers 0 and 1, respectively. Eg: flag = True - Set data types
Python provides two ways for set-like objects:-
i. set -> mutable unordered collection of unique items. Eg: numbers = {1, 3, 6, 4, 10}
ii. frozenset -> immutable unordered collection of unique items. Eg: numbers = {1, 3, 6, 4, 10} - Binary data types
i. bytes -> used to convert an object to an immutable byte object of the given size and data. Eg: name = b'lcmcoding'
ii. bytearray -> a mutable sequence of integers in the range between 0 and 255. It allows you to work directly with binary data. It can be used to work with low-level data such as that inside of images or arriving directly from the network. Bytearray type inherits methods from both list and str types. This type can be used in lieu of a mutable string if needed. Eg: by_array = bytearray(7) {stored as bytearray(b'\x00\x00\x00\x00\x00\x00\x00')}
iii. memoryview -> A memoryview object exposes the C level buffer interface as a Python object which can then be passed around like any other object. Eg: memoryview(b"lcmcoding") {stored as <memory at 0x7fceda9f8b80>}
Python Type Conversion
- name = str("lcmcoding") {Specifying the type of variable}
- player = 0
player_as_str = str(player) {Modifying data type here, result will be "0"}
Getting the type of data
- name = "lcmcoding"
print(type(name)) { Result is <class 'str'>} - flag = False
print(type(flag)) { Result is <class 'bool'>}
Comments