Found 10414 Articles for Python

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

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

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

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

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

402 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

159 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

363 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

119 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

398 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

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

Advertisements