Home/Blog/Python Data Types | Strings Numbers Collections
Python

Python Data Types | Strings Numbers Collections

Master strings, numbers, collections, and datetime for effective programming

Python Data Types | Strings Numbers Collections

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.

Frequently Asked Questions

Find answers to common questions

Python provides built-in functions for type conversion: str() converts to strings and int() converts to integers. To convert integers to strings, use str(number), which is useful for concatenation or formatted output. To convert strings to integers, use int(string_number), but ensure the string represents a valid integer or a ValueError will be raised. Handle conversion errors with try-except blocks to gracefully manage invalid input. Best practices include: always validating input before conversion (use regular expressions to check for numeric characters); using try-except blocks when converting user input; avoiding implicit conversions between incompatible types, as Python requires explicit conversions to prevent unexpected results. For example, in a web application receiving age input as a string, wrap the conversion in a try-except block: try: age = int(age_input) to store in the database, except ValueError: handle the invalid input appropriately. This approach ensures robust applications with proper error handling and type management.

Automate Your IT Operations

Leverage automation to improve efficiency, reduce errors, and free up your team for strategic work.