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 823 of 855
Python - Check if two lists have any element in common
When working with Python lists, we often need to check if two lists share any common elements. This is useful for data comparison, finding overlaps, or validating data consistency. Python provides several efficient approaches to solve this problem. Using the 'in' Operator with Loops The most straightforward approach uses nested loops to check each element of the first list against all elements in the second list ? # Sample lists for comparison fruits = ['apple', 'banana', 'orange', 'mango'] colors = ['red', 'yellow', 'orange', 'blue'] numbers = [1, 2, 3, 4, 5] def has_common_elements(list1, list2): ...
Read Morehtml5lib and lxml parsers in Python
html5lib is a pure-python library for parsing HTML. It is designed to conform to the WHATWG HTML specification, as is implemented by all major web browsers. It can parse almost all the elements of an HTML doc, breaking it down into different tags and pieces which can be filtered out for various use cases. It parses the text the same way as done by the major browsers. It can also tackle broken HTML tags and add some necessary tags to complete the structure. lxml is also a similar parser but driven by XML features than HTML. It has dependency ...
Read MoreGenerating hash ids using uuid3() and uuid5() in Python
The universally unique identifier (UUID) is a 128-bit hexadecimal number that guarantees uniqueness within a given namespace. Python's uuid module provides uuid3() and uuid5() functions to generate hash-based UUIDs from a namespace and name string. Syntax uuid.uuid3(namespace, name) uuid.uuid5(namespace, name) Both functions take two parameters: namespace − A UUID object defining the namespace name − A string used to generate the UUID Key Differences Function Hash Algorithm Security Use Case uuid3() MD5 Lower General purpose uuid5() SHA-1 Higher Security-sensitive applications Common ...
Read MoreFind frequency of each word in a string in Python
As a part of text analytics, we frequently need to count words and assign weightage to them for processing in various algorithms. In this article, we will explore three different approaches to find the frequency of each word in a given sentence. Using Counter from Collections The Counter class from the collections module provides an elegant way to count word frequencies. It creates a dictionary where keys are words and values are their counts ? from collections import Counter line_text = "Learn and practice and learn to practice" freq = Counter(line_text.split()) print("Word frequencies:", freq) print("Most ...
Read MoreFacebook Login using Python
We can use the Python package called Selenium to automate web browser interactions. In this article, we will see how to automate Facebook login using Python's Selenium package. Prerequisites Before automating Facebook login, we need to set up Selenium and a web driver. Here are the required steps ? Step 1: Install Selenium Install the Selenium package in your Python environment ? pip install selenium Step 2: Install WebDriver Download ChromeDriver from the official website or install it using webdriver-manager ? pip install webdriver-manager Finding HTML Elements ...
Read MoreDictionary Methods in Python (cmp(), len(), items()...)
Dictionary in Python is one of the most frequently used collection data types. It is represented by key-value pairs where keys are indexed but values may not be. There are many Python built-in functions that make using dictionaries very easy in various Python programs. In this topic we will see three built-in methods: cmp(), len(), and items(). cmp() Method The cmp() method compares two dictionaries based on keys and values. It is helpful in identifying duplicate dictionaries as well as doing relational comparisons among dictionaries. Note: This method is only available in Python 2 and has been removed ...
Read MoreFind the first repeated word in a string in Python using Dictionary
In a given sentence, there may be a word which gets repeated before the sentence ends. In this Python program, we are going to find the first word that appears multiple times in a string using a dictionary approach. The logical steps we follow to get this result are: Split the given string into words separated by space Convert these words into a dictionary using collections.Counter Traverse the list of words and find the first word with frequency > 1 Using collections.Counter ...
Read MoreDifferent messages in Tkinter - Python
Tkinter is Python's built-in GUI toolkit that provides various message display options for user interactions and program state changes. The Message widget displays multi-line text with customizable formatting, while the messagebox module offers standard dialog boxes like confirmations, errors, and warnings. Custom Message Widget The Message widget displays text with customizable appearance ? import tkinter as tk main = tk.Tk() key = "The key to success is to focus on goals and not on obstacles" message = tk.Message(main, text=key) message.config(bg='lightblue', font=('Arial', 14, 'italic'), width=300) message.pack(padx=20, pady=20) main.mainloop() This creates a ...
Read MoreCreate a stopwatch using python
A stopwatch is used to measure the time interval between two events, usually in seconds to minutes. It has various usage like in sports or measuring the flow of heat, current etc in an industrial setup. Python can be used to create a stopwatch by using its tkinter library. This library provides GUI features to create a stopwatch showing the Start, Stop and Reset options. The key component of the program is using the label.after() method of tkinter. Syntax label.after(ms, function=None) Parameters: ms − Time in milliseconds function − Callback function to execute ...
Read MoreFind all the patterns of "1(0+)1" in a given string using Python Regex
In this tutorial, we will find all occurrences of the pattern "1(0+)1" in a string using Python's regex module. The re module provides powerful pattern matching capabilities for text processing. The pattern "1(0+)1" represents a literal string containing the number 1, followed by parentheses with "0+" inside, and ending with another 1. Example Input and Output Input: string = "Sample 1(0+)1 string with 1(0+)1 unnecessary patterns 1(0+)1" Output: Total number of pattern matches are 3 ['1(0+)1', '1(0+)1', '1(0+)1'] Algorithm Follow these steps to find the pattern: 1. Import the re ...
Read More