Skip to main content

Posts

Showing posts from May, 2022

Python program to replace words in a sentence

Write a program that replaces words in a sentence. The input begins with word replacement pairs (original and replacement). The next line of input is the sentence where any word on the original list is replaced. Ex: If the input is: automobile car manufacturer maker children kids The automobile manufacturer recommends car seats for children if the automobile doesn't already have one. the output is: The car maker recommends car seats for kids if the car doesn't already have one. You can assume the original words are unique. w = input().split() x = input() for i in range(0, len(w), 2): if w[i] in x: x = x.replace(w[i], w[i+1]) print(x) Hope this is helpful. Write back to me in case you need help with your python task. I generally reply back within 24 hours. You can connect with me on Whatsapp also: +918602715108

Program to count frequency of words in python

Frequency of words counting Write a program that reads a list of words. Then, the program outputs those words and their frequencies. Ex: If the input is: hey hi Mark hi mark the output is: hey 1 hi 2 Mark 1 hi 2 mark 1 data = input().split() for word in data: count = 0 for item in data: if word == item: count += 1 print(word, count) Hope this is helpful. Write back to me in case you need help with your python task. I generally reply back within 24 hours. You can connect with me on Whatsapp also: +918602715108

Python program to print key-value pair of dictionary with country name and population

  Write a loop that prints each country's population in country_pop. Sample output with input: 'China:1365830000,India:1247220000,United States:318463000,Indonesia:252164800': United States has 318463000 people. India has 1247220000 people. Indonesia has 252164800 people. China has 1365830000 people. # required dictionary country_pop = { # countries as key and numbers as value 'China': 1365830000, 'India': 1247220000, 'United States': 318463000, 'Indonesia': 252164800 } # requied for loop to pop keys for key in country_pop.keys(): # assign key to the country country = key # store the popped countries un a variable pop = country_pop[key] # print the required output as per the format print(country, 'has', pop, 'people.') Hope this is helpful. Write back to me in case you need help with your python task. I generally reply back within 24 hours. You can connect with me on Whatsapp als...

Multidimensional list in python print as matrix

 Two Dimensional List in Python  Print the two-dimensional list mult_table by row and column using Nested loops Sample Output with input: 1 2 3, 2 4 6, 3 6 9 1 | 2 | 3 2 | 4 | 6 3 | 6 | 9  user_input = input() lines = user_input.split(',') mult_table = [[int(num) for num in line.split()] for line in lines] for lst in mult_table: for i in range(len(lst)): print(lst[i], end='') if i != len(lst)-1: print(' | ', end='') print() Hope this is helpful. Write back to me in case you need help with your python task. I generally reply back within 24 hours. You can connect with me on Whatsapp also: +918602715108

Printing the hourly temperature in python

  Write a loop to print all elements in hourly_temperature. Seperate elements with a -> surrounded by spaces Sample output for the given program with input: 90 92 94 95 90 -> 92 -> 94 -> 95 user_input= input() hourly_temprature = user_input.split() # iterate the hourly_temprature using for loop for i in range(0, len(hourly_temprature)-1): print(hourly_temprature[i],"->", end=' ') # last element of hourly_temprature, follwed by space and then new line print(hourly_temprature[-1]," ") print() Hope this is helpful. Write back to me in case you need help with your python task. I generally reply back within 24 hours. You can connect with me on Whatsapp also: +918602715108

Calculating extra credit scores in python

Extra Credit Calculation in Python Assign sum_extra with the total extra credit received given list test_grades. Full credit is 100, so anything over 100 is extra credit. Sample output for the given program with input: 101 83 107 90 Sum Extra = 1 + 7 = 8 user_input= input() test_grades= list(map(int, user_input.split())) sum_extra = 0 for i in test_grades: if i>100 : sum_extra = sum_extra + i-100 print('Sum extra:',sum_extra) Hope this is helpful. Write back to me in case you need help with your python task. I generally reply back within 24 hours. You can connect with me on Whatsapp also: +918602715108

Create a list from user's input in python

  Write a loop to populate the list user_gusses with a number of guesses. The variable num_guesses is the number of guesses the user will have, which is read first as an integer. Read each guess (an integer) one at a time using int(input()) Sample output with input 3 9 5 2  user_gusses: [9, 5, 2] num_guesses= int(input()) user_guesses= [] #ask user to enter the user_guesses element for i in range(0, num_guesses): x= int(input()) user_guesses.append(x) print('user_guesses:', user_guesses) Hope this is helpful. Write back to me in case you need help with your python task. I generally reply back within 24 hours. You can connect with me on Whatsapp also: +918602715108

Popping element from list in python

  Modify short_names by deleting the first element and changing the last element to Joe Sample output with input : "Jig Jag Jam Jim" ['Jag', 'Jam', 'Joe'] user_input = input() short_names = user_input.split() short_names.pop(0) short_names[-1] = 'Joe' print(short_names) Hope this is helpful. Write back to me in case you need help with your python task. I generally reply back within 24 hours. You can connect with me on Whatsapp also: +918602715108

Python program from reversing a number, calculating number of digits

Write a program to reverse a number entered by the user. Call your program int_reverser.py . Suggested approach:  a. Write an expression for the number of digits. This is the log in base 10 , math.log100, but it should be a whole number.  b. Write a loop to keep dividing the number by 10 and printing the remainder.  c. Add an accumulator variable for the result. Keep multiplying the result by 10 and adding the remainder. Example usage:  Integer reverer  Integer: 1234  4321  import math n = int(input("Enter number: ")) numberOfDigit = math.log10(n) if n Enter number: 1234 4321 Hope this is helpful. Write back to me in case you need help with your python task. I generally reply back within 24 hours. You can connect with me on Whatsapp also: +918602715108

Prime Number program in Python

A prime number is a number that has only two factors: 1 and the number itself. For example, 2, 3, 5, 7 and 11 are the first few prime numbers. Write a program that repeatedly prompts a user for numbers until the user enters a positive larger number. Once a valid number is entered, print out the list of prime number from 0 to the entered number. If the user enters anything other than a valid number MO it with a try/except and put out an appropriate message and ignore the number. For example, enter -1, bob and 10 and match the output below.  Enter a positive integer number: -1  The number must be positive.  Enter a positive integer number: bob  The number must be a positive number.  Enter a positive integer number: 10  The prime numbers from 0 to 10: 2 3 5 7  while True: try: n = int(input("Enter a positive integer number: ")) if n <= 0: print("The number must be a positive.") else: print("The...