Strings and Characters
Strings and characters are fundamental data types in Python. A character is essentially a string with a single number, letter, or special character such as ‘!’ or ‘@’. A string is a collection of characters that may or may not form readable words and sentences.
name = "Python Programming"
character = "P"
print(name) # Output: Python Programming
print(character) # Output: P
Numbers
Python supports four main types of numbers:
- Integer: Whole numbers without decimal points (e.g., 5, -3, 100)
- Long Integer: Very large whole numbers
- Float: Numbers with decimal points (e.g., 3.14, -2.5)
- Complex: Numbers with real and imaginary parts
integer_num = 42
float_num = 3.14
complex_num = 2 + 3j
print(type(integer_num)) # <class 'int'>
print(type(float_num)) # <class 'float'>
print(type(complex_num)) # <class 'complex'>
Collections
Collections are data types used when you want to store multiple items. Python offers four main collection types:
- List: Ordered, changeable, allows duplicates
- Dictionary: Ordered, changeable, key-value pairs
- Tuple: Ordered, unchangeable, allows duplicates
- Set: Unordered, changeable, no duplicates
# List example
fruits = ["apple", "banana", "cherry"]
# Dictionary example
person = {"name": "John", "age": 30, "city": "New York"}
# Tuple example
coordinates = (10, 20)
# Set example
unique_numbers = {1, 2, 3, 4, 5}
Date and Time
Date time data types are used for dates and times. While you can store dates as strings, using proper datetime objects provides built-in functionality for formatting, arithmetic, and manipulation.
from datetime import datetime, timedelta
# Current date and time
now = datetime.now()
print(now)
# Add one day
tomorrow = now + timedelta(days=1)
print(tomorrow)
# Format date
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_date)
Advantage: Using datetime objects instead of strings gives you automatic date arithmetic, formatting options, and timezone handling.



