Posts

Python Sort List

Image
Python Program to Sort the List According to the Length of the Words Hey There Python Geeks , In this python tutorial , I have shown how to sort the list according to the length of the words. Consider a python list : list =['bye bye bye','hi','good morning','YouTube'] Take an empty list : empty_list = [ ] Take another empty list : final_list = [ ] Now, we need to find the length of the words which are there in the list and that can be done as : for i in list: For every word in the list, we need to add the length of the words to the empty_list : empty_list.append(len(i)) We have every word length in empty_list ,we need sort the empty_list ,so that we can have the words in order of low to high : empty_list.sort() Take for loop for every element in empty_list and another for loop for every element in list : for i in empty_list:                           ...

Python Program to Replace Selected Character with * in a String

Image
  Python Program to Replace Selected Character with * in a String Hey There Python Geeks, In this python tutorial , I have shown how to replace selected character with * in a string. Consider a string : str = "hello youtube' For character we ask the user to select a character : char = input("Enter a character that can be replaced") Create a function called replace_char and pass the string(str) to it, you can name a function whatever you want  and that can be done as : def replace_char(str): To check for every element we use for loop and can be written as : for i in str: If the character and the element which is in the string matches exactly the same, we need to replace it with *, that can be done as : if char==i: str.replace(char , " ") And at last we need to return the string(str) : return str.replace(char , " ") We successfully replaced the character with * which is present in the string, now what if a character is not present in the string?. Fo...

Python Program to Swap First and Last Element of a Given List

Image
Python Program to Swap First and Last Element of a Given List Hey Python Geeks , In this python tutorial ,  I have shown how to swap first and last element of a list. Consider a python list : list = [1,2,3,4,5,6,7,8,9] Create a python function called Swap that takes above python list as input, you can name it whatever you want and that can be written as : def Swap(list): Now, we have to swap first element to last element and that can be done as : list[0],list[-1]=list[-1], list[0] list[0] is the first element of the list list[-1] is the last element of the list We swapped them successfully, now call the function to see the swapped list and that can be done as : Swap(list) The code can be written as :             list = [1,2,3,4,5,6,7,8,9]             print("List before swapping",list)             def swap(list): ...

Python Program to Put Even and Odd Elements of a List into Two Different Lists

Image
  Hey There, In this blog, I have shown how to put even an odd elements of a list into two different lists. Consider the given list as : list = [1,2,3,4,5,6,7,8,9] Take list for even elements : even_list = [ ] Take list for odd elements = odd_list = [ ] To check each element in the given list we will use for loop, that can be written as : for i in list: To know a element is even, we divide it by 2. If element remainder is zero then it is a even number. That can be written as : if i%2 == 0: If that element is divisible by 2, then we add that element to the even_list and can be written as : even_list.append( i ) If it is not divisible by 2, then we append it to odd_list : odd_list.append( i ) So, the program can be written as :       list = [1,2,3,4,5,6,7,8,9]       even_list = [ ]       odd_list = [ ]       for i in list:            if...

Python Program to Find the Largest Number in a Given List

Image
  Hey There, In this video, I have shown how to find the largest number in a given list. Consider the given list as : list = [2,4,6,8,1,9,3,0] Now,sort the list to get the largest number at the last. The sorting can be done as : list.sort() After sorting the list look like  : list = [0,1,2,3,4,6,8,9] We got the largest number in the list at the end. To print the last element in the list, we can write the code as : print(list[-1]) So,the code can be written as :       list =  [2,4,6,8,1,9,3,0]       list.sort()       b = list[-1]       print("Largest number in the given list is : ",b) Output : Python Program to Find the Largest Number in a Given List Video Tutorial:

Python Program to Print all the Numbers in a Range Divisible by Given Number

Image
  Hey There, In this blog, I have shown how to print all the numbers in a range that are divisible by given number. Taking n value from the user :  n = int(input("Enter a number")) To run in a range we need to take a for loop that can be done as : for i in range(1,26) Here I want to print all the numbers in a range that starts form 1 and ends at 25,26 is exclusive. To check number is divisible by given number(n), we do that by checking it's remainder.If remainder is 0,then that number is divisible by n.That can be written as: if  i%n==0: If the above if condition satisfies then we can print i value as : print(i) Now,solution is as follows :        n = int(input("Enter a number"))               for i in range(1,26):                            if i % n ...

Python Program to Calculate the Average of Numbers in a Given List

Image
  Hey There, In this blog, I have shown how to calculate average of numbers in a given list.Average can be calculated as :                           Average = (Sum of all the numbers)/(Total numbers) Here the given list is :  list = [1,2,3,4,5,6,7,8,9] Sum can be calculated as :  sum_all = sum(list) Total numbers can be calculated as :  length = len(list) Now, Average will be :   avg = (sum_all/length) Printing the Average as :    print("Average is : ",avg) The above code can be written in a single line as :                          print("Average of numbers is : ", sum(list)/len(list)) Output :  Python Program to Calculate the Average of Numbers in a Given List Video Tutorial: