Found 10413 Articles for Python

How to Check Leap Year using Python?

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

888 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

785 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

797 Views

Factorial of a number is product of all numbers from 1 to that number.A function is called a recursive function if it calls itself.In following program factorial() function accepts one argument and keeps calling itself by reducing value by one till it reaches 1.Exampledef factorial(x):     if x==1:         return 1     else:         return x*factorial(x-1) f=factorial(5) print ("factorial of 5 is ",f)OutputThe result isfactorial of 5 is  120

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

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

223 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

144 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

121 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

How to calculate absolute value in Python?

Disha Verma
Updated on 20-May-2025 15:54:39

9K+ Views

The absolute value of a number is its non-negative representation. It specifies the distance of that number from zero on a number line, irrespective of its direction.For example, the absolute value of -2 is 2, and 2 is simply 2. In this article, we will explore how to calculate the absolute value of a number in Python. Calculating Absolute Value in Python The following methods can be efficiently used to calculate the absolute value of a number. Using User-Defined Function (Brute Method) Using abs() function ... Read More

Advertisements