Posts

Python String isascii() Method

The isascii() method is a string method that returns True if all the characters in the string are ASCII characters (a-z). ASCII stands for American Standard Code for Information Interchange. It is a character encoding standard that uses numbers from 0 to 127 to represent English characters and some symbols. For example: name = "Alice" print(name.isascii()) # prints True The method returns False if the string contains any non-ASCII characters, such as accented letters, emojis, etc. For example: name = "Alïce" print(name.isascii()) # prints False If you have any questions about this code, you can drop a line in comment .  

Python filter() Built in Function

The filter() function in Python is used to filter out elements from an iterable (such as a list, tuple, or string) that satisfy a given condition. The filter() function takes two arguments: a function that returns True or False for each element, and an iterable to apply the function to. The filter() function returns a new iterable with only the elements that return True for the function. Here are some examples of using filter() in Python: Filter a list of numbers to only include even numbers: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers) Output: [2, 4, 6, 8, 10] In this example, we use filter() to create a new list called even_numbers that only includes the even numbers from the original list numbers. The lambda function inside the filter() function takes a single argument x and returns True if x is even (x % 2 == 0), and False otherwise. The list() function is used to convert the resulting filter object...

Python Flight Price Prediction

Flight price prediction is a challenging task that involves analyzing various factors that affect the ticket prices, such as date, time, destination, airline, seasonality, demand and supply. You can use Python to build a machine learning model that predicts the flight prices based on historical data and features. Here are some examples of flight price prediction using Python: Using Kaggle dataset of flight booking data obtained from “Ease My Trip” website to perform exploratory data analysis and build a regression model using scikit-learn library. # Import libraries import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.linear_model import LinearRegression # Read the dataset data = pd.read_csv("flight_price_prediction.csv") # Explore the dataset data.head() # Plot a boxplot of price vs airline sns.boxplot(x="Airline", y="Price", data=data) plt.xticks(rotation=90) plt.show() # Train a linear regression m...

Python String isalnum() Method

The isalnum() method is a string method that returns True if all the characters in the string are alphanumeric, meaning letters (a-z) and numbers (0-9). For example: name = "Alice123" print(name.isalnum()) # prints True The method returns False if the string contains any non-alphanumeric characters, such as spaces, punctuation marks, symbols, etc. For example: name = "Alice 123" print(name.isalnum()) # prints False If you have any questions about this code, you can drop a line in comment .

Python String index() Method

The index() method is a list method that returns the position of the first occurrence of a given element in the list. For example: fruits = ["apple", "banana", "cherry", "orange"] print(fruits.index("cherry")) # prints 2 You can also specify a start and end index to limit the search range. For example: numbers = [1, 2, 3, 4, 5, 6, 7, 8] print(numbers.index(5, 3)) # prints 4 print(numbers.index(5, 3, 6)) # prints 4 If the element is not found in the list, a ValueError exception is raised. For example: animals = ["cat", "dog", "bird", "fish"] print(animals.index("lion")) # raises ValueError: 'lion' is not in list If you have any questions about this code, you can drop a line in comment .