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
Server Side Programming Articles
Page 874 of 2109
floor() and ceil() function Python
Python's math module provides two useful functions for rounding decimal numbers to integers: floor() and ceil(). These functions help convert fractional numbers to their nearest integer values in different directions. floor() Function The floor() function returns the largest integer that is less than or equal to the given number. For positive numbers, it rounds down; for negative numbers, it rounds away from zero. Syntax math.floor(x) Where x is a numeric value Example of floor() Let's see how floor() works with different types of numeric values ? import math x, ...
Read MoreFinally keyword in Python
The finally keyword in Python defines a block of code that executes regardless of whether an exception occurs or not. This makes it useful for cleanup operations like closing files, database connections, or releasing resources. Syntax try: # Main Python code except ExceptionType: # Optional block to handle specific exceptions finally: # Code that always executes Example with Exception Handling Here's an example where a NameError occurs when trying to print an undefined variable ? try: ...
Read Moredivmod() in Python and its application
The divmod() function is part of Python's standard library which takes two numbers as parameters and returns the quotient and remainder of their division as a tuple. It is useful in many mathematical applications like checking for divisibility of numbers and establishing if a number is prime or not. Syntax divmod(a, b) Parameters: a − The dividend (number to be divided) b − The divisor (number that divides a) Both a and b can be integers or floats Return Value: Returns a tuple (quotient, remainder) Examples with Integers and Floats ...
Read Morecasefold() string in Python
The casefold() method in Python converts a string to lowercase, making it ideal for case-insensitive string comparisons. Unlike lower(), casefold() handles special Unicode characters more aggressively, making it the preferred choice for string matching. Syntax string.casefold() This method takes no parameters and returns a new string with all characters converted to lowercase. Basic Usage Here's how to convert a string to lowercase using casefold() − string = "BestTutorials" # print lowercase string print("Lowercase string:", string.casefold()) Lowercase string: besttutorials Case-Insensitive String Comparison You can compare two ...
Read MoreCapitalize first letter of a column in Pandas dataframe
A Pandas DataFrame is similar to a table with rows and columns. Sometimes we need to capitalize the first letter of strings in a specific column, which can be achieved using the str.capitalize() method. Creating a DataFrame Let's start by creating a DataFrame with columns for days and subjects ? import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Day': ['mon', 'tue', 'wed', 'thu', 'fri'], 'Subject': ['math', 'english', 'science', 'music', 'games'] }) print(df) Day ...
Read MoreXMLRPC server and client modules in Python
XML-RPC (XML Remote Procedure Call) is a protocol that allows programs to make function calls over HTTP using XML for encoding. Python's xmlrpc.server and xmlrpc.client modules make it easy to create cross-platform, language-independent servers and clients. Creating an XML-RPC Server We use SimpleXMLRPCServer to create a server instance and register functions that clients can call remotely. The server listens for incoming requests and executes the appropriate functions ? Example from xmlrpc.server import SimpleXMLRPCServer from xmlrpc.server import SimpleXMLRPCRequestHandler class RequestHandler(SimpleXMLRPCRequestHandler): rpc_paths = ('/RPC2', ) with SimpleXMLRPCServer(('localhost', 9000), ...
Read MorePython Code Objects
Code objects are a low-level detail of the CPython implementation. Each one represents a chunk of executable code that hasn't yet been bound into a function. Though code objects represent some piece of executable code, they are not, by themselves, directly callable. To execute a code object, you must use the exec() function. Code objects are created using the compile() function and contain various attributes that provide information about the compiled code, such as bytecode, variable names, and filename. Creating a Code Object Use the compile() function to create a code object from a string of Python ...
Read MorePython a += b is not always a = a + b
In Python, the expressions a += b and a = a + b are not always equivalent, especially when dealing with mutable objects like lists. The key difference lies in how Python handles these operations internally. Understanding the Difference For immutable types like integers and strings, both expressions produce the same result. However, for mutable types like lists, the behavior differs significantly: a = a + b creates a new object a += b modifies the existing object in-place (when possible) Case of a = a + b When using a = a ...
Read MorePython - Create Test DataSets using Sklearn
The Sklearn Python library provides sample datasets which can be used to create various graph plots. The usefulness of these datasets is in creating sample graphs and charts, predicting graph behavior as values change, and experimenting with parameters like colors and axes before using actual datasets. Using make_blobs The make_blobs function generates isotropic Gaussian blobs for clustering. This is useful for testing clustering algorithms and creating scatter plots with distinct groups of data points. Example In the below example we use the sklearn library along with matplotlib to create a scatter plot with a specific style. ...
Read MoreFind maximum length sub-list in a nested list in Python
Finding the maximum length sub-list in a nested list is a common task when working with data structures in Python. This article explores different approaches to identify the longest sub-list and return both the sub-list and its length. Using lambda and max() The most efficient approach combines lambda with the max() function. The max() function uses a key parameter to determine which sub-list has the maximum length ? Example def longest(nested_list): longest_list = max(nested_list, key=lambda i: len(i)) max_length = max(map(len, nested_list)) return longest_list, ...
Read More