Articles on Trending Technologies

Technical articles with clear explanations and examples

How to split Python tuples into sub-tuples?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 11K+ Views

In this article, we will show you how to split Python tuples into sub-tuples of equal size. Tuples are immutable, ordered collections in Python that can be divided into smaller chunks using various methods. What are Tuples? Tuples are immutable, ordered data structures used to store collections in Python. Unlike lists which are mutable and have variable length, tuples have a fixed length once created and cannot be modified. Method 1: Using Slicing The most straightforward approach uses slice notation with the range() function to create sub-tuples of a specific size. Example The following ...

Read More

How to zip a Python Dictionary and List together?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 13K+ Views

In this article, we will show you how to zip a Python dictionary and list together. Below are the various methods to accomplish this task ? Using zip() function Using append() function and items() function Using append() function and in operator Using zip() Function The zip() function combines two iterables by pairing their elements. When zipping a dictionary with a list, we use items() to get key-value pairs from the dictionary ? # input dictionary input_dict = {'Hello': 1, 'tutorialspoint': 2, 'python': 3, 'codes': 3} # input list input_list = [10, 20, ...

Read More

How to create Python dictionary with duplicate keys?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 7K+ Views

In Python, a dictionary doesn't allow duplicate keys. However, we can work around this limitation using defaultdict from the Collections module to store multiple values for the same key in the form of lists. Understanding defaultdict The defaultdict is a subclass of the built-in dict class that provides a default value for keys that don't exist. When you access a missing key, it automatically creates that key with a default value using a default factory function. from collections import defaultdict # Create defaultdict with list as default factory d = defaultdict(list) print(type(d)) print(d['new_key']) # ...

Read More

How to do Python math at command line?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 2K+ Views

Python can be used as a powerful calculator at the command line. When you invoke the Python interpreter, you get the (>>>) prompt where any mathematical expression can be entered and evaluated immediately upon pressing ENTER. Python Math Operators Python provides several arithmetic operators for mathematical calculations ? Operation Description a + b Addition - returns the sum of a and b a - b Subtraction - returns the difference of a and b -a Negation - changes the sign of a +a Identity - returns a ...

Read More

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

Jayashree
Jayashree
Updated on 24-Mar-2026 185 Views

Python's core library provides built-in functions max() and min() to find maximum and minimum values from sequences like lists, tuples, or multiple arguments. Additionally, Python offers ways to discover the system limits for integer values. Using max() and min() Functions With Multiple Arguments You can pass multiple numbers directly to max() and min() − print(max(23, 21, 45, 43)) print(min(23, 21, 45, 43)) 45 21 With Lists and Tuples These functions also work with sequence objects like lists and tuples − numbers_list = [20, 50, 40, 30] numbers_tuple ...

Read More

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

Disha Verma
Disha Verma
Updated on 24-Mar-2026 520 Views

In Python, objects are instances of classes containing attributes and methods, while dictionaries are collections of key-value pairs. Converting an object's fields to a dictionary is useful for serialization, debugging, or data processing. Python provides two main approaches for this conversion. Using __dict__ Attribute Every Python object has a __dict__ attribute that stores the object's attributes and their values as a dictionary. This attribute directly exposes the internal namespace of the object ? Example class Company: def __init__(self, company_name, location): self.company_name = company_name ...

Read More

How to prevent loops going into infinite mode in Python?

Gireesha Devara
Gireesha Devara
Updated on 24-Mar-2026 3K+ Views

In Python, while loops can run infinitely if the loop condition never becomes False. To prevent infinite loops, you need to modify the condition variable inside the loop body or use control statements like break. Using a Counter Variable The most common approach is to use a counter that gets incremented in each iteration ? count = 0 while count < 5: print('Python!') count += 1 Python! Python! Python! Python! Python! Here, the count variable is incremented in each iteration, ensuring the condition ...

Read More

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

Jayashree
Jayashree
Updated on 24-Mar-2026 8K+ Views

Python dictionary is a collection of key-value pairs. There are several ways to print or access the value associated with a specific key. Using the get() Method The get() method returns the value for a given key. If the key doesn't exist, it returns None by default ? D1 = {'a': 11, 'b': 22, 'c': 33} print(D1.get('b')) print(D1.get('d')) # Key doesn't exist 22 None Using get() with Default Value You can provide a default value to return when the key is not found ? D1 = {'a': ...

Read More

How to convert float to integer in Python?

Gireesha Devara
Gireesha Devara
Updated on 24-Mar-2026 3K+ Views

In Python there are two main numeric data types: integers and floats. Integers are whole numbers without decimal points, while floats contain decimal values. Python provides several built-in methods to convert float values to integers ? Using the int() Function The int() function converts floating-point numbers to integers by truncating the decimal part. It does not round values − it simply removes everything after the decimal point. Basic Float to Integer Conversion Here's a simple example of converting a float to an integer ? num = 39.98 print('Data type of num:', type(num).__name__) ...

Read More

How to convert an integer to a character in Python?

Gireesha Devara
Gireesha Devara
Updated on 24-Mar-2026 39K+ Views

To convert an integer to a character in Python, we can use the chr() method. The chr() is a Python built−in function that returns a Unicode character corresponding to the given integer value. Syntax chr(number) Parameters number − An integer between 0 and 1, 114, 111 (0x110000 in hexadecimal). Return Value Returns a Unicode character corresponding to the integer argument. Raises ValueError if the integer is out of range (not in range(0x110000)). Raises TypeError for non−integer arguments. Basic Example Let's convert integer 100 to its corresponding Unicode character ? ...

Read More
Showing 1–10 of 61,302 articles
« Prev 1 2 3 4 5 6131 Next »
Advertisements