All you need to know about Python datatypes

Python is a dynamically-typed language, which means that it automatically infers the data type of a variable based on the value that’s assigned to it. Here are the main data types in Python:

  1. Numbers: This includes integers, floats, and complex numbers. Integers are whole numbers, floats are numbers with decimal points, and complex numbers are numbers with a real and imaginary component. For example:
codex = 5          # This is an integer
y = 3.14       # This is a float
z = 2 + 3j     # This is a complex number
  1. Strings: This is a sequence of characters enclosed in quotes. Strings can be manipulated using various string operations. For example:
s = "Hello, World!"   # This is a string
  1. Booleans: This is a binary value that can be either True or False. Booleans are often used in conditional statements and loops. For example:
pythonCopy codea = True        # This is a boolean
b = False       # This is also a boolean
  1. Lists: This is an ordered collection of items, enclosed in square brackets and separated by commas. Lists can contain any type of data, and their items can be accessed using indexing. For example:
lst = [1, 2, "three", 4.0]     # This is a list
  1. Tuples: This is an ordered collection of items, enclosed in parentheses and separated by commas. Tuples are similar to lists, but they are immutable, meaning they cannot be changed once they are created. For example:
tup = (1, 2, "three", 4.0)     # This is a tuple
  1. Sets: This is an unordered collection of unique items, enclosed in curly braces and separated by commas. Sets can contain any type of data, but they cannot contain duplicates. For example:
s = {1, 2, 3, 4}     # This is a set
  1. Dictionaries: This is an unordered collection of key-value pairs, enclosed in curly braces and separated by commas. Dictionaries can be used to store and retrieve data using a key. For example:
pythonCopy codedict = {"name": "John", "age": 30, "city": "New York"}     # This is a dictionary

I hope this overview helps! Let me know if you have any other questions.

Leave a Reply