Found 10476 Articles for Python

How to calculate absolute value in Python?

Disha Verma
Updated on 20-May-2025 15:54:39

9K+ Views

The absolute value of a number is its non-negative representation. It specifies the distance of that number from zero on a number line, irrespective of its direction.For example, the absolute value of -2 is 2, and 2 is simply 2. In this article, we will explore how to calculate the absolute value of a number in Python. Calculating Absolute Value in Python The following methods can be efficiently used to calculate the absolute value of a number. Using User-Defined Function (Brute Method) Using abs() function ... Read More

Which data types are immutable in Python?

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

16K+ 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?

Disha Verma
Updated on 20-May-2025 15:27:23

421 Views

In Python, dictionaries are used to store data as key-value pairs, and lists store a collection of items separated by commas. To map two lists into a dictionary, we have various methods in Python that we will explore in this article. Mapping two lists into a Dictionary Mapping two lists into a dictionary can be done using the following methods - Using a Loop Using a Zip and Dict Method Using Dictionary Comprehension Using a Loop For loops are used to iterate ... Read More

How to access Python dictionary in simple way?

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

175 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?

Disha Verma
Updated on 20-May-2025 16:12:44

391 Views

In Python, objects are instances of classes containing attributes and methods, while dictionaries are collections of key-value pairs. We can obtain a dictionary from an object's fields - Using __dict__ Using vars() Using __dict__ You can access an object's attributes as a dictionary using its _dict_ attribute. In Python, every object has a _dict_ attribute that stores the object's attributes and their values as a dictionary (key-value pairs). Example In this example, we have defined a class Company with attributes Companyname and Location, and created ... Read More

How to convert Python Dictionary to a list?

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

107K+ 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 use continue statement in Python loop?

Pythonista
Updated on 27-Feb-2020 05:24:32

120 Views

The loop control statement continue abandons the pending statements in current iteration of the looping block and starts next iteration. The continue statement appears in a conditional block inside loopExamplex=0 while x

How to stop an infinite loop safely in Python?

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

428 Views

Infinite loop is the one that doesn't stop on its own. It happens when the looping condition continues to remain true forever. In such a case, the loop must be forcibly stopped by pressing ctrl-C to generate keyboard interrupt

How to prevent loops going into infinite mode in Python?

Gireesha Devara
Updated on 23-Aug-2023 19:02:26

3K+ Views

In python the while loop needs to be controlled by making some provision inside the body of the loop to drive the condition mentioned in the beginning to false. This is usually done by keeping count of iterations. If the while loop condition never evaluates to False, then we will have an infinite loop, which is a loop that never stops automatically, in this case we need to interrupt externally. count=0 while condition: stmt1 stmt2 . . count=count+1 Example Let’s take an example and ... Read More

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

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

7K+ 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

Advertisements