Tasks

Tasks

Find Factorial

def find_factorial(number): if number == 0 or number == 1 or number < 0: return 1 factorial_counter = 1 for i in range(number): factorial_counter *= (i + 1) return factorial_counter x = find_factorial(10) print(x)

Take two lists and check weather if the first one is a subset of the second one

def is_subset(list1, list2): set1 = set(list1) set2 = set(list2) return set2.issubset(set1) x = is_subset([1, 2, 3, 3, 3], [1, 9]) print(x)

A function that returns a string

x = "Hey there" def reverse_string(string): return string[::-1] print(reverse_string('Hey'))

A function that checks weather if a sentence contains all the vowels or not using set

def contains_all_vowels(sentence): vowels = {'a', 'e', 'i', 'o', 'u'} sentence_lower = sentence.lower() # Convert the sentence to lowercase for case-insensitive comparison sentence_set = set(sentence_lower) # Convert the sentence into a set of characters return vowels.issubset(sentence_set) # Testing the function with different sentences sentence1 = "Hello, how are you?" print(contains_all_vowels(sentence1)) # Output: True sentence2 = "Python programming is fun!" print(contains_all_vowels(sentence2)) # Output: False sentence3 = "Quick brown fox jumps over the lazy dog" print(contains_all_vowels(sentence3)) # Output: True sentence4 = "AEIOU are vowels" print(contains_all_vowels(sentence4)) # Output: True sentence5 = "This sentence misses some vowels" print(contains_all_vowels(sentence5)) # Output: False

Write a function to find the sum of all positive numbers in a given list of integers.

def sum_positive_numbers(input_list): sum_pos = 0 for num in input_list: if num > 0: sum_pos += num return sum_pos numbers = [-1, 3, -5, 7, 0, 10, -2] result = sum_positive_numbers(numbers) print("Sum of positive numbers:", result)

Calculate the sum of two numbers input by the user

def sum_two_numbers(): num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) result = num1 + num2 print(f"The sum of {num1} and {num2} is {result}") sum_two_numbers()

Check if a string contains the substring "Chai"

def check_python_substring(input_string): if "Chai" in input_string: print("Found Chai!") else: print("Chai not found!") input_string = input("Enter a string: ") check_python_substring(input_string)

Return a new list containing only the even numbers from a given list of integers

x = input('Enter a list of numbers seperated by a space: ') x = x.split() total = 0 for i in x: total += int(i) print(f"The total of all the numbers is {total}")

Count the number of vowels in a given string:

def count_vowels(input_string): vowels = "aeiouAEIOU" count = 0 for char in input_string: if char in vowels: count += 1 return count input_string = input("Enter a string: ") num_vowels = count_vowels(input_string) print(f"Number of vowels in '{input_string}' is: {num_vowels}")

Create a calculator function using match case

def calculator(operand1, operand2, operator): total = 0 match operator: case '+': total = operand1 + operand2 case '-': total = operand1 - operand2 case '*': total = operand1 * operand2 case '/': total = operand1 / operand2 case _: print("Error buddy!") return None return total x = calculator(23, 2, '8') print(x)

Find min and max from a list

working = [2, 34, 5, 6,32, 2, 44, 68, 2] def find_minmax(input_list): track = { 'min': input_list[0], 'max': input_list[0] } for i in input_list: if track['min'] > i: track['min'] = i if track['max'] < i: track['max'] = i return track x = find_minmax(working) print(x)

Python Translate Program

from googletrans import Translator translator = Translator() translation = (translator.translate("I will kill you someday", dest="hi")) translation = translation.text file_name = "translation.txt" with open(file_name, 'w') as file: file.write(translation)

Practice Set

  1. Write a function that takes an input of 4 number from the user and then finds the largest one and returns it.
    1. def find_max4(): max = False for i in range(4): user_input = int(input(f"Enter {i+1} Number: ")) if max == False or max < user_input: max = user_input print(f"The highest number is {max}") find_max4()
  1. Create a program that finds weather a number is odd, even or zero. If the number is not valid number we should return False
  1. A network fetch request
    1. import requests # API endpoint URL for posts url = 'https://pastebin.com/raw/1MsLWem7' # Send GET request response = requests.get(url) fruits = None # Check if the request was successful (status code 200) if response.status_code == 200: # Parse JSON data data = response.text fruits = data.split(", ") # Display each post's title print(data) else: print('Failed to retrieve data:', response.status_code) print(fruits[0])
  1. Now using the same mechanism create a list of spam keywords and put them in a pastebin. now create a function that checks weather if a sentence is spam or not.
    1. import requests def get_spam_keywords(): URL = "https://pastebin.com/raw/46sDytwZ" response = requests.get(URL) spam_keywords = None if response.status_code == 200: spam_keywords = response.text.split(", ") else: print("Response failed.", response.status_code) return spam_keywords def detect_spam(sentence): spam_keywords = get_spam_keywords() for i in spam_keywords: if i in sentence: return True return False x = detect_spam("Hey there buddy I hope you are doing good. Win a Prize.") if x: print('The sentence is probably a spam 😠') else: print("The sentence is not a spam 😇")
  1. Create a program that fetches all the emails from a webpage
    1. import requests response = requests.get("https://shantanuuchak.tech") strings = None if response.status_code == 200: data = response.text strings = data.split('"') else: print("Error while fetching", response.status_code) def filter_email(list): emails_list = [] for i in list: if 'mailto' in i: emails_list.append(i) return emails_list x = filter_email(strings) print(x)

Practice Set

  1. Write a program to find weather if a number is prime or not
    1. def find_prime(number): for i in range(2, number): if number % i == 0: print("Number is not a prime") break else: print("Congrats, the number is a prime") find_prime(24)
  1. Write a program to find the sum of all the natural numbers upto a given number taken from the user. Using a while loop
    1. n = int(input("Enter a number: ")) i = 1 sum = 0 while i <= n: sum += i i+=1 print(sum)
  1. Factorial nikalne wala code
    1. def factorial(number): total = 1 for i in range(1, number+1): total *= i return total print(factorial(8))
  1. Find the average of n numbers given in a function
    1. def average(*numbers): sum = 0 for i in numbers: sum += i return sum/len(numbers) x = average(2, 3, 4) print(x)
  • Remaining: Star Patterns