Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
Python has several built-in data types that you can use to represent and manipulate data in your programs. In this tutorial, we will discuss the most commonly used Python data types.
Python has three numeric data types: integers, floating-point numbers, and complex numbers.
a) Integers:
Integers are whole numbers, either positive or negative, without any decimal point. In Python, you can represent integers as follows:
a = 5 b = -3 c = 0
b) Floating-point numbers:
Floating-point numbers are numbers that have a decimal point. They can also be expressed using scientific notation. In Python, you can represent floating-point numbers as follows:
a = 3.14 b = -0.5 c = 1.5e2 # 1.5 * 10^2 = 150.0
c) Complex numbers:
Complex numbers have a real part and an imaginary part, written as a + bj
, where a
and b
are real numbers, and j
is the imaginary unit. In Python, you can represent complex numbers as follows:
a = 2 + 3j b = 1 - 1j
Strings are sequences of characters enclosed in single quotes ('
), double quotes ("
), or triple quotes ('''
or """
). In Python, you can represent strings as follows:
a = 'Hello, World!' b = "Python is fun." c = '''This is a multiline string in Python.'''
Lists are ordered, mutable (changeable) sequences of items. Items can be of different data types. In Python, you can represent lists as follows:
a = [1, 2, 3, 4, 5] b = ['apple', 'banana', 'cherry'] c = [1, 'apple', 3.14, [2, 4, 6]]
Tuples are similar to lists, but they are immutable (cannot be changed). In Python, you can represent tuples as follows:
a = (1, 2, 3, 4, 5) b = ('apple', 'banana', 'cherry') c = (1, 'apple', 3.14, (2, 4, 6))
Dictionaries are unordered collections of key-value pairs. They are mutable and have unique keys. In Python, you can represent dictionaries as follows:
a = {'a': 1, 'b': 2, 'c': 3} b = {'name': 'Alice', 'age': 30, 'occupation': 'Engineer'}
Sets are unordered collections of unique items. They are mutable and do not allow duplicate items. In Python, you can represent sets as follows:
a = {1, 2, 3, 4, 5} b = {'apple', 'banana', 'cherry'}
Booleans represent the truth values True
and False
. They are often used in conditional statements and comparisons. In Python, you can represent booleans as follows:
a = True b = False
In conclusion, Python has several built-in data types, including numeric types (integers, floating-point numbers, and complex numbers), strings, lists, tuples, dictionaries, sets, and booleans.
Numeric data types in Python: int, float, complex:
int
), floating-point numbers (float
), and complex numbers (complex
).# Integer x = 5 # Float y = 3.14 # Complex z = 2 + 3j
String data type in Python and string manipulation:
str
) are sequences of characters. Python provides various string manipulation methods.# String text = "Hello, World!" # String manipulation upper_case = text.upper() lower_case = text.lower() substring = text[7:12]
List data type in Python and list operations:
list
) are ordered, mutable collections. Various list operations are available.# List numbers = [1, 2, 3, 4, 5] # List operations numbers.append(6) numbers.extend([7, 8]) numbers.pop(2)
Tuple data type in Python and tuple usage:
tuple
) are ordered, immutable collections.# Tuple coordinates = (3, 4) # Tuple usage x, y = coordinates
Dictionary data type in Python and dictionary operations:
dict
) are unordered collections of key-value pairs.# Dictionary person = {'name': 'Alice', 'age': 25} # Dictionary operations person['city'] = 'New York' del person['age']
Set data type in Python and set operations:
set
) are unordered collections of unique elements.# Set fruits = {'apple', 'banana', 'orange'} # Set operations fruits.add('kiwi') fruits.remove('banana')
Boolean data type in Python and boolean operations:
bool
) represent truth values: True or False.# Boolean is_python_fun = True is_learning = False # Boolean operations result = is_python_fun and not is_learning
Custom data types and user-defined classes in Python:
# Custom class class Point: def __init__(self, x, y): self.x = x self.y = y # Create an instance point1 = Point(3, 4)