Python Articles

Page 818 of 855

How can we print multiple blank lines in python?

Samual Sam
Samual Sam
Updated on 05-Mar-2020 3K+ Views

We can print multiple blank lines in python by using the character the number of times we need a blank line. For example, If you need 5 blank lines, you can use −Python 2.x: print "" Python 3.x: print("")You can use features like repetition operator in python to make this easier. For example,Python 2.x: print "" * 5 Python 3.x: print("" * 5)All of these commands will print 5 blank lines on the STDOUT.

Read More

Basic Calculator II in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 04-Mar-2020 989 Views

Suppose we have to implement a basic calculator to evaluate a simple expression string. The expression string will hold only non-negative integers, some operators like, +, -, *, / and empty spaces. The integer division should take the quotient part only.So if the input is like “3+2*2”, then the output will be 7.To solve this, we will follow these steps −define a stack s, i := 0, x := an empty stringfor each character j in sif j is not a blank characterappend j into xs := x, n := length of xwhile i < nif s[i] is ‘/’, thenincrease ...

Read More

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

Jayashree
Jayashree
Updated on 02-Mar-2020 227 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 256 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 354 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

What is vertical bar in Python bitwise assignment operator?

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

Vertical bar (|) stands for bitwise or operator. In case of two integer objects, it returns bitwise OR operation of two>>> a=4 >>> bin(a) '0b100' >>> b=5 >>> bin(b) '0b101' >>> a|b 5 >>> c=a|b >>> bin(c) '0b101'

Read More

How to implement Python __lt__ __gt__ custom (overloaded) operators?

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

Python has magic methods to define overloaded behaviour of operators. The comparison operators (=, == and !=) can be overloaded by providing definition to __lt__, __le__, __gt__, __ge__, __eq__ and __ne__ magic methods. Following program overloads < and > operators to compare objects of distance class. class distance: def __init__(self, x=5,y=5): self.ft=x self.inch=y def __eq__(self, other): if self.ft==other.ft and self.inch==other.inch: return "both objects are equal" else: return "both objects are not equal" def __lt__(self, other): in1=self.ft*12+self.inch in2=other.ft*12+other.inch if in1

Read More
Showing 8171–8180 of 8,547 articles
« Prev 1 816 817 818 819 820 855 Next »
Advertisements