Programming Articles

Page 901 of 2547

Importing Data in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 17K+ Views

When working with data analysis in Python, importing data from external sources is a fundamental skill. Python provides several powerful modules for reading different file formats including CSV, Excel, and databases. Let's explore the most common approaches for importing data. Importing CSV Files The built-in csv module allows us to read CSV files row by row using a specified delimiter. We open the file in read mode and iterate through each row ? import csv import io # Sample CSV data csv_data = """customerID, gender, Contract, PaperlessBilling, Churn 7590-VHVEG, Female, Month-to-month, Yes, No 5575-GNVDE, Male, ...

Read More

Catching the ball game using Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 2K+ Views

Python's tkinter library makes it easy to create interactive games. In this article, we'll build a ball catching game where a ball falls from random positions and the player moves a paddle to catch it using left and right buttons. Game Overview The game consists of a falling blue ball and a green paddle at the bottom. Players use two buttons to move the paddle left or right to catch the ball. Each successful catch scores 5 points, and missing a ball ends the game. Approach The game development follows these key steps: Step ...

Read More

Binding function in Python Tkinter

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 2K+ Views

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 More

Associating a single value with all list items in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 428 Views

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 More

Standard errno system symbols in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 578 Views

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 More

Python Get the numeric prefix of given string

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 459 Views

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 More

Python Get a list as input from user

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 10K+ Views

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 More

Python Generate successive element difference list

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 543 Views

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 More

Python Generate random string of given length

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 744 Views

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 More

Python Generate random numbers within a given range and store in a list

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 591 Views

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 More
Showing 9001–9010 of 25,466 articles
« Prev 1 899 900 901 902 903 2547 Next »
Advertisements