What is Computer

Module: M2-R5: Web Design & Publishing

Chapter: Ch1 Computer Intro

🔹 User-Defined Function Examples (Practical Scenarios)
🔹 Addition Calculator
def add(a, b):
    return a + b

print(add(10, 5))
print(add(23, 77))
print(add(0, 0))
🔹 Subtraction Calculator
def subtract(a, b):
    return a - b

print(subtract(10, 5))
print(subtract(50, 20))
print(subtract(100, 75))
🔹 Multiplication Calculator
def multiply(a, b):
    return a * b

print(multiply(4, 5))
print(multiply(7, 8))
print(multiply(0, 100))
🔹 Division Calculator
def divide(a, b):
    if b != 0:
        return a / b
    else:
        return "Cannot divide by zero"

print(divide(10, 2))
print(divide(25, 5))
print(divide(7, 0))
🔹 Age Calculator
def calculate_age(current_year, birth_year):
    return current_year - birth_year

print(calculate_age(2025, 2000))
print(calculate_age(2025, 1995))
print(calculate_age(2025, 2010))
🔹 Square Calculator
def square(num):
    return num ** 2

print(square(5))
print(square(12))
print(square(0))
🔹 Cube Calculator
def cube(num):
    return num ** 3

print(cube(2))
print(cube(5))
print(cube(10))
🔹 Area of Rectangle
def rectangle_area(length, width):
    return length * width

print(rectangle_area(5, 10))
print(rectangle_area(7, 3))
print(rectangle_area(0, 10))
🔹 Area of Circle
def circle_area(radius):
    pi = 3.1416
    return pi * radius ** 2

print(circle_area(5))
print(circle_area(7))
print(circle_area(0))
🔹 Even or Odd Checker
def even_odd(num):
    return "Even" if num % 2 == 0 else "Odd"

print(even_odd(5))
print(even_odd(12))
print(even_odd(0))
🔹 Dice Game Roll
import random
def roll_dice():
    return random.randint(1,6)

print(roll_dice())
print(roll_dice())
print(roll_dice())
🔹 Greeting Function
def greet_user(name):
    return f"Hello, {name}!"

print(greet_user("Sourav"))
print(greet_user("Alice"))
print(greet_user("Bob"))
🔹 Reverse a String
def reverse_string(s):
    return s[::-1]

print(reverse_string("Python"))
print(reverse_string("Sourav"))
print(reverse_string("Hello"))
🔹 Factorial Calculator
def factorial(n):
    if n==0 or n==1:
        return 1
    return n * factorial(n-1)

print(factorial(5))
print(factorial(3))
print(factorial(0))
🔹 Prime Checker
def is_prime(n):
    if n < 2:
        return False
    for i in range(2,n):
        if n%i==0:
            return False
    return True

print(is_prime(7))
print(is_prime(10))
print(is_prime(2))
🔹 Fibonacci Series Up to n
def fibonacci(n):
    series = [0,1]
    for i in range(2,n):
        series.append(series[i-1]+series[i-2])
    return series[:n]

print(fibonacci(5))
print(fibonacci(8))
print(fibonacci(3))
🔹 Celsius to Fahrenheit
def c_to_f(c):
    return (c * 9/5) + 32

print(c_to_f(0))
print(c_to_f(100))
print(c_to_f(37))
🔹 Kilometers to Miles
def km_to_miles(km):
    return km * 0.621371

print(km_to_miles(10))
print(km_to_miles(42))
print(km_to_miles(0))
🔹 Maximum of Two Numbers
def maximum(a, b):
    return a if a>b else b

print(maximum(10,20))
print(maximum(5,3))
print(maximum(7,7))
🔹 Minimum of Two Numbers
def minimum(a, b):
    return a if a< b else b

print(minimum(10,20))
print(minimum(5,3))
print(minimum(7,7))
✅ Summary
  • User-defined functions can perform calculations, string manipulations, or complex operations.
  • They help make code modular, reusable, and organized.
  • Functions can include parameters, return values, recursion, and use Python built-ins.
  • Practical examples include calculators, games, converters, and series generators.
🔹 User-Defined Function Examples (Advanced Utilities & Games)

Here are 20 advanced examples of user-defined functions for practical applications like games, scoreboards, and calculators.

🔹 Guess the Number Game
import random
def guess_number():
    num = random.randint(1,10)
    guess = int(input("Guess a number (1-10): "))
    return "Correct!" if guess == num else f"Wrong! The number was {num}"

print(guess_number())
print(guess_number())
print(guess_number())
🔹 Simple Interest Calculator
def simple_interest(principal, rate, time):
    return (principal * rate * time) / 100

