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
Python Articles
Page 807 of 855
Python - end parameter in print()
The print() function in Python automatically adds a newline character () at the end of each print statement. However, you can customize this behavior using the end parameter to specify different ending characters or strings. Syntax print(value1, value2, ..., end='character_or_string') Default Behavior By default, print() ends with a newline character ? print("Welcome to") print("Tutorialspoint") Welcome to Tutorialspoint Using Space as End Character You can replace the newline with a space to print on the same line ? print("Welcome to", end=' ') print("Tutorialspoint") ...
Read MorePython - Difference in keys of two dictionaries
Two Python dictionaries may contain some common keys between them. In this article we will find how to get the difference in the keys present in two given dictionaries. Using Set Difference Here we take two dictionaries and apply set function to them. Then we subtract the two sets to get the difference. We do it both ways, by subtracting second dictionary from first and next subtracting first dictionary from second. Those keys which are not common are listed in the result set ? Example dictA = {'1': 'Mon', '2': 'Tue', '3': 'Wed'} print("1st Dictionary:", ...
Read MorePython - Create a dictionary using list with none values
Sometimes you need to create a dictionary from a list where each list element becomes a key with a None value. This is useful for creating placeholder dictionaries or initializing data structures. Python provides several methods to achieve this conversion. Using dict.fromkeys() The dict.fromkeys() method creates a dictionary with keys from an iterable and assigns the same value to all keys. By default, it assigns None ? days = ["Mon", "Tue", "Wed", "Thu", "Fri"] print("Given list:") print(days) result = dict.fromkeys(days) print("Dictionary with None values:") print(result) Given list: ['Mon', 'Tue', 'Wed', 'Thu', ...
Read MorePython - Convert given list into nested list
Sometimes we need to convert list elements into individual sublists, creating a nested list structure. Python provides several approaches to achieve this transformation depending on your specific requirements. Using List Comprehension The simplest approach is to wrap each element in a list using list comprehension ? days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] print("Given list:", days) # Convert each element to a sublist nested_list = [[item] for item in days] print("Nested list:", nested_list) Given list: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] Nested list: [['Mon'], ['Tue'], ['Wed'], ['Thu'], ['Fri']] Using map() Function ...
Read MoreBroadcasting with NumPy Arrays in Python
Broadcasting is a NumPy feature that allows arithmetic operations between arrays of different shapes without explicitly reshaping them. When arrays have unequal dimensions, NumPy automatically adjusts the smaller array's shape by prepending dimensions of size 1, enabling element-wise operations. Rules of Array Broadcasting NumPy follows these rules when broadcasting arrays ? Arrays with smaller ndim are prepended with dimensions of size 1 in their shape. The output shape in each dimension is the maximum of the input sizes in that dimension. An array can be used in calculation if its size in a particular dimension matches ...
Read MoreAvoiding class data shared among the instances in Python
When we create multiple instances of a class in Python, class variables are shared among all instances. This can lead to unexpected behavior when modifying mutable objects like lists or dictionaries. In this article, we will explore the problem and two effective solutions to avoid shared class data. The Problem: Shared Class Variables In the below example, we demonstrate how class variables are shared across all instances, leading to unintended data sharing ? class MyClass: listA = [] # This is a class variable, shared by all instances # Instantiate ...
Read MoreAverage of each n-length consecutive segment in a Python list
We have a list containing only numbers. We plan to get the average of a set of sequential numbers from the list which keeps rolling from the first number to next number and then to next number and so on. Example The below example simplifies the requirement of finding the average of each 4-length consecutive elements of the list ? Given list: [10, 12, 14, 16, 18, 20, 22, 24, 26] Average of every segment of 4 consecutive numbers: [13.0, 15.0, 17.0, 19.0, 21.0, 23.0] Using sum() and range() We use the ...
Read Moreasksaveasfile() function in Python Tkinter
Tkinter is Python's built-in GUI toolkit. The asksaveasfile() function from tkinter.filedialog opens a save dialog that allows users to specify where to save a file and returns a file object for writing. Syntax asksaveasfile(mode='w', **options) Parameters Common parameters include: mode − File opening mode (default: 'w' for write) filetypes − List of file type tuples defaultextension − Default file extension initialdir − Initial directory to open title − Dialog window title Basic Example Here's how to create a simple save file dialog ? import tkinter as tk ...
Read MoreArithmetic operations in excel file using openpyxl in Python
Python can help us work with Excel files directly from the Python environment using the openpyxl module. We can refer to individual cells or ranges of cells in Excel and apply arithmetic operators on them. The results of these operations can be stored in specific cells whose location we define in our Python program. In the examples below, we perform various arithmetic operations using built-in Excel functions like SUM, AVERAGE, PRODUCT, and COUNT. We use the openpyxl module to create a workbook, store values in predefined cells, apply functions on those cells, and save the results to other cells ...
Read MorePython - Contiguous Boolean Range
Given a list of boolean values, we are interested to know the positions where contiguous ranges of the same boolean value start and end. This means finding where a sequence of True values begins and ends, and where a sequence of False values begins and ends. Using itertools We can use accumulate along with groupby from the itertools module. The groupby function groups consecutive identical values, and accumulate helps track the cumulative positions where each group ends ? Example from itertools import accumulate, groupby # Given list bool_values = [False, True, True, False, False] ...
Read More