Found 10476 Articles for Python

How to split Python tuples into sub-tuples?

Vikram Chiluka
Updated on 09-Nov-2022 07:48:02

11K+ Views

In this article, we will show you how to split python tuples into sub-tuples. Below are the various methods to accomplish this task − Using slicing Using enumerate() & mod operator Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable. Using slicing Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Create a variable to ... Read More

How to Check Leap Year using Python?

Akshitha Mote
Updated on 12-Dec-2024 19:45:16

945 Views

A year that occurs every four years is considered a leap year in the calendar. For example, 2024 is divisible by 4 therefore, it is a leap year. Conditions for a Leap Year To determine whether a given year is a leap year, the following conditions must be met − The year must be divisible by 4. If the year is divisible by 100, it must also be divisible by 400 to qualify as a leap year. If the year is divisible by 100 but not ... Read More

How to zip a Python Dictionary and List together?

Vikram Chiluka
Updated on 09-Nov-2022 07:04:11

12K+ Views

In this article, we will show you how to zip a python dictionary and list together. Below are the various methods to accomplish this task − Using zip() function Using append() function and items() function Using append() function and in operator. Combing dictionary and list together using zip() Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Create a variable to store the input dictionary. Create another variable to store the input list. Use the zip() function(The zip() function can be used to combine two lists/iterators) to combine the input ... Read More

How to create Python dictionary with duplicate keys?

SaiKrishna Tavva
Updated on 17-Oct-2024 12:16:03

6K+ Views

In Python, a dictionary doesn't allow duplicate keys or repeated keys. So, we can use defaultdict from the Collections module. As it can store multiple values for the same key in the form of lists or any other data structures. 'defaultdict' from 'collections' Module The subclass of the built-in dict class that allows us to provide a default value for a key that doesn't exist is known as defaultdict. A function that returns the default value for new keys is known as 'default factory', and if you want to pass this default factory, we can use a list which allows storing ... Read More

How to Find Armstrong Number in an Interval using Python?

Jayashree
Updated on 21-Feb-2020 12:58:37

811 Views

If sum of cubes of individual digits in a number add up to the number itself, it is called armstrong number. for example 153=1**3+5**3+3**3ExampleFollowing Python program find armstrong numbers between 100 to 1000for num in range(100,1000):   temp=num   sum=0   while temp>0:       digit=temp%10       sum=sum+digit**3       temp=temp//10       if sum==num:            print (num)OutputThe output is as follows −153 370 371 407

How to Find Factorial of Number Using Recursion in Python?

Jayashree
Updated on 21-Feb-2020 12:54:14

895 Views

We can write a recursive function in Python to find the factorial of a number. Recursion means that a function calls itself repeatedly to work through different stages of the same task. This technique is useful for tasks that follow a repetitive pattern or have a step-by-step structure, like calculating factorials, generating the Fibonacci series, or navigating tree structures (tree traversal). The factorial of a number is the product of all positive integers from 1 to that number. It is represented using the symbol n! and defined as - n! = n X (n - 1) X (n - 2) ... Read More

How to find the greatest number in a list of numbers in Python?

Jayashree
Updated on 21-Feb-2020 12:27:17

242 Views

Python's built-in library function max() returns the largest number in an iterable or commaa separated list of numbers.>>> max(10,23,43,21) 43 >>> l1=[4,7,2,9,1] >>> max(l1) 9

How to do Python math at command line?

Vikram Chiluka
Updated on 28-Oct-2022 11:29:37

2K+ Views

In this article, we will show you how to do python math at the command line. Python is an interpreter-based language. When you invoke the Python interpreter, (>>>) Python prompt appears. Any Python statement can be entered in front of it. As soon as you press ENTER, the statement is executed. Hence a mathematical expression using operators defined in Python will be evaluated at the command line. What are Operators? A symbol or function that represents an operation is called an operator. For instance, in mathematics, the addition operator is represented by the plus sign (+). While some of ... Read More

How to find sum a list of numbers in Python?

Jayashree
Updated on 21-Feb-2020 12:30:30

160 Views

Python's built-in function sum() returns sum of numbers in an iterable object such as list or tuple. It takes two arguments, initial value which is optional and 0 by default and iterable objectExample>>> l1=[10,20,30,40,50] >>> ttl=sum(l1) >>> ttl 150 >>> ttl=sum(range(10)) >>> ttl 45

How to know Maximum and Minimum values for ints in Python?

Jayashree
Updated on 21-Feb-2020 12:51:33

139 Views

Python's core library has two built-in functions max() and min() respectively to find maximum and minimum number from a sequence of numbers in the form of list or tuple object.example>>> max(23,21,45,43) 45 >>> l1=[20,50,40,30] >>> max(l1) 50 >>> t1=(30,50,20,40) >>> max(t1) 50 >>> min(l1) 20 >>> min(t1) 20 >>> min(23,21,45,43) 21

Advertisements