Programming Articles

Page 2375 of 2547

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

Jayashree
Jayashree
Updated on 02-Mar-2020 241 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 279 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 365 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 start object-oriented programming in C++?

Arjun Thakur
Arjun Thakur
Updated on 02-Mar-2020 982 Views

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of attributes; and instructions to do things, in the form of methods.For example, a person is an object which has certain properties such as height, gender, age, etc. It also has certain methods such as move, talk, and so on.ObjectThis is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.ClassWhen you define a class, you define a blueprint for an object. This doesn't actually ...

Read More

Why should C++ programmers minimize use of 'new'?

Ankith Reddy
Ankith Reddy
Updated on 02-Mar-2020 178 Views

new is used for dynamic memory allocation. The memory allocated in this case goes on the heap. There are several costs associated with this type of memory allocation along with the programmer having to do manual memory cleaning and management. This type of allocation must be used when − You don't know how much memory you need at compile time.You want to allocate memory which will persist after leaving the current block.Other than these, there are very few cases where dynamic memory allocation is required. This is because, in C++, there is the concept of a destructor. This function gets called ...

Read More

How to Swap Two Variables using Python?

Jayashree
Jayashree
Updated on 02-Mar-2020 477 Views

By using a temporary variable −>>> x=10 >>> y=20 >>> z=x >>> x=y >>> y=z >>> x,y (20, 10)Without using temporary variable>>> a,b=5,7 >>> a,b (5, 7) >>> a,b=b,a >>> a,b (7, 5)

Read More
Showing 23741–23750 of 25,466 articles
Advertisements