Found 10413 Articles for Python

How to randomly select an item from a string in Python?

Vikram Chiluka
Updated on 27-Oct-2022 13:08:03

10K+ Views

In this article, we will show you how to randomly select an item from a string using python. Below are the various methods in python to accomplish this task − Using random.choice() method Using random.randrange() method Using random.randint() method Using random.random() Using random.sample() method Using random.choices() method Assume we have taken a string containing some elements. We will generate a random element from the given input string using different methods as specified above. Method 1: Using random.choice() method Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Use the import keyword ... Read More

How to randomly select an item from a tuple in Python?

Vikram Chiluka
Updated on 28-Oct-2022 11:37:12

6K+ Views

In this article, we will show you how to randomly select an item from a tuple using python. Below are the various methods in python to accomplish this task: Using random.choice() method Using random.randrange() method Using random.randint() method Using random.random() Using random.sample() method Using random.choices() method Assume we have taken a tuple containing some elements. We will generate a random element from the given input tuple using different methods as specified above. Using random.choice() method Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task – Use the import keyword to ... Read More

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

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

249 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

How to generate non-repeating random numbers in Python?

Vikram Chiluka
Updated on 26-Aug-2023 03:38:07

47K+ Views

In this article, we will show you how to generate non-repeating random numbers in Python. Below are the methods to accomplish this task: Using randint() & append() functions Using random.sample() method of given list Using random.sample() method of a range of numbers Using random.choices() method Using randint() & append() functions Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Use the import keyword, to import the random module. Create an empty list which is the resultant random numbers list. Use the for loop, to traverse the loop 15 times. Use ... Read More

How to overload Python comparison operators?

Pythonista
Updated on 02-Mar-2020 08:17:26

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

What is @ operator in Python?

Jayashree
Updated on 30-Jul-2019 22:30:22

624 Views

@ symbol is used to define decorator in Python. Decorators provide a simple syntax for calling higher-order functions. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.we have two different kinds of decorators in Python:Function decoratorsClass decorators A decorator in Python is any callable Python object that is used to modify a function or a class. A reference to a function  or a class  is passed to a decorator and the decorator returns a modified function or class. The modified functions or classes usually contain calls to ... Read More

How to implement Python __lt__ __gt__ 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

Is there a “not equal” operator in Python?

Pythonista
Updated on 30-Jul-2019 22:30:22

365 Views

In Python 2.x as well as != symbols are defined as 'not equal to' operators. In Python 3, operator is deprecated.

How to save a Python Dictionary to CSV file?

SaiKrishna Tavva
Updated on 23-Sep-2024 14:29:19

44K+ Views

In Python to save a dictionary to a CSV file, we can use the CSV' module. This process slightly depends on the structure of your dictionary. Generally, a CSV file refers to each line is corresponds to a row in a table, and each value in the line is separated by a comma. CSV files are widely used because they are easy to read and write (handling files) and also easy to transfer data in the form of strings. Common Approaches There are various scenarios for saving a Python Dictionary to a CSV file, in this article, we focus ... Read More

How to split Python tuples into sub-tuples?

Vikram Chiluka
Updated on 09-Nov-2022 07:48:02

10K+ Views

In this article, we will show you how to split python tuples into sub-tuples. Below are the various methods to accomplish this task − Using slicing Using enumerate() & mod operator Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable. Using slicing Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Create a variable to ... Read More

Advertisements