Niharikaa Aitam

Niharikaa Aitam

77 Articles Published

Articles by Niharikaa Aitam

Page 4 of 8

How to sort a dictionary in Python by values?

Niharikaa Aitam
Niharikaa Aitam
Updated on 29-May-2025 2K+ Views

Python allows dictionaries to be sorted using the built-in sorted() function. In this article, we will explore different ways to sort a dictionary in Python by its values. Sort a Dictionary Using itemgetter() method When we use the sorted() function along with the itemgetter() method of the operator module, the dictionary will be sorted by its values. The itemgetter() function returns a callable object that fetches an item from its operand using the provided index. Example Following is an example that shows how to sort a dictionary by its values using itemgetter() along with the sorted() function - from operator import ...

Read More

What is the maximum value of float in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 29-May-2025 1K+ Views

In Python, floating-point numbers can be implemented using double-precision, i.e., 64-bit format, as per the IEEE 754 standard. These floats have a finite range and precision. The maximum value that a float can represent depends on the platform that we are using, but can be accessed using the sys.float_info attribute. Maximum Value of Float using sys.float_info The sys.float_info object provides details about the float implementation on the current platform. The attribute float_info.max gives the largest positive floating-point number representation. Example Here in this example, we are going to use float_info.max attribute from the sys.float_info object to get the maximum float value ...

Read More

How to sort a dictionary in Python by keys?

Niharikaa Aitam
Niharikaa Aitam
Updated on 29-May-2025 740 Views

Python allows dictionaries to be sorted using the built-in sorted() function. Although dictionaries in Python 3.7+ preserve insertion order, we can still create a new dictionary sorted by its keys using various methods. In this article, we will explore different ways to sort a dictionary in Python by its keys. Using sorted() with a for Loop We can use the sorted() function on the dictionary keys and then iterate through them to build a new dictionary in key order. Example Following is an example, which shows how to sort a dictionary in Python using the sorted() function - dictionary = ...

Read More

How to change any data type into a string in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 29-May-2025 347 Views

In Python, converting different data types into strings is a common requirement when we want to display or store information in text format. The built-in str() function is used for this conversion. This function accepts any Python object as a parameter and returns a string representation of it. The basic syntax of the str() function is as follows: str(object) Where object is any valid Python object, such as int, float, bool, list, etc. In this article, we will explore different data type conversions into a string in Python. Conversion of Integer to String An integer can be converted into ...

Read More

What is correct syntax to create Python dictionary?

Niharikaa Aitam
Niharikaa Aitam
Updated on 29-May-2025 3K+ Views

A Dictionary is a set of key-value pairs, and each key in a Dictionary is separated from its value by a colon ":"; the items are separated by commas. They do not allow duplicate values. Since the 3.7 Python update, dictionaries are ordered. The correct syntax to create a Python Dictionary is to store values in the form of key-value pairs within the curly braces "{ }". On the left of the colon, we store keys, and on the right, values. The keys should be unique (within one dictionary), and we need to separate the items using commas. Basic ...

Read More

How are files extracted from a tar file using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 28-May-2025 22K+ Views

A TAR file is abbreviated as Tape Archive, which is an archive format used mainly in Unix and Linux environments. The Tar file is used to collect more number of files into a single file, which makes it easier to share or backup together. In Python, when we want to work with TAR files, we can use the tarfile module, which allows us to create, read and extract TAR archives programmatically. In this article, we will explore how to extract the files from tar file by using Python. Extracting all files from a"tar" file The extractall() method in Python is ...

Read More

How to print a string two times with single statement in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 28-May-2025 388 Views

Printing a String Twice Using Multiplication When we are developing Python, we may want to repeat or duplicate a string for display or formatting purposes. In such cases, Python provides several ways to print a string in defined number of times with a single statement. There are different ways to print a string twice with a single statement in Python. They are - Using Multiplication Operator Using the Concatenation Operator Using the format() Method Using f-string Using join() ...

Read More

How an entire file is read into buffer and returned as a string in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 28-May-2025 9K+ Views

When dealing with large files in Python, it is not efficient to read the entire file into memory at once. In such cases, we can read the file in chunks using a specified buffer size. This helps reduce memory consumption and allows for efficient processing of large files. Following is the syntax of using the Buffer while reading a file - with open('filename', 'r') as file: chunk = file.read(buffer_size) Where, filename: The path of the file. 'r': Read mode buffer_size: Number of characters ...

Read More

How to zip a folder recursively using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 27-May-2025 7K+ Views

Zipping a folder recursively means compressing a folder along with all its subfolders and files. In this article, we will explore all the possible ways to zip a folder recursively using Python. Using zipfile and os.walk() In Python, we have the methods zipfile() and os.walk() to zip all the folders recursively. This is a manual approach of zipping the available subfolders and files into one. Following is an example that shows how to zip a folder recursively using Python - import zipfile import os def zip_folder_manual(folder_path, zip_path): with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: ...

Read More

How are files added to a tar file using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 27-May-2025 3K+ Views

Python offers a built-in module named tarfile, which allows developers to create, read, and modify tar archives, i.e., standard Unix tarballs. We can use this module to compress multiple files into a single archive, with or without compression. Different File Modes to Create a tar file Here are the available different file modes to create a tar file using Python - "w": Write a tar archive without compression. "w:gz": Write a gzip-compressed archive. "w:bz2": Write a bzip2-compressed archive. "w:xz": Write an xz-compressed archive (Python 3.3+). Adding a Single File to ...

Read More
Showing 31–40 of 77 articles
« Prev 1 2 3 4 5 6 8 Next »
Advertisements