Programming 

Python List reverse()

This tutorial shows you everything you need to know to help you master the essential reverse() method of the most fundamental container data type in the Python programming language. Definition and Usage: The list.reverse() reverses the order of the elements in the list. If you want to create a new list with reversed elements, use slicing with negative step size list[::-1]. Here’s a short example: >>> lst = [1, 2, 3, 4] >>> lst.reverse() >>> lst [4, 3, 2, 1] In the first line of the example, you create the…

Read More
Programming 

Python List pop()

This tutorial shows you everything you need to know to help you master the essential pop() method of the most fundamental container data type in the Python programming language. Definition and Usage: The list.pop() method removes and returns the last element from an existing list. The list.pop(index) method with the optional argument index removes and returns the element at the position index. Here’s a short example: >>> lst = [1, 2, 3] >>> lst.pop() 3 >>> lst [1, 2] In the first line of the example, you create the list…

Read More

Real Python: Python Scope & the LEGB Rule: Resolving Names in Your Code

The concept of scope rules how variables and names are looked up in your code. It determines the visibility of a variable within the code. The scope of a name or variable depends on the place in your code where you create that variable. The Python scope concept is generally presented using a rule known as the LEGB rule. The letters in the acronym LEGB stand for Local, Enclosing, Global, and Built-in scopes. This summarizes not only the Python scope levels but also the sequence of steps that Python follows…

Read More
java 

How to Do a Binary Search in Python

Binary search is a classic algorithm in computer science. It often comes up in programming contests and technical interviews. Implementing binary search turns out to be a challenging task, even when you understand the concept. Unless you’re curious or have a specific assignment, you should always leverage existing libraries to do a binary search in Python or any other language. In this tutorial, you’ll learn how to: Use the bisect module to do a binary search in Python Implement a binary search in Python both recursively and iteratively Recognize and…

Read More

Python List remove()

How can you insert an element at a given index in a given list? Python’s remove() method is your friend. This tutorial shows you everything you need to know to help you master an essential method of the most fundamental container data type in the Python programming language. Definition and Usage: The list.remove(element) method removes the first occurrence of the element from an existing list. It does not, however, remove all occurrences of the element in the list! Here’s a short example: >>> lst = [1, 2, 99, 4, 99]…

Read More
Programming 

Python List insert() Method

How can you insert an element at a given index in a given list? Python’s insert() method is your friend. This tutorial shows you everything you need to know to help you master an essential method of the most fundamental container data type in the Python programming language. Definition and Usage: The list.insert(i, element) method adds an element element to an existing list at position i. All elements j>i will be moved by one index position to the right. Here’s a short example: >>> lst = [1, 2, 3, 4]…

Read More

Reuven Lerner: Announcing: Free, weekly “Python for non-programmers” workshop

This is a tough time for the world. Wherever you live, you have likely been affected by covid-19, the coronavirus that has been making its way to every country, city, and town. Many countries, companies, and individuals are now restricted to their homes.  This can be frustrating in many ways.  Moreover, I’m not alone in believing that we’re about to see some very troubled times for the world economy. I’ve been trying to decide what I can do, as a Python instructor, to help people in these trying times.  And…

Read More

Ned Batchelder: Functional strategies in Python

I got into a debate about Python’s support for functional programming (FP) with a friend. One of the challenging parts was listening to him say, “Python is broken” a number of times. Python is not broken. It’s just not a great language for writing pure functional programs. Python seemed broken to my friend in exactly the same way that a hammer seems broken to someone trying to turn a screw with it. I understand his frustration. Once you have fully embraced the FP mindset, it is difficult to understand why…

Read More
Programming 

Python List extend() Method

How can you not one but multiple elements to a given list? Use the extend() method in Python. This tutorial shows you everything you need to know to help you master an essential method of the most fundamental container data type in the Python programming language. Definition and Usage The list.extend(iter) method adds all elements in the argument iterable iter to an existing list. Here’s a short example: >>> lst = [1, 2, 3] >>> lst.extend([4, 5, 6]) >>> lst [1, 2, 3, 4, 5, 6] In the first line…

Read More

Real Python: Using the Python defaultdict Type for Handling Missing Keys

A common problem that you can face when working with Python dictionaries is to try to access or modify keys that don’t exist in the dictionary. This will raise a KeyError and break up your code execution. To handle these kinds of situations, the standard library provides the Python defaultdict type, a dictionary-like class that’s available for you in collections. The Python defaultdict type behaves almost exactly like a regular Python dictionary, but if you try to access or modify a missing key, then defaultdict will automatically create the key…

Read More