Category: Python programming question

How to split a string into an list of characters in Python?
We’ll discuss following ways to convert string into an array of characters: Using list constructor Using list comprehension Using map and lambda Using for loop Unpacking into an empty list Using extend() method Method 1: Using list constructor The list constructor takes a single argument which is iterable. You can pass a sequence, collection or…

Python program to find sum of elements in a list
We’ll see different ways to find sum of elements in a list. The requirement is that if a list contains numbers, then find the sum of all the elements in the list. We’ll discuss four ways to find the sum of all the elements in the list: Using sum() method Using for loop Using while…

How to clear a list in Python
We’ll discuss different ways to clear a list in Python. The 4 ways which we’ll discuss to clear the list are: Using clear() method Using *= 0 Using del() method Reinitializing the list Method 1: Using clear() method We can use clear() method to clear the list. Output Method 2: Using *=0 This is not…

Python program to convert time from 12 hour to 24 hour format
Approach 1: Using slicing Whether the input time is in 12 hour format or not can be checked by checking the last two elements. If the last two elements are AM or PM then the input time is in 12 hour format.If PM, add 12 to the time. If AM, then don’t add. Output Approach…

How to reverse a list in Python
For the provided list, for example [1,2,3,4,5,6,7,8,9], task is to reverse the list. The output after reversing the list should be[9,8,7,6,5,4,3,2,1] We’ll discuss three ways to reverse the list: Inplace reversal using reverse() method. Slicing the list using the [::1] Creating a reversed iterator using reversed() builtin function. 1.Reverse a list using reverse() method Python…

Python program to find largest number in a list
We’ll discuss 3 methods to find largest number in a list. Method 1 : Sort the list in ascending order and print the last element in the list. Output Method 2 : Using max() method Output Method 3: Without using built in functions Output

Python Program to find sum of numbers in an array
Approach 1 In the first approach we’ll loop through the elements of the array and add every element to the sum which is initialized to 0. Output Approach 2 In the second approach, we’ll use the inbuilt method sum() to calculate the sum of elements of an array. Please note that you can pass any…

Python Program to check Armstrong Number
A positive integer of n digits is an Armstrong number of order n (order is number of digits) if For example, 153 is an Armstrong number. 153 = 1^3 + 5^3 + 3^3 Output

Python program to calculate simple interest
Simple interest formula is given by: Here, P stands for Principal, T stands for Time and R stands for Rate of Interest In the following example, we’ll use principal and rate of interest as float values and time as int value just for the sake of simplicity. Output

Python Program for factorial of a number
In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n: For example, Note: factorial is defined only for nonnegative integer numbers. Finding factorial of a number in Python using Iteration 1. Factorial of a number using for loop Output 2.…