2. Python Data Types

2. Python Data Types

Built-in Data Typesbr In programming, data type is an important concept.br br Variables can store data of different types, and different types can do different things.br br Python has the following data types built-in by default, in these categories:br br Text Type: strbr Numeric Types: int, float, complexbr Sequence Types: list, tuple, rangebr Mapping Type: dictbr Set Types: set, frozensetbr Boolean Type: boolbr Binary Types: bytes, bytearray, memoryviewbr None Type: NoneTypebr Getting the Data Typebr You can get the data type of any object by using the type() function:br br ExampleGet your own Python Serverbr Print the data type of the variable x:br br x = 5br print(type(x))br Setting the Data Typebr In Python, the data type is set when you assign a value to a variable:br br Example Data Type Try itbr x = "Hello World" str br x = 20 int br x = 20.5 float br x = 1j complex br x = ["apple", "banana", "cherry"] list br x = ("apple", "banana", "cherry") tuple br x = range(6) range br x = {"name" : "John", "age" : 36} dict br x = {"apple", "banana", "cherry"} set br x = frozenset({"apple", "banana", "cherry"}) frozenset br x = True bool br x = b"Hello" bytes br x = bytearray(5) bytearray br x = memoryview(bytes(5)) memoryview br x = None NoneType br ADVERTISEMENTbr br Setting the Specific Data Typebr If you want to specify the data type, you can use the following constructor functions:br br Example Data Type Try itbr x = str("Hello World") str br x = int(20) int br x = float(20.


User: Hacker Experts

Views: 8

Uploaded: 2024-07-01

Duration: 04:46