Setting specific data type in Python

This code demonstrates how to convert between different data types in Python.

  1. The variable a is assigned the value 5 cast as an integer data type using int(5).
  2. The variable b is assigned the value 5.5 cast as a floating-point data type using float(5.5).
  3. The variable c is assigned the value 1j cast as a complex data type using complex(1j).
  4. The variable d is assigned the value "hello" cast as a string data type using str("hello").
  5. The variable e is assigned the value True cast as a boolean data type using bool(True).
  6. The variable f is assigned the value (1, 2, 3) cast as a list data type using list((1, 2, 3)).
  7. The variable g is assigned the value (1, 2, 3) cast as a tuple data type using tuple((1, 2, 3)).
  8. The variable h is assigned the value (1, 2, 3) cast as a set data type using set((1, 2, 3)).
  9. The variable i is assigned the value {1: 2, 2: 3} cast as a dictionary data type using dict(val1 = 2, val2 = 3).
  10. The variable j is assigned the value {"first", "second", "third"} cast as a frozen set data type using frozenset(("first", "second", "third")).
  11. The variable k is assigned the value b"Hello" cast as a byte data type using bytes(b"Hello").
  12. The variable l is assigned the value 2 cast as a bytearray data type using bytearray(2).
  13. The variable m is assigned the value 2 cast as a memoryview data type using memoryview(bytes(2)).

Finally, for each variable, the type function is used to print the data type of the value stored in the variable.

a = int(5)
b = float(5.5)
c = complex(1j)
d = str("hello")
e = bool(True)
f = list((1, 2, 3))
g = tuple((1, 2, 3))
h = set((1, 2, 3))
i = dict(val1 = 2, val2 = 3)
j = frozenset(("first", "second", "third"))
k = bytes(b"Hello")
l = bytearray(2)
m = memoryview(bytes(2))

<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'bool'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
<class 'frozenset'>
<class 'bytes'>
<class 'bytearray'>
<class 'memoryview'>