print(simple_interest(1000, 5, 2))
print(simple_interest(5000, 7, 3))
print(simple_interest(2000, 10, 1))
🔹 Compound Interest Calculator
def compound_interest(principal, rate, time):
    return principal * ((1 + rate/100) ** time)

print(compound_interest(1000, 5, 2))
print(compound_interest(2000, 7, 3))
print(compound_interest(1500, 10, 1))
🔹 BMI Calculator
def bmi(weight, height):
    return weight / (height ** 2)

print(bmi(70, 1.75))
print(bmi(60, 1.6))
print(bmi(90, 1.8))
🔹 Rock Paper Scissors
import random
def rps(player):
    options = ["rock","paper","scissors"]
    comp = random.choice(options)
    if player == comp: return "Tie!"
    elif (player=="rock" and comp=="scissors") or (player=="paper" and comp=="rock") or (player=="scissors" and comp=="paper"):
        return "Player Wins!"
    else: return "Computer Wins!"

print(rps("rock"))
print(rps("paper"))
print(rps("scissors"))
🔹 Leap Year Checker
def is_leap(year):
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

print(is_leap(2020))
print(is_leap(1900))
print(is_leap(2000))
🔹 Count Vowels in String
def count_vowels(s):
    return sum(1 for char in s.lower() if char in "aeiou")

print(count_vowels("Sourav"))
print(count_vowels("Python"))
print(count_vowels("Hello World"))
🔹 Palindrome Checker
def is_palindrome(s):
    return s == s[::-1]

print(is_palindrome("racecar"))
print(is_palindrome("python"))
print(is_palindrome("level"))
🔹 Sum of Digits
def sum_digits(n):
    return sum(int(digit) for digit in str(n))

print(sum_digits(123))
print(sum_digits(456))
print(sum_digits(0))
🔹 Dice Game Sum
import random
def dice_sum():
    return random.randint(1,6) + random.randint(1,6)

print(dice_sum())
print(dice_sum())
print(dice_sum())
🔹 USD to INR Converter
def usd_to_inr(usd):
    rate = 83.0
    return usd * rate

print(usd_to_inr(10))
print(usd_to_inr(50))
print(usd_to_inr(100))
🔹 Largest in List
def largest(lst):
    return max(lst)

print(largest([1,5,3]))
print(largest([10,20,15]))
print(largest([-1,-5,-3]))
🔹 Smallest in List
def smallest(lst):
    return min(lst)

print(smallest([1,5,3]))
print(smallest([10,20,15]))
print(smallest([-1,-5,-3]))
🔹 Password Strength Checker
def check_password(pwd):
    if len(pwd)<6: return "Weak"
    if any(char.isdigit() for char in pwd) and any(char.isupper() for char in pwd): return "Strong"
    return "Medium"

print(check_password("abc"))
print(check_password("abc123"))
print(check_password("Abc123"))
🔹 Word Count in String
def word_count(s):
    return len(s.split())

print(word_count("Hello World"))
print(word_count("Python programming is fun"))
print(word_count(""))
🔹 Random Password Generator
import random, string
def random_password(length):
    chars = string.ascii_letters + string.digits
    return ''.join(random.choice(chars) for _ in range(length))

print(random_password(6))
print(random_password(8))
print(random_password(12))
🔹 RPS Score Tracker
def rps_score(player, computer):
    if player==computer: return "Tie"
    elif (player=="rock" and computer=="scissors") or (player=="paper" and computer=="rock") or (player=="scissors" and computer=="paper"): return "Player Wins"
    else: return "Computer Wins"

print(rps_score("rock","scissors"))
print(rps_score("paper","rock"))
print(rps_score("scissors","scissors"))
🔹 Shopping Discount Calculator
def discount(price, percent):
    return price - (price * percent/100)

print(discount(1000,10))
print(discount(500,20))
print(discount(200,5))
🔹 Fahrenheit to Celsius
def f_to_c(f):
    return (f - 32) * 5/9

print(f_to_c(32))
print(f_to_c(100))
print(f_to_c(212))
🔹 Tip Calculator
def calculate_tip(amount, tip_percent):
    return amount * tip_percent / 100

print(calculate_tip(500,10))
print(calculate_tip(1200,15))
print(calculate_tip(250,20))
✅ Summary
  • Advanced user-defined functions can create games, calculators, and mini utilities.
  • They demonstrate practical applications like finance, conversions, string and list operations.
  • Functions make code reusable, organized, and modular for real-world use cases.
Quick Links