Python Functions for Summation and Squaring

  • Share this:

Code introduction


The function takes two arguments and returns the sum of the first argument and the square of the second argument.


Technology Stack : os, re, sys, time, json, random, math

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
import os
import re
import sys
import time
import json
import random
import math

def read_file(file_path):
    with open(file_path, 'r') as file:
        return file.read()

def extract_emails(text):
    email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
    return re.findall(email_pattern, text)

def list_files(directory):
    return [file for file in os.listdir(directory) if os.path.isfile(os.path.join(directory, file))]

def is_leap_year(year):
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

def generate_random_string(length, letters=True, digits=True, special_chars=True):
    letters_str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    digits_str = '0123456789'
    special_chars_str = '!@#$%^&*()-_=+[{]}\\|;:\'",<.>/?'
    all_chars = ''.join([letters_str, digits_str, special_chars_str]) if special_chars else ''.join([letters_str, digits_str])
    return ''.join(random.choice(all_chars) for i in range(length))

def sleep_seconds(seconds):
    time.sleep(seconds)

def calculate_square_root(number):
    return math.sqrt(number)

def load_json(file_path):
    with open(file_path, 'r') as file:
        return json.load(file)

def random_choice(choices):
    return random.choice(choices)

def get_system_platform():
    return sys.platform

def current_time():
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())

def xxx(arg1, arg2):
    """
    This function takes two arguments and returns the sum of the first argument and the square of the second argument.
    """
    return arg1 + arg2**2