DEV Community

Cover image for Python Data Types
almasi-y
almasi-y

Posted on

Python Data Types

PYTHON DATA TYPES😄

Types of data types

1️⃣ Numeric data types
int

  • An integer, is a whole number, positive or negative, without decimals, of unlimited length.
 numbers = 20
 print(numbers)
 print(type(numbers)) 
Enter fullscreen mode Exit fullscreen mode

float

  • A float is a number, positive or negative, containing one or more decimals.
 my_float = 20.5
 print(float)
 print(type(float)) 
Enter fullscreen mode Exit fullscreen mode

complex

  • A complex is a number made up of some parts that are unknown
 my_complex = 1j
 print(my_complex)
 print(type(my_complex)) 
Enter fullscreen mode Exit fullscreen mode

2️⃣ Sequence Types
lists

  • Lists are used to store multiple items in a single variable.
  • Take note of the square brackets
  • Unlike a set, a list can allow duplicate values
  • They can contain different data types
 fruits = ["apple", "banana", "cherry"]
 print(fruits)
 print(type(fruits)) 
Enter fullscreen mode Exit fullscreen mode

tuple

  • Tuples are used to store multiple items in a single variable.
  • Take note of the round brackets
  • Unlike lists, tuples are unchangeable.
  • Allows duplicate members.
 cars = ("toyota", "porsche", "mercedes")
 print(cars)
 print(type(cars)) 
Enter fullscreen mode Exit fullscreen mode

range

  • A range is a sequence of numbers starting from 0 by default and increments by one
 my_range = range(6)
 print(my_range)
 print(type(my_range)) 
Enter fullscreen mode Exit fullscreen mode

3️⃣ Mapping types

dict

  • Dictionaries are used to store data values in key:value pairs.
  • Dictionaries are ordered and changeable
  • Like sets they do not allow duplication of items
 my_dict = {"name" : "John", "age" : 36}
 print(my_dict)
 print(type(my_dict)) 
Enter fullscreen mode Exit fullscreen mode

4️⃣ Set types
set

  • Sets are used to store multiple items in a single variable.
  • Sets are unindexed, unordered and unchangeable however you can add or remove items from a set
  • Sets also do not allow duplicates
  • Take note of the curly braces
my_set = {"gold", "silver", "titanium"}
print(my_set)
print(type(my_set)) 
Enter fullscreen mode Exit fullscreen mode

frozen set

  • Similar to a set but completely immutable; you cannot add or remove items
my_frozenset = frozenset({"apple", "banana", "cherry"})
print(my_frozenset)
print(type(my_frozenset)) 
Enter fullscreen mode Exit fullscreen mode

5️⃣ Boolean types
boolean

  • A boolean is a value that can be true or false
my_bool = True
print(my_bool)
print(type(my_bool)) 
Enter fullscreen mode Exit fullscreen mode

6️⃣ Binary types
bytes

  • Bytes represent an immutable sequence of 8-bit values
z = b"Hello"
print(z)
print(type(z)) 
Enter fullscreen mode Exit fullscreen mode

bytesarray

  • Bytesarray represents a mutable sequence of 8-bit values.
my_bytearray = bytearray(5)
print(my_bytearray)
print(type(my_bytearray)) 
Enter fullscreen mode Exit fullscreen mode

memoryview

  • A memoryview object provides a view into the memory of a bytes-like object (like bytes or bytearray) without copying the underlying data.
my_memoryviews = memoryview(bytes(5))
print(my_memoryviews)
print(type(my_memoryviews)) 
Enter fullscreen mode Exit fullscreen mode

7️⃣ None types
NoneTypes

  • This is simply a data type that represents an absence of value
z = None
print(z)
print(type(z)) 
Enter fullscreen mode Exit fullscreen mode

8️⃣ Text types
string

  • A string is a collection of characters
greetings = "Hello World"
print(greetings)
print(type(greetings)) 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)