Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to split Python tuples into sub-tuples?
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 MoreHow to zip a Python Dictionary and List together?
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 MoreHow to create Python dictionary with duplicate keys?
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 MoreHow to do Python math at command line?
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 MoreHow to know Maximum and Minimum values for ints in Python?
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 MoreHow to create a Python dictionary from an object\'s fields?
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 MoreHow to prevent loops going into infinite mode in Python?
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 MoreHow to print a value for a given key for Python dictionary?
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 MoreHow to convert float to integer in Python?
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 MoreHow to convert an integer to a character in Python?
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