Loading...
Loading...
00:00:00

Python Data Types

Data Type In Python

In python, There are many data built in python such as int, float, double, lists, tuples, sets, dictionaries etc . Each Data types belongs to specific class and hold the particular types of data. we can convert their data types into another data types using type casting, if their type casting is possible. All Data types has class in python and it has serveral methods .

In Python, data types are the classification or categorization of data items. Every value in Python has a data type, which determines the type of operations that can be performed on the value and the amount of memory that is required to store the value.

Here are the most commonly used data types in Python:

  • int (integer): Represents positive or negative whole numbers (e.g., 42, -7).
  • float (floating-point decimal): Represents real numbers (e.g., 3.14, -0.01).
  • complex (complex number): Represents complex numbers (e.g., 3 + 4j).
  • str (string): Represents sequences of characters (e.g., "hello", "goodbye").
  • bool (boolean): Represents truth values (True or False).
  • list: Represents a collection of values that can be of different data types (e.g., [1, 2, 3, "apple", "banana"]).
  • tuple: Represents a collection of values that can be of different data types (e.g., (1, 2, 3, "apple", "banana")). Tuples are similar to lists, but unlike lists, tuples are immutable, which means their values cannot be changed once they are created.
  • set: Represents a collection of unique values of any data type (e.g., {1, 2, 3, "apple", "banana"}).
  • dict (dictionary): Represents a collection of key-value pairs (e.g., {"name": "John", "age": 30}).

You can use the type() function to check the data type of a value in Python. For example:

print(type(42))       # <class 'int'>
print(type(3.14))     # <class 'float'>
print(type("hello"))  # <class 'str'>
# create variable and check data types
a = 100
b = "100"
c = 45.8
d = True
e = "hello world"
f = (23, 89, -38, "python")
g = [35, 87, 2, 83, 45, 27]
h = {100: "python", 200:"php", 300: "java", 400: "C++"}
print("type of ", a ,"is",type(a))
print("type of ", b ,"is",type(b))
print("type of ", c ,"is",type(c))
print("type of ", d ,"is",type(d))
print("type of ", e ,"is",type(e))
print("type of ", f ,"is",type(f))
print("type of ", g ,"is",type(g))
print("type of ", f ,"is",type(f))

In this example, the type() function is used to check the data type of the values 42, 3.14, and "hello", which are of type int, float, and str, respectively.

Set And Get The Data Types

In python, There is no need to define the data types of variables. When we assign the value in variables python automatically defined the data types. we can check the data types using type() function .

Here is the list of data types that you can use

  • int
  • float
  • str
  • bool
  • list
  • tuple
  • set
  • dict
  • complex
  • range
  • frozen set
  • bytes
  • bytearray
  • memoryview