Programming Articles

Page 181 of 2547

How do you remove multiple items from a list in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 4K+ Views

Removing multiple items from a list in Python can be accomplished using several approaches. Each method has its own advantages depending on whether you're removing by value, index, or condition. Using del with Slice Notation The del keyword with slice notation removes consecutive items by index range − # Creating a List names = ["David", "Jacob", "Harry", "Mark", "Anthony", "Steve", "Chris"] # Displaying the List print("List =", names) # Remove multiple items from a list using del keyword del names[2:5] # Display the updated list print("Updated List =", names) List ...

Read More

How do you remove duplicates from a list in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 609 Views

Removing duplicates from a list is a common task in Python. There are several efficient approaches: using set(), OrderedDict, and list comprehension. Each method has different characteristics regarding order preservation and performance. Using set() Method The simplest approach converts the list to a set, which automatically removes duplicates. However, this doesn't preserve the original order ? # Creating a List with duplicate items names = ["Jacob", "Harry", "Mark", "Anthony", "Harry", "Anthony"] # Displaying the original List print("Original List =", names) # Remove duplicates using set unique_names = list(set(names)) print("Updated List =", unique_names) ...

Read More

How do I iterate over a sequence in reverse order in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 719 Views

Python provides several effective methods to iterate over sequences in reverse order. Whether you're working with lists, tuples, or strings, you can choose from multiple approaches depending on your specific needs and coding style. Using While Loop The while loop approach manually controls the iteration by starting from the last index and decrementing until reaching the first element ? # Creating a List names = ["Jacob", "Harry", "Mark", "Anthony"] # Displaying the List print("List =", names) # Length - 1 i = len(names) - 1 # Iterate in reverse order print("Display the List ...

Read More

What is a Negative Indexing in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 41K+ Views

Negative indexing in Python allows you to access elements from the end of a sequence (string, list, tuple) by using negative numbers. Instead of counting from the beginning (0, 1, 2...), negative indexing counts backwards from the last element (-1, -2, -3...). Understanding Negative Index Positions In a string like "Python", the negative indices work as follows ? Negative Indexing in Python P ...

Read More

How do I convert between tuples and lists in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 398 Views

Python provides simple built-in functions to convert between tuples and lists. Use list() to convert a tuple to a list, and tuple() to convert a list to a tuple. Converting Tuple to List To convert a tuple to a list, use the list() function with the tuple as a parameter. Example with Integer Elements # Creating a Tuple mytuple = (20, 40, 60, 80, 100) # Displaying the Tuple print("Tuple =", mytuple) print("Tuple Length =", len(mytuple)) # Tuple to list mylist = list(mytuple) # Display the list print("List =", mylist) print("Type =", ...

Read More

Functional Programming in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

Functional programming is a programming paradigm based on mathematical functions, using expressions and recursion to perform computations. Python supports functional programming concepts alongside its object-oriented features, making it a multi-paradigm language. Key Characteristics of Functional Programming The most prominent characteristics of functional programming are as follows: Based on mathematical functions using conditional expressions and recursion Supports higher-order functions and lazy evaluation Emphasizes immutability and pure functions (no side effects) Functions are first-class objects that can be assigned, passed, and returned Advantages of Functional Programming Modularity Functional programming forces you to break problems ...

Read More

What does [::-1] do in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 23K+ Views

Slicing in Python allows you to extract portions of sequences like strings, lists, and other data structures. The slice notation uses the format [start:stop:step], where [::-1] is a special case that reverses the entire sequence. Understanding [::-1] Syntax The slice notation [::-1] means: start: empty (defaults to the end) stop: empty (defaults to the beginning) step: -1 (move backwards one element at a time) Here's how different slicing patterns work ? text = "Hello" # Different slicing patterns print("Original:", text) print("First 3 chars:", text[:3]) print("From index 1 to end:", text[1:]) print("Every ...

Read More

What type of language is python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 5K+ Views

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Let's understand each paradigm that defines Python's characteristics. Interpreted Language Python is processed at runtime by the interpreter. You do not need to compile your program before executing it, similar to PERL and PHP. Python Execution Process Python follows a three-step execution process ? Python Source Code (.py) Bytecode (.pyc) Python Virtual Machine (PVM) ...

Read More

How to create an empty class in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 6K+ Views

A class in Python is a user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation. We can easily create an empty class in Python using the pass statement. This statement in Python does nothing and acts as a placeholder ? Basic Empty Class Syntax Here's how to create a simple empty class ? class Student: pass print("Empty class created successfully!") Empty class ...

Read More

How to combine dataframes in Pandas?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 880 Views

Pandas provides several methods to combine DataFrames efficiently. The three most common approaches are concat() with inner join for column-wise combination, concat() for vertical stacking, and merge() for database-style joins. Using concat() with Inner Join The concat() function with join='inner' combines DataFrames side by side, keeping only matching indices ? import pandas as pd # Create sample DataFrames df1_data = {'Player': ['Jacob', 'Steve', 'David', 'John', 'Kane'], 'Age': [29, 25, 31, 26, 27]} df2_data = {'Rank': [1, 2, 3, 4, 5], ...

Read More
Showing 1801–1810 of 25,469 articles
« Prev 1 179 180 181 182 183 2547 Next »
Advertisements