Python Datatypes
Variables can store data of different types, each data type has different characteristics and can do different things.
Everything is an object in Python programming, Python data types are classes and variables are instances (objects) of these classes
Python has the following data types built-in by default, in these categories:
* Text Type: `str`
* Numeric Types: `int`, `float`, `complex`
* Sequence Types: `list`, `tuple`, `range`
* Mapping Type: `dict`
* Set Types: `set`, `frozenset`
* Boolean Type: `bool`
* Binary Types: `bytes`, `bytearray`, `memoryview
Here is very brief summary of important data types
### Numberic types
* `Int`, or integer, is a whole number, positive or negative, without decimals
* `float`, or "floating point number" is a number, positive or negative, containing one or more decimals.
* Float can also be scientific numbers with an "e" to indicate the power of 10.
* `complex` numbers are written with a "j" as the imaginary part:
```python
x = 1 # int
y = 2.8 # float
z = 1j # complex
```
Casting
```python
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
```
### strings
The string is a sequence of characters. Python supports Unicode characters. Generally, strings are represented by either single or double-quotes.
```python
name = "techtrekking"
name = 'techtrekking'
```
### list
Python list is collection of values.
Main feature of python list is that, it can hold different types of datatypes in same list
```python
id_list = [ 11, 55, 'arya', 5.5 ]
```
### typle
Tuple is very simillar to list however it is immutable.
Another differentiation is that list is represented by square brackets `[]` and tuple by Parentheses `()`
```python
id_tuple = [ 11, 55, 'arya', 5.5 ]
```
### dictionary
Python Dictionary is an unordered sequence of data of key-value pair.
```python
team_data = {'first_name': 'arya', 'last_name' : 'stark', 'episode':'10'}
```
### boolean
boolean type stores either `True` or `False` value
```python
a = True
b = False
print(type(a)) # Output: <class 'bool'>
print(type(b)) # Output: <class 'bool'
print(9==8)
print(None==None)
```
### Setting specific datatype
Python will consider datatype based on value of variable however many times we need specific datatype and this can be explicitely assigned as below
```python
x = str("Hello World")
x = int(20)
x = float(20.5)
x = complex(1j)
x = list(("apple", "banana", "cherry"))
x = tuple(("apple", "banana", "cherry"))
x = range(6)
x = dict(name="John", age=36)
x = set(("apple", "banana", "cherry"))
x = frozenset(("apple", "banana", "cherry"))
x = bool(5)
x = bytes(5)
x = bytearray(5)
x = memoryview(bytes(5))
```
This post is written by Pravin
Tags :
Share it :