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 809 of 855
Binding function in Python Tkinter
In Python, Tkinter is a GUI library used for desktop application development. Binding functions is a crucial concept that connects events (like keyboard presses or mouse clicks) to specific functions, allowing your application to respond to user interactions. Binding Keyboard Events You can bind keyboard events to functions using the bind() method. When a key is pressed, the bound function executes automatically ? Example from tkinter import * # Function to handle key press events def press_any_key(event): value = event.char print(f'{value} - A button is pressed') ...
Read MoreAssociating a single value with all list items in Python
We may have a need to associate a given value with each and every element of a list. For example − there are the names of the days and we want to attach the word "day" as a suffix to them. Such scenarios can be handled in the following ways. Using itertools.repeat We can use the repeat method from the itertools module so that the same value is used again and again when paired with the values from the given list using the zip function ? from itertools import repeat days = ['Sun', 'Mon', 'Tues'] ...
Read MoreStandard errno system symbols in Python
Python provides built-in error handling through the errno module, which contains standard system error codes and messages. These error codes help identify specific types of errors that can occur during program execution. Listing All Standard Error Codes The errno module contains predefined error numbers and their corresponding symbolic names. We can list all available error codes using the errorcode dictionary along with os.strerror() to get human-readable descriptions ? import errno import os print("Error Code : Description") print("-" * 30) for error_code in sorted(errno.errorcode): print(f"{error_code} : {os.strerror(error_code)}") Error ...
Read MorePython Get the numeric prefix of given string
Sometimes we need to extract the numeric prefix from a string that contains numbers at the beginning. Python provides several approaches to extract only the numeric part from the start of a string. Using takewhile() with isdigit() The isdigit() method checks if each character is a digit. Combined with takewhile() from itertools, we can extract consecutive digits from the beginning ? from itertools import takewhile # Given string string_data = "347Hello" print("Given string:", string_data) # Using takewhile with isdigit result = ''.join(takewhile(str.isdigit, string_data)) print("Numeric prefix from the string:", result) The output ...
Read MorePython Get a list as input from user
In this article, we will see how to ask the user to enter elements of a list and create the list with those entered values. Python provides several approaches to collect user input and build lists dynamically. Using Loop with input() and append() The most straightforward approach is to ask for the number of elements first, then collect each element individually using a loop ? numbers = [] # Input number of elements n = int(input("Enter number of elements in the list : ")) # iterating till the range for i in range(0, n): ...
Read MorePython Generate successive element difference list
Finding the difference between successive elements in a list is a common operation in Python. This article explores three efficient methods to calculate consecutive element differences using list comprehension, list slicing, and the operator module. Using List Comprehension with Index The most straightforward approach uses list comprehension with index positions to access consecutive elements ? numbers = [12, 14, 78, 24, 24] # Given list print("Given list:", numbers) # Using index positions with list comprehension differences = [numbers[i + 1] - numbers[i] for i in range(len(numbers) - 1)] # Print result print("Successive differences:", ...
Read MorePython Generate random string of given length
In this article we will see how to generate a random string with a given length. This will be useful in creating random passwords or other programs where randomness is required. Using random.choices() The choices() function in the random module can produce multiple random characters which can then be joined to create a string of given length ? Example import string import random # Length of string needed N = 5 # With random.choices() res = ''.join(random.choices(string.ascii_letters + string.digits, k=N)) # Result print("Random string :", res) The output of the ...
Read MorePython Generate random numbers within a given range and store in a list
In this article we will see how to generate random numbers between a pair of numbers and store those values in a list. Python's random module provides the randint() function for this purpose. Syntax random.randint(start, end) Parameters: start − Lower bound (inclusive) end − Upper bound (inclusive) Both start and end should be integers Start should be less than or equal to end Using a Function with Loop In this example we use the range() function in a for loop. With help of append() we generate and add these random numbers ...
Read MorePython Categorize the given list by string size
Let's consider a list containing many strings of different lengths. In this article we will see how to group those elements into categories where the strings are of equal length in each group. Using For Loop We design a for loop which will iterate through every element of the list and append it only to the group where its length matches with the length of existing elements ? Example days = ['Monday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # Given list print("Given list:") print(days) # Categorize by string size len_comp = lambda x, y: len(x) ...
Read MoreConverting an image to ASCII image in Python
Converting an image to ASCII art involves transforming pixel brightness values into corresponding ASCII characters. This creates a text-based representation of the original image using characters like #, @, *, and . to represent different shades. Required Library We'll use the Pillow library for image processing − pip install Pillow ASCII Character Set First, define the ASCII characters arranged from darkest to lightest − ASCII_CHARS = ['#', '?', '%', '.', 'S', '+', '.', '*', ':', ', ', '@'] print("ASCII characters from dark to light:", ASCII_CHARS) ASCII characters from ...
Read More