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 Samual Sam
Page 4 of 151
Python program to check the validity of a Password?
Password validation is crucial for application security. A strong password should meet specific criteria including minimum length, mixed case letters, numbers, and special characters. Python's re module provides regular expressions to check these requirements efficiently. Password Validation Rules A valid password must satisfy the following criteria − Minimum 8 characters long 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 (_ @ $) No whitespace characters Algorithm Step 1: Import re module for regular expressions Step 2: Get password ...
Read MoreGet emotions of images using Microsoft emotion API in Python?
Every human being has emotions like happy, sad, neutral, surprise, sorrow, and more. We can analyze these emotions in images using Python with Microsoft's Cognitive Services Emotion API. This API can detect and classify facial expressions in photographs. The Microsoft Emotion API analyzes facial expressions and returns confidence scores for different emotions including happiness, sadness, surprise, anger, fear, contempt, disgust, and neutral. Prerequisites Before using the Emotion API, you need to: Register for a Microsoft Azure account Subscribe to the Cognitive Services Emotion API Obtain your subscription key Install required Python packages: requests and json ...
Read MoreMap function and Lambda expression in Python to replace characters
Sometimes we need to swap specific characters in a string. Python's map() function combined with lambda expressions provides an elegant solution for character replacement operations. Problem Statement We want to replace character a1 with character a2 and a2 with a1 simultaneously. For example ? Input string: "puporials toinp" Characters to swap: p and t Expected output: "tutorials point" Using map() and Lambda Expression The map() function applies a lambda expression to each character in the string. The lambda handles the character swapping logic ? def replaceUsingMapAndLambda(sent, a1, ...
Read MoreRemove all duplicates from a given string in Python
To remove all duplicates from a string in Python, we need to first split the string by spaces so that we have each word in an array. Then there are multiple ways to remove duplicates from the resulting list. We can remove duplicates by first converting all words to lowercase, then sorting them and finally picking only the unique ones. Let's explore different approaches ? Using Manual Loop with Sorting This approach converts words to lowercase, sorts them, and uses a loop to extract unique words ? sent = "Hi my name is John Doe ...
Read MoreHow to compare numbers in Python?
You can use relational operators in Python to compare numbers (both float and int). These operators compare the values on either side of them and decide the relation among them. Comparison Operators Python provides six main comparison operators for comparing numbers ? Operator Description Example (a=10, b=20) Result == Equal to (a == b) False != Not equal to (a != b) True > Greater than (a > b) False = Greater than or equal to (a >= b) False , =,
Read MoreWhat are the best practices for using loops in Python?
Loops are fundamental constructs in Python programming, and following best practices can significantly improve both performance and code readability. While Python's interpreter handles some optimizations automatically, developers should write efficient loop code to avoid performance bottlenecks. Avoid Repeated Function Calls The most critical optimization is to avoid calling functions repeatedly inside loops. Operations that seem fast become expensive when executed thousands of times ? Example: Computing Length Outside the Loop # Efficient: compute length once numbers = [i for i in range(1000000)] length = len(numbers) for i in numbers: print(i - ...
Read MoreHow to execute Python multi-line statements in the one-line at command-line?
There are multiple ways to execute Python multi-line statements in a single command-line call. You can use bash's multi-line support or compress statements using newline characters. Using Bash Multi-line Support Bash supports multi-line statements, which you can use with the python -c command ? $ python -c ' > a = True > if a: > print("a is true") > ' The output of the above command is ? a is true Using Newline Characters in Single Line If you prefer to have the Python ...
Read MoreHow to optimize nested if...elif...else in Python?
Nested if...elif...else statements can slow down your program if not structured efficiently. Here are proven techniques to optimize them for better performance and readability. Put Most Common Conditions First Place the most frequently executed condition at the top to minimize the number of checks ? # Less efficient - rare condition checked first def process_score(score): if score >= 95: # Only 5% of students return "Excellent" elif score >= 85: # 15% of students ...
Read MoreCan we change operator precedence in Python?
No, you cannot change operator precedence in Python. Operator precedence is built into the Python language itself and determines how the parser builds syntax trees from expressions. What is Operator Precedence? Operator precedence determines the order in which operations are performed when multiple operators appear in an expression. Python follows a predetermined precedence similar to mathematical conventions and most programming languages. Example of Default Precedence Here's how Python's built-in precedence works ? # Multiplication has higher precedence than addition result1 = 2 + 3 * 4 print("2 + 3 * 4 =", result1) ...
Read MoreHow to split Python dictionary into multiple keys, dividing the values equally?
Sometimes you need to distribute dictionary values equally across new keys. This is useful for data partitioning, load balancing, or creating balanced datasets from existing data structures. Basic Dictionary Splitting Here's how to split a dictionary into multiple keys with equal distribution of values − # Original dictionary data = {'items': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} num_splits = 3 # Calculate chunk size chunk_size = len(data['items']) // num_splits remainder = len(data['items']) % num_splits # Split the values result = {} start = 0 for i in range(num_splits): ...
Read More