Posts

Showing posts from May, 2021

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...

Python Program to Check Leap Year or Not

Image
  In this post, we will write a python program to check an inputted program, a leap year or not. General Idea : A leap is a year that has 366 days. It will come once in a four year. To check whether a year is a leap year or not can be easily verified with the year itself. If the year is divisible by 400 as the year 2000, it's a leap year. Or otherwise, if the year is not divisible by 100 but divisible by 4 as the year 2004, it's a leap year. Except these everything is not a leap year. Pre-Requisites : Python Basics Conditional Statements Program : n=int(input("Enter a Year ")) if ((n%400==0)or((n%100!=0)and(n%4==0))):     print(n," is a leap year") else :     print(n," is not a leap year") Explanation : 1. First we need to get the year for which we need to check whether it's a leap year or not. Here we are inputting the year using the input function and storing it to a variable 'n'. 2. Here we are using the if statement to check whether...

Python Program Print The Sum Of Digits - Codicaly

Image
In this post, we are going to write a python program to print the sum of digits of a number.   General Idea : To find the sum of the digits of a number, first, we are going to put the digits of that number in an empty list, then we are going to use the sum function which basically prints the sum of the elements present in it. Pre - Requisites : Python Basics Iteration ( For loop ) List Program : num=input("Enter an integer") l=len(num) n=int(num) list1=[] for i in range(0,l):     d=n%10     n=int(n/10)     list1.append(d) print("The Sum of Digits of ",num," is ",sum(list1)) Explanation : 1.First, we need to input the integer for which we are finding the sum of digits. Here we are taking the input as a string as we need to find its length and we are assigning it to num. 2. As we took the input as a string we can find its length. This is nothing but the number of digits number. If we enter 2343 as input, its length will be 4, as there are 4 digits. We ar...

Python Program to Print Fibonacci series - Codicaly

Image
  In this post, we are going to write a program to print the Fibonacci series. General idea :               In mathematics, the  Fibonacci number  is a sequence , called the  Fibonacci sequence , such that each number is the sum of the two preceding ones, starting from 0 and 1. Like 0,1,1,2,3,5,8,13,21........... In this sequence, 0 and 1 are default numbers. Then the sum of 0 and 1 is 1.Then the sum of 1 and 1 is 2. Then the sum of 1 and 2 is 3. Then the sum of 2 and 3 is 5. And so on. Pre-Requisites : Python Basics Iteration ( For loop) Program : n=int(input("Enter the number of terms")) total=0 a=0 b=1 print(a) print(b) for i in range(0,n-2):     total=a+b     print(total)     a=b     b=total Explanation : 1.First we need to input the number of terms we need to print. Here we are doing that using print. 2.Here in these lines we are initializing the total to 0, a to 0, and b to 1. 'a' is the...