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 24 of 855
max() and min() in Python
Finding maximum and minimum values from a given list of values is a very common need in data processing programs. Python provides built-in max() and min() functions that handle both numbers and strings efficiently. Syntax max(iterable, *[, key, default]) min(iterable, *[, key, default]) Using max() and min() with Numeric Values Both functions work with integers, floats, and mixed numeric types to find the maximum and minimum values ? numbers = [10, 15, 25.5, 3, 2, 9/5, 40, 70] print("Maximum number is:", max(numbers)) print("Minimum number is:", min(numbers)) Maximum number is: ...
Read MoreGetter and Setter in Python
In Python, getters and setters are methods used to control access to an object's attributes. They provide a way to implement data encapsulation, ensuring that attributes are accessed and modified in a controlled manner. Getters retrieve the value of an attribute, while setters allow you to modify it. This approach helps prevent direct manipulation of attributes from outside the class. Basic Class with Public Attributes Let's start with a simple class that has a public attribute − class YearGraduated: def __init__(self, year=0): self.year ...
Read MoreFractal Trees in Python
Fractal patterns are everywhere in nature − a small branch resembles the entire tree, a fern leaf's part looks like the whole leaf. This self-repeating pattern concept is called a fractal tree. Python provides several modules to generate beautiful fractal trees programmatically. What is a Fractal Tree? A fractal tree is a mathematical structure that exhibits self-similarity at different scales. Each branch splits into smaller branches following the same pattern, creating a recursive tree-like structure. The recursion continues until reaching a specified depth. Using pygame Module The pygame module provides graphics functions to draw fractal trees. ...
Read MoreAppend Odd element twice in Python
Sometimes we need to process a list where odd numbers appear twice while even numbers remain unchanged. This is useful in data processing scenarios where odd values need special emphasis or duplication. Using List Comprehension The most concise approach uses list comprehension with conditional repetition ? numbers = [2, 11, 5, 24, 5] # Repeat odd numbers twice, keep even numbers once result = [value for num in numbers for value in ([num] if num % 2 == 0 else [num, num])] print("Original list:", numbers) print("After processing:", result) Original list: [2, ...
Read MoreAppend at front and remove from rear in Python
When working with Python lists, we often need to add elements to the front and remove elements from the rear. Python provides several efficient methods to accomplish this task, both from the standard library and through built-in operators. Using + Operator with List Slicing The simplest approach combines list concatenation with slicing to add an element at the front while removing the last element ? days = ['Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] print("Original list:", days) # Add 'Mon' at front and remove last element result = ['Mon'] + days[:-1] print("After append front and remove ...
Read MoreAdding a Chartsheet in an excel sheet using Python XlsxWriter module
The XlsxWriter module in Python is a powerful external library that allows you to create Excel files with data, formatting, and charts. Unlike Python's built-in libraries, XlsxWriter specializes in generating Excel files with advanced features like charts, images, and complex formatting. What is a Chartsheet? A chartsheet is a separate worksheet in Excel that contains only a chart, without any data cells. This is different from inserting a chart into a regular worksheet − the entire sheet becomes the chart. Creating a Pie Chart in a Chartsheet Let's create a pie chart that displays data about ...
Read MoreAdd the element in a Python list with help of indexing
A Python list is a collection data type that is ordered and changeable. It allows duplicate members and is the most frequently used collection data type in Python programs. We can add elements to a list at specific positions using indexing and the insert() method. Before adding elements, let's first understand how to access list elements using indexing. Accessing List Elements Using Index Every element in a list is associated with an index, which keeps the elements ordered. We can access elements by their index positions using slicing or direct indexing ? vowels_list = ['a', ...
Read MoreAdd similar value multiple times in a Python list
There are occasions when we need to add the same value multiple times to a Python list. This is useful for initializing lists, creating test data, or padding sequences. Python provides several built-in methods to achieve this efficiently. Using the * Operator This is the most straightforward method. The * operator creates a new list by repeating elements a specified number of times. Example Creating a list with repeated strings ? given_value = 'Hello! ' repeated_list = [given_value] * 5 print(repeated_list) The output of the above code is ? ['Hello! ...
Read MoreEvent scheduler in Python
Python provides the schedule module for running tasks at specific times. This module offers a simple and intuitive way to create scheduled jobs using the every() function with various time intervals. Installation First, install the schedule module ? pip install schedule Syntax schedule.every(n).[timeframe] Where: n is the time interval (integer) timeframe can be seconds, minutes, hours, days, or weekday names (monday, tuesday, etc.) Basic Example Here's a simple example that prints messages at different intervals ? import schedule import time def job(): ...
Read MoreAdding value to sublists in Python
Sometimes we need to add new values to sublists in an existing list. In this article, we will see how new values can be inserted into sublists by combining them with each item of the existing list. Using List Comprehension with Fixed-Length Sublists When you have sublists of the same length, you can use list comprehension to unpack and add new values ? data = [[10, 20], [14, 8], ['Mon', 'Tue']] print("Given List:") print(data) s = "Rise" t = "fast" result = [[m, n, s, t] for m, n in data] print("New List:") print(result) ...
Read More