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 Lakshmi Srinivas
233 articles
How to create a lambda inside a Python loop?
Creating lambda functions inside Python loops requires careful handling to avoid common pitfalls. The key issue is that lambdas created in loops can capture variables by reference, leading to unexpected behavior. Using a Helper Function The most straightforward approach is to use a helper function that returns a lambda ? def square(x): return lambda: x*x list_of_lambdas = [square(i) for i in [1, 2, 3, 4, 5]] for f in list_of_lambdas: print(f()) 1 4 9 16 25 Using Default Parameters ...
Read MoreHow to overload python ternary operator?
The ternary operator in Python (value_if_true if condition else value_if_false) cannot be directly overloaded like other operators. However, you can create reusable ternary-like functionality using lambdas, functions, or custom classes. Using Lambda Functions You can wrap ternary logic in a lambda function to make it reusable ? # Create a reusable ternary function result = lambda x: 1 if x < 3 else 10 print(result(2)) print(result(1000)) 1 10 Using Regular Functions For more complex logic, regular functions provide better readability ? def categorize_number(x): ...
Read MoreHow to put comments inside a Python dictionary?
Python dictionaries support single-line comments using #, but multiline comments require special handling. You can add comments to explain dictionary keys, values, or sections within your data structure. Single-Line Comments in Dictionaries You can add single-line comments anywhere inside a dictionary using the # symbol ? test_items = { 'TestOne': 'Hello', # Active item # 'TestTwo': None, # Commented out item 'TestThree': 'World', # ...
Read MoreHow to sum values of a Python dictionary?
It is pretty easy to get the sum of values of a Python dictionary. You can first get the values in a list using the dict.values(). Then you can call the sum method to get the sum of these values. Example Here's how to sum dictionary values using the sum() function ? d = { 'foo': 10, 'bar': 20, 'baz': 30 } print(sum(d.values())) This will give the output ? 60 Using List Comprehension You can also sum ...
Read MoreHow do we compare two tuples in Python?
Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the comparison, else the second item is considered, then the third and so on. Lexicographic Comparison Python compares tuples element by element from left to right using lexicographic ordering ? a = (1, 2, 3) b = (1, 2, 5) print(a < b) print(a == b) print(a > b) True False False In this example, the first two ...
Read MoreHow to use date command in day to day practical usage
In this article, we will learn about the date command in Linux and how to practically use it in day-to-day usage with some practical examples. The date command is used to print or change the system date and time, making it an essential tool for system administrators and users. General Syntax [root@localhost ~]# date [OPTION]... [+FORMAT] [root@localhost ~]# date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] Key Features of the Date Command Print current date and time on the system Display date and time in custom formats Read dates from files Display Universal Coordinated Time (UTC) Set system ...
Read MoreLinux nslookup commands to troubleshoot dns domain name server
nslookup is a network administration command-line tool available for many computer operating systems for querying the Domain Name System (DNS) to obtain domain name or IP address mapping or for any other specific DNS record. This article explains the nslookup command in detail for DNS troubleshooting. Nslookup can be run in two modes: Interactive and Non-Interactive. Interactive mode is used to query DNS servers about various domains and hosts interactively, while Non-Interactive mode is used to query information about a specific domain or host with a single command. Basic DNS Queries Finding A Record (IP Address) ...
Read MoreHow to find a list of block devices information
The lsblk command is used to display a list of information about all available block devices in Linux systems. However, it does not list information about RAM disks by default. Examples of block devices include hard disks, flash drives, and CD-ROM drives. This article explains how to find and display block device information on Linux machines. Installing lsblk For Fedora and CentOS systems, use the following command − $ sudo yum install util-linux-ng For Ubuntu and Linux Mint systems, use the following command − $ sudo apt-get install util-linux -y ...
Read MoreUpgrade your computer for free to windows 10
People who have been using Windows operating systems typically encounter persistent messages to upgrade to Windows 10. The good news is that upgrading to Windows 10 was initially offered as a free upgrade for eligible users. However, this free upgrade period had specific time limitations that users needed to be aware of. Microsoft took a different approach compared to Apple, which allows its users to upgrade to the newest operating system for free at any point. Windows users who missed the free upgrade window would need to purchase a license, which could cost around $119 or more depending on ...
Read MoreHow to build a wireless home network without a router
Setting up a wireless internet connection without a router is usually done using an ad-hoc network. This is a peer-to-peer network configuration where devices connect directly to each other without requiring a central access point. Small businesses and home users often establish ad-hoc computer-to-computer networks when they don't have access to a traditional router. What is an Ad-hoc Network An ad-hoc network allows devices with wireless capabilities to connect directly to each other, creating a temporary network without infrastructure. If you have Windows Vista, Windows 7, Windows XP, or newer systems, you can build your own wireless network ...
Read More