Jayashree has Published 41 Articles

How do we check in Python whether a string contains only numbers?

Jayashree

Jayashree

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

177 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')) FalseRead More

How to use Python Numpy to generate Random Numbers?

Jayashree

Jayashree

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

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

How does modulus work with complex numbers in Python?

Jayashree

Jayashree

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

219 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: ... Read More

How to Swap Two Variables using Python?

Jayashree

Jayashree

Updated on 02-Mar-2020 07:40:44

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

How to Find LCM using Python?

Jayashree

Jayashree

Updated on 02-Mar-2020 05:16:37

675 Views

 LCM (Least common multiple) of two (or more) numbers is a number which is the smallest number that is divisible by both (or all).First we find the larger number of two given numbers. Starting from it we try and find the first number that is divisible by both, which is ... Read More

How to print a value for a given key for Python dictionary?

Jayashree

Jayashree

Updated on 26-Feb-2020 11:22:05

5K+ Views

Python dictionary is collection of key value pairs. Value associated with a certain key is returned by get() method.>>> D1={'a':11,'b':22,'c':33} >>> D1.get('b') 22You can also obtain value by using key inside square brackets.>>> D1['c'] 33

How does Python dictionary keys() Method work?

Jayashree

Jayashree

Updated on 25-Feb-2020 11:24:09

277 Views

The keys() method of Python dictionary class returns a view object consisting of keys used in the dictionary.>>> d1 = {'name': 'Ravi', 'age': 21, 'marks': 60, 'course': 'Computer Engg'} >>>d1.keys() dict_keys(['name', 'age', 'marks', 'course'])It can be stored as a list object. If new key-value pair is added, the view object ... Read More

How to Find Armstrong Number in an Interval using Python?

Jayashree

Jayashree

Updated on 21-Feb-2020 12:58:37

696 Views

If sum of cubes of individual digits in a number add up to the number itself, it is called armstrong number. for example 153=1**3+5**3+3**3ExampleFollowing Python program find armstrong numbers between 100 to 1000for num in range(100, 1000):   temp=num   sum=0   while temp>0:       digit=temp%10     ... Read More

How to Find the Sum of Natural Numbers using Python?

Jayashree

Jayashree

Updated on 21-Feb-2020 12:54:53

996 Views

You can use while loop to successively increment value of a variable i by one and adding it cumulatively.s,i=0,0 n=10 while i

How to Find Factorial of Number Using Recursion in Python?

Jayashree

Jayashree

Updated on 21-Feb-2020 12:54:14

677 Views

Factorial of a number is product of all numbers from 1 to that number.A function is called a recursive function if it calls itself.In following program factorial() function accepts one argument and keeps calling itself by reducing value by one till it reaches 1.Exampledef factorial(x):     if x==1:   ... Read More

Advertisements