Found 10784 Articles for Python

How to Find Factorial of Number Using Recursion in Python?

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

677 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 print the Fibonacci Sequence using Python?

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

563 Views

Fibonacci series contains numbers where each number is sum of previous two numbers. This type of series is generated using looping statement.Examplex=0 y=1 fibo=0 while fibo

How to generate armstrong numbers in Python?

Jayashree
Updated on 21-Feb-2020 12:26:50

995 Views

Any three digit number is called an Armstrong number of sum of cube of its digits equals the number itself. In order to check if a number satisfies this condition, each digit from it is successively separated from right and its cube is cumulatively added. In the end if the sum is found to be equal to original number, it is called Armstrong number.ExampleFollowing Python code prints all armstrong numbers between 100 to 999for 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:     ... Read More

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

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

173 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

1K+ 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

111 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 extract numbers from a string using Python?

Jayashree
Updated on 23-Jun-2020 06:13:54

491 Views

To extract each digit from a string −>>> str1='a34e 345 bcd 5he 78 xyz' >>> for s in str1: if s.isdigit():print (s) 3 4 3 4 5 5 7 8To extract only integers from a string in which words are separated by space character −>>> str1='h3110 23 cat 444.4 rabbit 11 2 dog' >>> for s in str1.split(): if s.isdigit(): print ((s)) 23 11 2

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

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

70 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?

Vikram Chiluka
Updated on 14-Feb-2023 10:04:35

8K+ Views

In this article, we will show you how to calculate the absolute value in python. Below are the methods to accomplish this task: Using User-Defined Function (Brute Method) Using abs() function Using math.fabs() function The magnitude of a number, whether positive or negative, is referred to as its absolute value. For example, the absolute value of -2, is 2, and 2 is simply 2. Important Points The absolute value is returned by the built-in abs() function. The math.fabs() function also returns the absolute value, but as a floating-point value. When we pass an integer or float value ... Read More

What is immutable in Python?

Jayashree
Updated on 30-Jul-2019 22:30:21

588 Views

Python defines variety of data types of objects. These objects are stored in memory. Contents of some objects can be changed after they are created while others can't be changed. Numeric objects such as integer, float and complex number objects occupy the memory and memory contents can not be changed. Such objects are called immutable. String and dictionary objects are also immutable. Tuple is also immutable. List object however is mutable because items in a list object can be modified, deleted or added in a list.

Advertisements