Python Code Examples from A to Z: A Comprehensive Overview

  • Share this:

Code introduction


The following is a collection of Python code examples using built-in libraries from A to Z.


Technology Stack : os, re, sys, json, time, random, string, math, cmath, datetime

Code Type : Code collection

Code Difficulty : Intermediate


                
                    
import os
import re
import sys
import json
import time
import random
import string
import math
import cmath
import datetime

def generate_random_string(length=10):
    letters = string.ascii_letters
    return ''.join(random.choice(letters) for i in range(length))

def get_file_size(file_path):
    if not os.path.exists(file_path):
        return "File does not exist"
    return os.path.getsize(file_path)

def extract_emails(text):
    email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}'
    return re.findall(email_pattern, text)

def sys_exit_code(code=0):
    sys.exit(code)

def parse_json(json_string):
    try:
        return json.loads(json_string)
    except json.JSONDecodeError:
        return "Invalid JSON"

def get_current_time():
    return datetime.datetime.now()

def calculate_circle_area(radius):
    return math.pi * (radius ** 2)

def complex_number_operations(a, b):
    return cmath.rect(a, b)