1. Find the Greatest Common Divisor (GCD) of Multiple Numbers:
import math
numbers = [24, 36, 48, 60, 72]
gcd = math.gcd(*numbers)
print("GCD of the numbers:", gcd)
2. Calculate the Standard Deviation of a List of Numbers:
import statistics
data = [1, 2, 3, 4, 5]
std_dev = statistics.stdev(data)
print("Standard deviation:", std_dev)
3. Generate a Random Password with Specific Requirements:
import random
import string
def generate_password(length, include_digits=True,
include_special_chars=True):
characters = string.ascii_letters
if include_digits:
characters += string.digits
if include_special_chars:
characters += string.punctuation
password = ''.join(random.choice(characters) for _ in range(length))
return password
password_length = 12
print("Generated password:", generate_password(password_length))
4. Implement a Simple Calculator:
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Cannot divide by zero"
return x / y
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("Sum:", add(num1, num2))
print("Difference:", subtract(num1, num2))
print("Product:", multiply(num1, num2))
print("Quotient:", divide(num1, num2))
5. Check if a Number is a Prime Number:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
number = int(input("Enter a number: "))
if is_prime(number):
print("Prime number")
else:
print("Not a prime number")
6. Sort a List of Dictionaries by a Specific Key:
list_of_dicts = [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25},
{'name': 'Bob', 'age': 35}]
sorted_list = sorted(list_of_dicts, key=lambda x: x['age'])
print("Sorted list of dictionaries:", sorted_list)
7. Generate a Random Matrix:
import numpy as np
rows = 3
cols = 3
random_matrix = np.random.rand(rows, cols)
print("Random matrix:")
print(random_matrix)
8. Implement a Counter Class:
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
def decrement(self):
self.count -= 1
def reset(self):
self.count = 0
counter = Counter()
counter.increment()
counter.increment()
print("Count:", counter.count)
counter.decrement()
print("Count:", counter.count)
counter.reset()
print("Count:", counter.count)
9. Find the Area of a Rectangle:
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = length * width
print("Area of the rectangle:", area)
10. Check if a String is a Valid URL:
import re
def is_valid_url(url):
regex = re.compile(
r'^(?:http|ftp)s?://'
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|
[A-Z0-9-]{2,}\.?)|'
r'localhost|'
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|'
r'\[?[A-F0-9]*:[A-F0-9:]+\]?)'
r'(?::\d+)?'
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
return re.match(regex, url) is not None
input_url = input("Enter a URL: ")
if is_valid_url(input_url):
print("Valid URL")
else:
print("Invalid URL")
0 Comments