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 157 of 855
Python Program to create a String object
In Python, we can create a string object using Python's built-in function str() and also by assigning a sequence of characters to a variable. The sequence of characters is enclosed in single quotes, double quotes, or triple quotes for multiline strings. In this article, we look at the various ways to create string objects in Python. Using Single Quotes We can create a string object by simply assigning a sequence of characters enclosed in single quotes to a variable − my_string = 'Hello World!' print(my_string) print(type(my_string)) The output of the above code is − ...
Read MorePython Program to compare two strings by ignoring case
In Python, we can compare two strings while ignoring their case using several approaches. Strings are character sequences that can contain uppercase and lowercase letters. When comparing strings case-insensitively, we need to normalize both strings to the same case before comparison. Using lower() Method The most common approach is converting both strings to lowercase using the lower() method before comparison ? string1 = "Hello" string2 = "hello" if string1.lower() == string2.lower(): print("The strings are equal, ignoring case.") else: print("The strings are not equal, ignoring case.") ...
Read MorePython Program to Clear the String Buffer
In Python, a string buffer is a mutable sequence of characters that can be modified before writing to an output stream. Python's StringIO class from the io module provides an in-memory buffer for string operations. We can clear this buffer using different methods depending on our requirements. Method 1: Using truncate() and seek() The truncate(0) method removes all content from the buffer starting at position 0, while seek(0) resets the cursor to the beginning ? from io import StringIO # Create a string buffer buffer = StringIO() # Add some text to the buffer ...
Read MorePython Program to Check if a string is a valid shuffle of two distinct strings
In Python, we can check if a string is a valid shuffle of two distinct strings by comparing the sorted characters. A valid shuffle means that a string is formed by mixing characters from two distinct strings while maintaining the relative order of characters from each original string. What is a Valid Shuffle? A valid shuffle combines characters from two strings without adding, removing, or changing any characters. Let's see some examples ? S1: "abc" S2: "def" Valid shuffles: "adbecf", "dabecf", "abdefc" Invalid shuffles: "abgfcd" (contains 'g'), "tabcde" (contains 't') ...
Read MoreDifference between \'and\' and \'&\' in Python
In Python 'and' and '&' both are used to perform logical operations, but they work differently. The and operator performs logical AND operations, while the & operator performs bitwise AND operations. Understanding their differences is crucial for writing correct Python code. Key Differences Feature and operator & operator Purpose Logical operations Bitwise operations Return Type Boolean or operand value Integer value Evaluation Short-circuit evaluation Evaluates all operands Operation Level Works on truthiness Works on binary representation The 'and' Operator The and operator performs logical ...
Read MoreDifference Between ‘+’ and ‘append’ in Python with examples
In Python, the + operator is used to concatenate two lists or strings together and return a new object, whereas the append() method is used to add elements to the end of an existing list. The + acts as an operator whereas append() is a method. In this article, we will understand the differences between the + operator and the append() method in Python. Key Differences Aspect + operator append() method ...
Read MoreDifference between \'__eq__\' VS \'is\' VS \'==\' in Python
In Python, object comparison can be performed using three different approaches: the __eq__ method, the is operator, and the == operator. Each serves a distinct purpose in determining equality or identity between objects. Overview of Comparison Methods Method Purpose Checks Usage __eq__ Custom equality logic Object values (customizable) Class method definition is Identity comparison Memory location a is b == Value comparison Object values a == b The __eq__() Method The __eq__ method allows you to define custom equality logic for your classes. When you use ...
Read MoreDifference between != and is not operator in Python
The != operator checks if the values of two objects are different, while the is not operator checks if two objects are not the same object in memory (different identity). Understanding this difference is crucial for proper Python comparisons. Key Differences != operator is not operator Compares values of objects Compares object identity (memory location) Returns True if values are different Returns True if objects have different identities Syntax: object1 != object2 Syntax: object1 is not object2 Example with Different Data Types Let's compare integers, strings, ...
Read MoreActive product sales analysis using matplotlib in Python
Matplotlib in Python provides powerful tools for analyzing product sales data. Every online business uses sales data analysis to increase revenue and understand customer behavior better. Companies involved in e-commerce use sales and customer data to identify trends, patterns, and insights that improve sales performance. Python is a popular programming language for data analysis and visualization. In this article, we will use Matplotlib, Pandas, and NumPy to perform active product sales analysis using sample sales data. Sample Sales Data Structure The sample sales data contains the following columns ? Column Description Order_Number ...
Read MoreActivation Functions in Pytorch
PyTorch is an open-source machine learning framework that provides various activation functions for building neural networks. An activation function determines the output of a node in a neural network given an input, introducing non-linearity which is essential for solving complex machine learning problems. What is an Activation Function? Neural networks consist of input layers, hidden layers, and output layers. The activation function is applied to the weighted sum of inputs at each node, transforming the linear combination into a non-linear output. This non-linearity enables neural networks to learn complex patterns and relationships in data. ...
Read More