Multiply Two Matrices Using Python

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

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

Check Whether a Number is Prime or Not Using Python

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

862 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 number.The function returns false for all numbers divisible by 2 and less than 2. For others return value of all) function will be false if it is divisible by any number upto its square root and true if it is not divisible by any numberExampledef is_prime(a):     if a ... Read More

Difference Between Integer and int in Java

Nitin Sharma
Updated on 02-Mar-2020 10:04:38

12K+ Views

A Java both int and Integer are used to store integer type data the major difference between both is type of int is primitive while Integer is of class type.This difference become significant when concept of OOPs comes in picture during development as int follows the principle of primitive data type while Integer behave as a wrapper class.Following are the important differences between int and Integer.Sr. No.KeyintInteger1TypeA int is a data type that stores 32 bit signed two's compliment integer.On other hand Integer is a wrapper class which wraps a primitive type int into an object.2Purposeint helps in storing integer ... Read More

Find Sum of Natural Numbers Using Recursion in Python

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

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

Check if a String Contains Only Numbers in Python

Jayashree
Updated on 02-Mar-2020 10:00:52

273 Views

Python has an in-built function isdigit() which returns true if all characters in a string are digit (between 0-9)>>> string='9764135408' >>> string.isdigit() True >>> string='091-9764135408' >>> string.isdigit() FalseYou can also use regex expression to check if string contains digits only.>>> import re >>> bool(re.match('^[0-9]+$','9764135408')) True >>> bool(re.match('^[0-9]+$','091-9764135408')) False

Generate Random Numbers Using Python Numpy

Jayashree
Updated on 02-Mar-2020 10:00:12

1K+ Views

The random module in Numpy package contains many functions for generation of random numbersnumpy.random.rand() − Create an array of the given shape and populate it with random samples>>> import numpy as np >>> np.random.rand(3,2) array([[0.10339983, 0.54395499], [0.31719352, 0.51220189], [0.98935914, 0.8240609 ]])numpy.random.randn() − Return a sample (or samples) from the “standard normal” distribution.>>> np.random.randn() -0.6808986872330651numpy.random.randint() − Return random integers from low (inclusive) to high (exclusive).>>> np.random.randint(5, size=(2, 4)) array([[2, 4, 0, 4], [3, 4, 1, 2]])numpy.random.random() − Return random floats in the half-open interval [0.0, 1.0).>>> np.random.random_sample() 0.054638060174776126

Modulus with Complex Numbers in Python

Jayashree
Updated on 02-Mar-2020 09:58:19

307 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)

What is Vertical Bar in Python Bitwise Assignment Operator

Pythonista
Updated on 02-Mar-2020 09:53:31

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'

Implement Python Custom Overloaded Operators

Pythonista
Updated on 02-Mar-2020 09:52:46

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

Deque At and Deque Swap in C++ STL

Sunidhi Bansal
Updated on 02-Mar-2020 09:33:32

253 Views

In this article we are going to discuss the deque::at() and deque::swap() function in C++ STL function syntax, working and its return values.What is deque::at() and deque::swap() function in STL?Deque or Double ended queues are as name suggests, sequence containers which can be expanded or contracted at both the ends. The user can easily insert data from any of the ends and similarly delete data from any of the ends. They are similar to vectors, but the only difference is that unlike vectors, contiguous storage allocation may not be guaranteed. Still Deque is more efficient in case of insertion and ... Read More

Advertisements