DEV Community

Phúc Dương Minh
Phúc Dương Minh

Posted on

Python: All you should know about data type

Last post, I told you about variable. This post, I would like to tell you about data type.

Built-in Data Types

Python has these following data types:

Numberic

In Python, you don't need to declare data type for variable; but you can declare if you want and there are some numberic data type: interger: int, float: float, etc.

Interger

You can declare int like this:

a: int = 18
Enter fullscreen mode Exit fullscreen mode

or

b = 10
Enter fullscreen mode Exit fullscreen mode

float

a: float = 2.5
Enter fullscreen mode Exit fullscreen mode

or

b = 5.2
Enter fullscreen mode Exit fullscreen mode

String data type

Imagine string is a necklace of chars. String can contain anything like character, number (but it's a string of number). You can declare string by str:

a: str = "Hello"
b: str = 'xin chào'
Enter fullscreen mode Exit fullscreen mode

or

c = "Hello"
d = 'xin chào'
Enter fullscreen mode Exit fullscreen mode

Bool data type

Bool data type can use for for loops, while loops and if, elif; its only have two values are True and False.

Ex:

is_on: bool = True
# or
if_off = False
Enter fullscreen mode Exit fullscreen mode

Structures data types

Beside these data types above, Python has structures data types to store data sets: dictionary: dict, list: list, tuple: tuple and set: set.

List data type

We use list to store a list of objects in order and you can change (mutable) all items.

Ex:


# declaration list
fruits: list = ["apple", "banana", "mango"]
print(friuts)
print(fruits[1]) # you can use index with list
# or
numbers = [1, 2, 3, 4, 5]

# change list content
fruits[2] = "watermelon"
print(fruits)

# make empty list
empty_list = []
print(empty_list)
# or
empty_list1 = list()
print(empty_list1)
Enter fullscreen mode Exit fullscreen mode

["apple", "banana", "mango"]
"banana"
["apple", "banana", "watermelon"]
[]
[]

Tuple data type

Like list, tuple store a list of objecs; but we can't change the items (immutable) after created. You can declare tuple like this:


coordinates: tuple = (1, 2)
print(coordinates)
print(coordinates[0]) # you can use index with tuple like list but you can't change it
# or
ex_tuple = 1,
print(ex_tuple)

# make empty tuple
empty_tuple = ()
print(empty_tuple)
# or
empty_tuple1 = tuple()
print(empty_tuple1)
Enter fullscreen mode Exit fullscreen mode

(1, 2)
1
(1,)
()
()

In tuple, you can unpack tuple value:


x, y = coordinates
print(x, y)
Enter fullscreen mode Exit fullscreen mode

1 2

Dictionary data type

From tuple, in dictionary data type (dict), you put key and value in the curly braces (the colon ':' between key and value, key in the left and value in the right).

Ex:


menu = {
    "milk": 1.78,
    "biscuits": 2.00,
    "candy": 4.55
}
print(menu)

empty_dict = {}
print(empty_dict)

empty_dict1 = dict()
print(empty_dict1)

# you can change the value by key
menu["candy"] = 2.55
print(menu)

# delete data
del menu["candy"]
print(menu)
Enter fullscreen mode Exit fullscreen mode

{"milk": 1.78, "biscuits": 2.00, "candy": 4.55}
{}
{}
{"milk": 1.78, "biscuits": 2.00, "candy": 2.55}
{"milk": 1.78, "biscuits": 2.00}

Set data type

This is the final data type I tell you this post, Set data type (set) used to store non-duplicate data, you can change the value in it and if duplicate, don't worry, each value is only once. The data in set type must be immutable.

Ex:


fruits = {"apple", "banana", "watermelon"}
print(fruits)

empty_set = set()
print(empty_set)
# empty_set = {} is wrong because it is dict

fruits.add("orange")
print(fruits)
fruits.add("orange")
print(fruits)
Enter fullscreen mode Exit fullscreen mode

{"apple", "banana", "watermelon"}
set()
{"apple", "banana", "watermelon", "orange"}
{"apple", "banana", "watermelon", "orange"}

If you have any question about it, you can ask ChatGPT or another LLM. And if you have any comment, you can send a comment. ✌️

Top comments (0)