Here are the main data types in Python, along with three examples of each:
- Integer (int) – These are whole numbers, both positive and negative.
a = 5
b = -10
c = 0
- Float – These are real numbers with decimal points.
a = 3.14
b = 2.5
c = -0.5
- Boolean (bool) – These are either True or False values.
a = True
b = False
c = 5 > 10
- String (str) – These are sequences of characters enclosed in quotation marks.
a = "Hello, World!"
b = 'Python is fun!'
c = "123"
- List – These are ordered collections of objects, which can be of any data type.
a = [1, 2, 3]
b = ['apple', 'banana', 'orange']
c = [True, 1, 'hello']
- Tuple – These are ordered collections of objects, like lists, but they are immutable (cannot be modified).
a = (1, 2, 3)
b = ('apple', 'banana', 'orange')
c = (True, 1, 'hello')
- Dictionary – These are collections of key-value pairs.
a = {'name': 'John', 'age': 30, 'city': 'New York'}
b = {'fruit1': 'apple', 'fruit2': 'banana', 'fruit3': 'orange'}
c = {'bool': True, 'num': 1, 'str': 'hello'}
- Set – These are unordered collections of unique elements.
a = {1, 2, 3}
b = {'apple', 'banana', 'orange'}
c = {True, 1, 'hello'}
The highly rated book to learn more about Python data types is “Python for Data Analysis” by Wes McKinney. It covers a wide range of topics related to data analysis using Python, including data types, data manipulation, and visualization. It’s written for intermediate-level programmers who are already familiar with Python basics and want to learn more about using Python for data analysis.