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
or
b = 10
float
a: float = 2.5
or
b = 5.2
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'
or
c = "Hello"
d = 'xin chào'
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
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:
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:
In tuple, you can unpack tuple value:
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:
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:
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)