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 by Pradeep Elance
Page 10 of 32
Python - Check if a given string is binary string or not
In this article we check if a given string contains only binary digits (0 and 1). We call such strings binary strings. If the string contains any other characters like 2, 3, or letters, we classify it as a non-binary string. Using set() Method The set() function creates a collection of unique characters from a string. We can compare this set with the valid binary characters {'0', '1'} to determine if the string is binary ? def is_binary_string_set(s): binary_chars = {'0', '1'} string_chars = set(s) ...
Read MorePassword validation in Python
Password validation is essential for securing applications. In this article, we will see how to validate if a given password meets certain complexity requirements using Python's re (regular expression) module. Password Requirements Our validation will check for the following criteria ? At least one lowercase letter (a-z) At least one uppercase letter (A-Z) At least one digit (0-9) At least one special character (@$!%*#?&) Password length between 8 and 18 characters Regular Expression Breakdown The regex pattern ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{8, 18}$ uses positive lookaheads to ensure all conditions are met ? ^ - ...
Read MoreMulti-dimensional lists in Python
Lists are a very widely used data structure in Python. They contain a list of elements separated by commas. But sometimes lists can also contain lists within them. These are called nested lists or multidimensional lists. In this article we will see how to create and access elements in a multidimensional list. Creating Multi-dimensional Lists In the below program we create a multidimensional list of 4 columns and 3 rows using nested for loops ? multlist = [[0 for columns in range(4)] for rows in range(3)] print(multlist) The output of the above code is ...
Read MoreLaunching parallel tasks in Python
Parallel processing allows Python programs to break work into independent subprograms that can run simultaneously. This improves performance by utilizing multiple CPU cores and reducing overall execution time. Using multiprocessing Module The multiprocessing module creates child processes that run independently from the main process. Each process has its own memory space and can execute code in parallel ? Basic Process Creation import multiprocessing import time class WorkerProcess(multiprocessing.Process): def __init__(self, process_id): super(WorkerProcess, self).__init__() self.process_id = ...
Read MoreImporting Data in Python
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 MoreCatching the ball game using Python
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 MoreBinding 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 More