Posts

Showing posts with the label For loop

Python Program To Replace The Elements of List With its Cube

Image
  In this post, we are going to write a python program to replace the elements of a list with its cube. General Idea : To replace the elements of the list with its cube, we are going to use the list transversing method. We are going to create two empty lists. In first, we are going to input the elements from the user. In second, we are going to fill the elements by the cube of the respective element which is stored in the first list. Pre-Requisites : Python Basics For loop List Manipulation Program : n=int(input("Enter the number of elements :")) l=[] cube=[] for i in range(0,n):     x=int(input("Enter the element "))     l.append(x) for i in l:     a=i**3     cube.append(a) print(cube) Explanation : 1.In this line, we are inputting the number of elements of the list using the input function and storing that to identifier n. 2.Now we are creating an empty list, for inputting the elements for which we need to replace the cube. 3.Now we are cre...

Python Program To Print Average

Image
  In this post, we are going to write a python program to print get the input of data from the user and print its average. General Idea : The average of a set of data is the sum of the data and divided by the total number of data. Pre-Requisites  : Python Basics For loop Program : n=int(input("Enter the  total number of numbers")) sum1=0 for i in range(0,n):     x=int(input("Enter number "))     sum1=sum1+x print("The Average is ",sum1/n) Explanation : 1.First we are inputting the total number of data and storing that to variable 'n'. (Here in this case total number of numbers)  2. Here we are initializing the variable (identifier) sum1 to 0, which would be used to find the sum of the data. 3. Here we are using for loop to input the data and find its sum. The start value is 0. The end value of the loop is n. That means it stops at n-1. The step value is 1 by default. Therefore the loop repeats n times. 4. This statement is written under the for lo...