Python is a dynamically typed language, which means that the type of a variable is determined at runtime. Python has several built-in data types that are used to represent different kinds of data. In this article, we will discuss the different data types available in Python.
Python has the following built-in data types:
Numbers in Python can be of three types: integers, floating-point numbers, and complex numbers. Integers are whole numbers, while floating-point numbers have decimal points. Complex numbers are numbers with a real and imaginary part.
Here are some examples:
>>> x = 5
>>> y = 3.14
>>> z = 2 + 3j
Strings in Python are used to represent text. They are enclosed in either single quotes or double quotes. Strings are immutable, which means that once a string is created, it cannot be changed.
Here are some examples:
>>> s1 = 'Hello, world!'
>>> s2 = "Python is awesome"
Lists in Python are used to store a collection of items. They are ordered and mutable, which means that you can add, remove, or modify items in a list.
Here are some examples:
>>> mylist = [1, 2, 3, 4, 5]
>>> mylist.append(6)
>>> mylist.remove(3)
Tuples in Python are similar to lists, but they are immutable, which means that once a tuple is created, it cannot be changed. Tuples are often used to represent a collection of related values.
Here are some examples:
>>> mytuple = (1, 2, 3, 4, 5)
>>> x, y, z = mytuple
Sets in Python are used to store a collection of unique items. They are unordered and mutable, which means that you can add or remove items from a set.
Here are some examples:
>>> myset = {1, 2, 3, 4, 5}
>>> myset.add(6)
>>> myset.remove(3)
Dictionaries in Python are used to store a collection of key-value pairs. They are unordered and mutable, which means that you can add, remove, or modify items in a dictionary.
Here are some examples:
>>> mydict = {'name': 'John', 'age': 30, 'city': 'New York'}
>>> mydict['age'] = 31
>>> del mydict['city']
Python has several built-in data types that are used to represent different kinds of data. Understanding these data types is essential for writing Python programs.