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
Articles by Vikram Chiluka
Page 20 of 22
How to remove an element from a list by index in Python?
In this article, we will show you how to remove an element from a list by index using Python. Here we see 4 methods to accomplish this task − Using the del keyword to remove an element from the list Using the pop() function to remove an element from the list Using slicing to remove an element from the list Using indices to remove multiple elements from the list Assume we have taken a list containing some elements. We will remove a specific item from a list by giving the index value using the above-specified methods. ...
Read MoreHow to get the last element of a list in Python?
In Python, getting the last element of a list is a common operation. There are several approaches to accomplish this task, each with different use cases and behaviors ? Using Negative Indexing Python supports negative indexing, where -1 refers to the last element, -2 to the second-to-last, and so on. This is the most common and pythonic way. Example The following program returns the last element using negative indexing ? # input list numbers = [5, 1, 6, 8, 3] # printing input list print("Input list:", numbers) # getting the last element ...
Read MoreHow to declare an attribute in Python without a value?
In Python, you sometimes need to declare attributes without assigning specific values. The conventional approach is to use None, which represents "no value" in Python. Variables in Python are just names that refer to objects. You cannot have a name that doesn't refer to anything − it must be bound to an object. When you need a placeholder for attributes that don't yet have meaningful values, None is the standard choice. Method 1: Direct Assignment with None You can declare class attributes by directly assigning them None values ? # Creating a class with attributes ...
Read MoreHow do nested functions work in Python?
In this article, we will explain nested/inner functions in Python and how they work with examples. Nested (or inner) functions are functions defined within other functions that allow us to directly access the variables and names defined in the enclosing function. Nested functions can be used to create closures and decorators, among other things. Defining an Inner/Nested Function Simply use the def keyword to initialize another function within a function to define a nested function. The following program demonstrates the inner function in Python − Example # creating an outer function def outerFunc(sample_text): ...
Read MoreHow to convert date and time with different timezones in Python?
In this article, we will show you how to convert date and time with different timezones in Python using the pytz library. Using astimezone() function Using datetime.now() function The easiest way in Python date and time to handle timezones is to use the pytz module. This library allows accurate and cross−platform timezone calculations and brings the Olson tz database into Python. Before you use it you'll need to install it using − pip install pytz Using astimezone() Function The astimezone() method converts a datetime object from one timezone to another. ...
Read MoreHow to compare time in different time zones in Python?
In this article, we will show you how to compare time in different timezones in Python using the below methods ? Comparing the given Timezone with the local TimeZone Comparing the Current Datetime of Two Timezones Comparing Two Times with different Timezone Method 1: Comparing Local Timezone with UTC This method compares a local timezone (CET) with UTC timezone to determine the time difference ? # importing datetime, pytz modules from datetime import datetime import pytz # Getting the local timezone localTimeZone = pytz.timezone('CET') # Getting the UTC timeZone utcTimeZone = ...
Read MoreHow to do date validation in Python?
In this article, we will show you how to do date validation in Python. Date validation ensures that date strings conform to expected formats and represent valid dates. Using datetime.strptime() Function The datetime.strptime() function parses a date string according to a specified format. It raises a ValueError if the date is invalid or doesn't match the format ? import datetime # Valid date string date_string = '2017-12-31' date_format = '%Y-%m-%d' try: # Parse the date string date_object = datetime.datetime.strptime(date_string, date_format) print(f"Valid date: ...
Read MoreHow to find only Monday\'s date with Python?
In this article, we will show you how to find only Monday's date using Python. We find the last Monday, next Monday, nth Monday's dates using different methods ? Using timedelta() function Using relativedelta() function to get Last Monday Using relativedelta() function to get next Monday Using relativedelta() function to get next nth Monday Using timedelta() function to get previous nth Monday Using timedelta() for Next Monday The timedelta() function from Python's datetime library calculates differences between dates and can manipulate dates. It provides parameters for days, seconds, microseconds, milliseconds, minutes, hours, and weeks. ...
Read MoreHow to sort a Python date string list?
Python provides several methods to sort a list of date strings. We'll explore three approaches: using sort() with lambda functions, using sort() with custom functions, and using sorted() function. Using sort() with Lambda Functions The datetime.strptime() function converts date strings into datetime objects for proper chronological sorting: # importing datetime from datetime import datetime # input list of date strings date_list = ['06-2014', '08-2020', '4-2003', '04-2005', '10-2002', '12-2021'] # sorting the input list by formatting each date using the strptime() function date_list.sort(key=lambda date: datetime.strptime(date, "%m-%Y")) # Printing the input list after sorting print("The ...
Read MoreHow to perform arithmetic operations on a date in Python?
Performing arithmetic operations on dates allows us to calculate differences between dates, add or subtract time intervals, or compare one date with another using the datetime module in Python. This article will discuss how to perform several arithmetic operations using the datetime module in Python. Adding and Subtracting Days Using timedelta In the Python datetime module, timedelta is a class that represents the difference or duration between two dates or times. We can use timedelta objects to perform date arithmetic, such as adding or subtracting a certain number of days, weeks, hours, minutes, etc. To add ...
Read More