Found 34494 Articles for Programming

What is the maximum value of float in Python?

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

646 Views

In sys module, a struct sequence (tuple of named elements) called float_info has been defined. In this structure, an element max returns maximum representable finite float number. >>> import sys >>> sys.float_info.max 1.7976931348623157e+308

How to sort a dictionary in Python by keys?

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

501 Views

Standard distribution of Python contains collections module. It has definitions of high performance container data types. OrderedDict is a sub class of dictionary which remembers the order of entries added in dictionary object. When iterating over an ordered dictionary, the items are returned in the order their keys were first added. >>> from collections import OrderedDict >>> D = {5:'fff', 3:'ttt', 1:'ooo', 4:'bbb', 2:'ddd'} >>> OrderedDict(D.items())  OrderedDict([(5, 'fff'), (3, 'ttt'), (1, 'ooo'), (4, 'bbb'), (2, 'ddd')]) We also need to use sorted() function that sorts elements in an iterable in a specified order. The function takes a function ... Read More

How to iterate through a dictionary in Python?

Sarika Singh
Updated on 18-Aug-2022 11:42:27

11K+ Views

Dictionaries are a valuable and frequently used data structure in Python. This article tells us how to traverse through a dictionary while performing operations on its key-value pairs. Using dict.items() Method Python's dict.items() method allows you to loop through the dictionary. Each repetition will provide you with both the key and value of each item. Example Following is an example to iterate through a dictionary using dict.items() method − dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' ... Read More

How to print all the values of a dictionary in Python?

Sarika Singh
Updated on 23-Aug-2023 13:04:37

85K+ Views

Python has a built-in method called values() that returns a view object. The dictionary's values are listed in the view object. We can use the values() method in Python to retrieve all values from a dictionary. Python's built-in values() method returns a view object that represents a list of dictionaries containing every value. This article explains about the various ways to print all the values of a dictionary in Python. Using dict.values() Method Python's dict.values() method can be used to retrieve the dictionary values, which can then be printed using the print() function. Example Following is an example to print ... Read More

How to print all the keys of a dictionary in Python

Sarika Singh
Updated on 23-Aug-2023 12:57:45

97K+ Views

An unordered collection of data values is what a Python dictionary is. A Python dictionary contains a key: value pair, in contrast to other data structures that only include one value per entry. This article explains about the various ways to print all the keys of a dictionary in Python. Using dict.keys() Method Python's dict.keys() method can be used to retrieve the dictionary keys, which can then be printed using the print() function. A view object that shows a list of every key in the dictionary is the result of the dict.keys() method. The dictionary's elements can be accessed using ... Read More

How do we specify the buffer size when opening a file in Python?

Rajendra Dharmkar
Updated on 22-Aug-2023 11:28:45

6K+ Views

File handling is a critical aspect of seamless data management in the realm of computer programming. There are times when choosing the buffer size is crucial when working with files, particularly when handling huge files or carrying out certain actions that need efficient memory utilization. Because of its integrated file-handling features, the strong and flexible programming language Python gives developers the freedom to choose the buffer size when opening files. This extensive essay will examine how to set the buffer size in Python when opening a file. In order to explain the ideas, we will go over them step-by-step and ... Read More

What does the 'U' modifier do when a file is opened using Python?

Rajendra Dharmkar
Updated on 22-Aug-2023 10:23:24

732 Views

File handling plays a pivotal role in efficiently managing data in the realm of computer programming. Python, being a versatile and powerful language, equips developers with robust mechanisms for reading and writing files seamlessly. When working with files, Python offers a range of modifiers that allow for fine-tuning the behavior of the file object. One such modifier, the 'U' modifier, holds particular significance as it influences the way Python handles line endings when reading a file in text mode. In this comprehensive exploration, we will delve into the intricacies of the 'U' modifier and its implications when opening files in ... Read More

What does the 'b' modifier do when a file is opened using Python?

Sarika Singh
Updated on 14-Nov-2022 08:24:56

12K+ Views

If we open a file in oython using the b modifier. The file is opened in binary mode using the 'b' modifier. Any file whose format doesn't consist of readable characters is referred to as a "binary" file. Binary files include audio files like MP3s, text formats like Word or PDF, and image files like JPEGs or GIFs. Files are automatically opened in text mode in Python. When choosing a mode, include the letter "b" for binary mode. By default, the open() function opens a file in text format. As a result, the "wb" mode opens the file in binary ... Read More

What are the modes a file can be opened using Python?

Rajendra Dharmkar
Updated on 22-Aug-2023 10:21:36

5K+ Views

In the realm of Python programming, working with files demands a thorough grasp of the various modes in which files can be opened. The mode of file opening dictates the operations that can be performed on the file, be it reading, writing, or a combination of both. Python offers a wide array of modes tailored to diverse use cases and requirements. Whether you seek to read data from a file, write data to it, or simultaneously undertake both operations, selecting the appropriate file opening mode holds paramount importance for seamless file handling. In this comprehensive article, we embark on a ... Read More

How to check file last access time using Python?

Sarika Singh
Updated on 18-Aug-2022 08:29:35

2K+ Views

The file last access datetime in Python can be obtained in a number of different ways. The following OS module methods will be used to obtain the file last access time in Python. Using os.path.getatime() Method In Python, we can use the os.path.getatime() method to retrieve a path's most recent access time. The path that we need to verify for the access time is taken by this approach. The amount of time since the epoch is returned as a floating point value. It throws one OSError if the requested path cannot be reached or if it does not exist. Syntax ... Read More

Advertisements