Jayashree has Published 41 Articles

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

Jayashree

Jayashree

Updated on 13-Mar-2020 05:15:39

102 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.

What are Ordered dictionaries in Python?

Jayashree

Jayashree

Updated on 02-Mar-2020 10:21:48

365 Views

An OrderedDict is a dictionary subclass that remembers the order in which its contents are added, supporting the usual dict methods.If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.>>> from collections import ... Read More

What is the difference between operator and method on Python set?

Jayashree

Jayashree

Updated on 02-Mar-2020 10:21:19

315 Views

Python's set object represents built-in set class. Different set operations such as union, intersection, difference and symmetric difference can be performed either by calling corresponding methods or by using operators.Union by method>>> s1={1, 2, 3, 4, 5} >>> s2={4, 5, 6, 7, 8} >>> s1.union(s2) {1, 2, 3, 4, 5, ... Read More

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

Jayashree

Jayashree

Updated on 02-Mar-2020 10:18:38

119 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

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

Jayashree

Jayashree

Updated on 02-Mar-2020 10:18:04

124 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

How to find the fractional and integer parts of x in a two-item tuple in Python?

Jayashree

Jayashree

Updated on 02-Mar-2020 10:16:43

86 Views

The method modf() returns the fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float. First item in tuple is fractional part>>> import math >>> math.modf(100.73) (0.730000000000004, 100.0) >>> math.modf(-5.55) (-0.5499999999999998, -5.0)

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

Jayashree

Jayashree

Updated on 02-Mar-2020 10:16:14

737 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 ... Read More

How to Multiply Two Matrices using Python?

Jayashree

Jayashree

Updated on 02-Mar-2020 10:08:06

3K+ 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 ... Read More

How to check whether a number is prime or not using Python?

Jayashree

Jayashree

Updated on 02-Mar-2020 10:06:49

667 Views

Principle used in following solution to this problem is to divide given number with all from 3 its square root, a number's square root is largest possible factor beyond which it is not necessary to check if it is divisible by any other number to decide that it is prime ... Read More

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

Jayashree

Jayashree

Updated on 02-Mar-2020 10:01:42

2K+ 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 ... Read More

Advertisements