Jayashree

Jayashree

19 Articles Published

Articles by Jayashree

19 articles

How to Find Factors of Number using Python?

Jayashree
Jayashree
Updated on 09-Sep-2023 12K+ Views

In order to find factors of a number, we have to run a loop over all numbers from 1 to itself and see if it is divisible.Examplenum=int(input("enter a number")) factors=[] for i in range(1,num+1):     if num%i==0:        factors.append(i) print ("Factors of {} = {}".format(num,factors))If i is able to divide num completely, it is added in the list. Finally the list is displayed as the factors of given numberOutputenter a number75 Factors of 75 = [3, 5, 15, 25, 75]

Read More

How to create Python dictionary by enumerate function?

Jayashree
Jayashree
Updated on 09-Sep-2023 5K+ Views

Python enumerate() function takes any iterable as argument and returns enumerate object using which the iterable can be traversed. It contains index and corresponding item in the iterable object like list, tuple, or string.Such enumerate object with index and value is then converted to a dictionary using dictionary comprehension.>>> l1=['aa','bb','cc','dd'] >>> enum=enumerate(l1) >>> enum >>> d=dict((i,j) for i,j in enum) >>> d {0: 'aa', 1: 'bb', 2: 'cc', 3: 'dd'} Learn Python with our step-by-step Python Tutorial.

Read More

How do I find the largest integer less than x in Python?

Jayashree
Jayashree
Updated on 27-Oct-2022 3K+ Views

In this article, we will show you how to find the largest integer less than x in python. The Greatest Integer Function [X] denotes an integral part of the real number x that is the closest and smallest integer to x. It's also called the X-floor. [x]=the largest integer less than or equal to x. Generally If n

Read More

Python: Cannot understand why the error - cannot concatenate 'str' and 'int' object ?

Jayashree
Jayashree
Updated on 13-Mar-2020 262 Views

This can be corrected by putting n+1 in brackets i.e. (n+1)for num in range(5):     print ("%d" % (num+1))Using %d casts the object following % to string. Since a string object can't be concatnated with a number (1 in this case) interpreter displays typeerror.

Read More

How to find the value closest to negative infinity in Python?

Jayashree
Jayashree
Updated on 02-Mar-2020 229 Views

Although infinity doesn't have a concrete representation, the closest number to negative infinity is represented as return value of float() function with '-inf' as argument>>> a=float('-inf') >>> -inf

Read More

How to find the value closest to positive infinity in Python?

Jayashree
Jayashree
Updated on 02-Mar-2020 258 Views

Although infinity doesn't have a concrete representation, the closest number to infinity is represented as return value of float() function with 'inf' as argument>>> a=float('inf') >>> a inf

Read More

How to Find the Power of a Number Using Recursion in Python?

Jayashree
Jayashree
Updated on 02-Mar-2020 1K+ Views

Following program accepts a number and index from user. The recursive funcion rpower() uses these two as arguments. The function multiplies the number repeatedly and recursively to return power.Exampledef rpower(num,idx): if(idx==1): return(num) else: return(num*rpower(num,idx-1)) base=int(input("Enter number: ")) exp=int(input("Enter index: ")) rpow=rpower(base,exp) print("{} raised to {}: {}".format(base,exp,rpow))OutputHere is a sample run −Enter number: 10 Enter index: 3 10 raised to 3: 1000

Read More

How to Multiply Two Matrices using Python?

Jayashree
Jayashree
Updated on 02-Mar-2020 4K+ Views

Multiplication of two matrices is possible only when number of columns in first matrix equals number of rows in second matrix.Multiplication can be done using nested loops. Following program has two matrices x and y each with 3 rows and 3 columns. The resultant z matrix will also have 3X3 structure. Element of each row of first matrix is multiplied by corresponding element in column of second matrix.ExampleX = [[1, 2, 3],          [4, 5, 6],          [7, 8, 9]]     Y = [[10, 11, 12],         [13, 14, 15], ...

Read More

How to Find Sum of Natural Numbers Using Recursion in Python?

Jayashree
Jayashree
Updated on 02-Mar-2020 3K+ Views

If a function calls itself, it is called a recursive function. In order to prevent it from falling in infinite loop, recursive call is place in a conditional statement.Following program accepts a number as input from user and sends it as argument to rsum() function. It recursively calls itself by decrementing the argument each time till it reaches 1.def rsum(n): if n

Read More

How does modulus work with complex numbers in Python?

Jayashree
Jayashree
Updated on 02-Mar-2020 355 Views

Floor and modulus operators (// and % respectively) are not allowed to be used on complex number in Python 3.x. However, these operations are defined for complex numbers in Python 2.7.xPython 3>>> x=9+2j >>> y=2+1j >>> x%y Traceback (most recent call last): File "", line 1, in x%y TypeError: can't mod complex numbers.Python 2.7>>> x=9+2j >>> y=2+1j >>> x%y (1-2j)Modulus of complex number operands returns their floor division multiplied by denominator>>> x-(x//y)*y (1-2j)

Read More
Showing 1–10 of 19 articles
« Prev 1 2 Next »
Advertisements