Found 34489 Articles for Programming

How to extract numbers from a string using Python?

Jayashree
Updated on 23-Jun-2020 06:13:54

493 Views

To extract each digit from a string −>>> str1='a34e 345 bcd 5he 78 xyz' >>> for s in str1: if s.isdigit():print (s) 3 4 3 4 5 5 7 8To extract only integers from a string in which words are separated by space character −>>> str1='h3110 23 cat 444.4 rabbit 11 2 dog' >>> for s in str1.split(): if s.isdigit(): print ((s)) 23 11 2

How to know Maximum and Minimum values for ints in Python?

Jayashree
Updated on 21-Feb-2020 12:51:33

70 Views

Python's core library has two built-in functions max() and min() respectively to find maximum and minimum number from a sequence of numbers in the form of list or tuple object.example>>> max(23,21,45,43) 45 >>> l1=[20,50,40,30] >>> max(l1) 50 >>> t1=(30,50,20,40) >>> max(t1) 50 >>> min(l1) 20 >>> min(t1) 20 >>> min(23,21,45,43) 21

How to calculate absolute value in Python?

Vikram Chiluka
Updated on 14-Feb-2023 10:04:35

8K+ Views

In this article, we will show you how to calculate the absolute value in python. Below are the methods to accomplish this task: Using User-Defined Function (Brute Method) Using abs() function Using math.fabs() function The magnitude of a number, whether positive or negative, is referred to as its absolute value. For example, the absolute value of -2, is 2, and 2 is simply 2. Important Points The absolute value is returned by the built-in abs() function. The math.fabs() function also returns the absolute value, but as a floating-point value. When we pass an integer or float value ... Read More

What is immutable in Python?

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

590 Views

Python defines variety of data types of objects. These objects are stored in memory. Contents of some objects can be changed after they are created while others can't be changed. Numeric objects such as integer, float and complex number objects occupy the memory and memory contents can not be changed. Such objects are called immutable. String and dictionary objects are also immutable. Tuple is also immutable. List object however is mutable because items in a list object can be modified, deleted or added in a list.

Which data types are immutable in Python?

Vikram Chiluka
Updated on 09-Sep-2023 09:39:52

12K+ Views

In this article, we will explain the immutable datatypes in Python. Python considers everything to be an object. A unique id is assigned to it when we instantiate an object. We cannot modify the type of object, but we may change its value. For example, if we set variable a to be a list, we can't change it to a tuple/dictionary, but we may modify the entries in that list. In Python, there are two kinds of objects. On the one hand, there are objects that can change their internal state (the data/content inside the objects), i.e. they can be ... Read More

How to map two lists into a dictionary in Python?

Pythonic
Updated on 30-Jul-2019 22:30:21

325 Views

Easiest way is to create a zip object that returns a generator of tuples, each having an item each from two lists. The zip object can then be transformed into a dictionary by using built-in dict() function >>> l1=['name', 'age', 'marks'] >>> l2=['Ravi', 23, 56] >>> z=zip(l1,l2) >>> newdict=dict(z) >>> newdict {'name': 'Ravi', 'age': 23, 'marks': 56}

How to access Python dictionary in simple way?

Pythonic
Updated on 30-Jul-2019 22:30:21

108 Views

There are two ways available to access value associated with a key in a dictionary collection object. The dictionary class method get() takes key as argument and returns value. >>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56} >>> d1.get('age') 23 Another way is to use key inside square brackets in front of dictionary object >>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56} >>> d1['age'] 23

How to create a Python dictionary from an object's fields?

Pythonic
Updated on 30-Jul-2019 22:30:21

254 Views

The __dict__ attribute returns a dictionary out of fields of any object. Let us define a class person >>> class person: def __init__(self): self.name='foo' self.age = 20 def show(self): print (self.name, self.age) We now declare an object of this class and obtain its __dict__ attribute which turns out to be dictionary object >>> p = person() >>> d = p.__dict__ >>> d {'name': 'foo', 'age': 20}

How to convert Python Dictionary to a list?

Vikram Chiluka
Updated on 23-Aug-2023 13:02:48

98K+ Views

In this article, we will show you how to convert a python dictionary to a list. Below are the methods to accomplish this task: Using list & items() Method Using keys() Method Using values() Method Using List Comprehension Using Zip() Function Using map() Function Using for loop & items() Method Dictionaries are Python's version of an associative array data structure. A dictionary is a collection of key-value pairs. Each key pair is represented by a key pair and its associated value. A dictionary is defined by a list of key-value pairs enclosed in curly braces and ... Read More

How to iterate over dictionaries using 'for' loops in Python?

Pythonic
Updated on 30-Jul-2019 22:30:21

269 Views

Even though dictionary itself is not an iterable object, the items(), keys() and values methods return iterable view objects which can be used to iterate through dictionary. The items() method returns a list of tuples, each tuple being key and value pair. >>> d1={'name': 'Ravi', 'age': 23, 'marks': 56} >>> for t in d1.items(): print (t) ('name', 'Ravi') ('age', 23) ('marks', 56) Key and value out of each pair can be separately stored in two variables and traversed like this − >>> d1={'name': 'Ravi', 'age': 23, 'marks': 56} >>> for k, v in d1.items(): print (k, ... Read More

Advertisements