Home » Programming » Python

Linked Lists in Python – How to Use Them, With Examples

Python Linked Lists

This article will explain in simple terms what linked lists are, why they’re useful, and how to use them in the Python programming language. Lists In Python, a list is an array of data – a simple list of values where each item has a value and a position in the list. Lists are indexed (so the positions start counting at position 0, not 1). For more information on lists in Python, check out these LinuxScrew articles: Python List ‘sort()’ Method – Sorting Lists in Python Easily … Read more

Home » Programming » Python

Converting a List to a String (and Back!) in Python [Examples]

Python List to String

This article will show you how to convert lists to a string in the Python programming language. Why would you want to convert a list to a string? Most commonly, because you want to print it or because you want to store it in a database text field or a text file. Converting List to String in Python The join() string method will take a list or iterable in Python and join each element, using the given string as a separator between each element. # Define a list of strings myList … Read more

Home » Programming » Python

Python Matrix (2D Array, NumPy), With Examples

Python Matrix

This article will explain what a matrix is and how to use them in the Python Programming Language with the NumPy library. What is a Matrix In mathematics and computer programming, a matrix is a two-dimensional array of values. It is a grid of columns and rows, with each position in the grid holding one of these values. It is an array because it’s a collection of elements with multiple elements, and it’s two-dimensional because the values exist at two coordinates (i.e., rows and columns). … Read more

Home » Programming » Python

How To Run a Python Script (Python 2/3)

How to Run Python Scripts

This article will show you how to run both Python 2 and Python 3 scripts in Linux and find out what versions of Python are installed. Running a Script To run a Python 3 script: python3 /path/to/script.py To run a Python 2 script: python2 /path/to/script.py Read on to find out how to install Python and find out which versions you have installed. Installing Python on Linux You can see what Python packages are installed on your system by running the following commands: which python which … Read more

Home » Programming » Python

Checking Whether a String Contains a String in Python, With Examples

Python String Contains

This article will show you how to check if a string contains another string in the Python programming language. PHP and Javascript both have similar functions included: PHP strpos Function: Check if a String Contains Another String Javascript String includes() Method – Check if a String Contains Another String Python has several methods for accomplishing the same thing – read on for how to do it. Using in to Check if A String Contains a String in Python The fastest and best way to check if a … Read more

Home » Programming » Python

Calculating Absolute Values in Python, With Examples

Python Absolute Value

In this article, you’ll learn about absolute values – what they are, how they’re used, and how to use the Python abs() function to calculate them. What is the Absolute Value of a Number? The absolute value of a number is that number’s value without any regard to its sign. A number’s sign is whether it is positive or negative. So, the absolute value of a number is never negative. To make it simple, consider the absolute value of a number that numbers distance from 0. It is also referred to in mathematics and … Read more

Home » Programming » Python

Command Line Arguments in Python Scripts, With Examples

Python Command Line Arguments

This article will show you simple methods to pass command line arguments to your Python scripts, with some examples. Passing arguments to your script makes it easy to build multi-use scripts that can work on different inputs. This is much easier than re-editing your code every time you want to change the inputs it will be working on. I’ve previously covered building a full command-line application in Python, which accepts command-line options and bundles the application into a distributable package with no external dependencies – … Read more

Home » Programming » Python

DIY Raspberry Pi/Python Powered PACHINKO [Kitchen Build]

Raspberry Pi Python Pachinko

Pachinko? Pichinko? Pychinko? I’ve been playing around with is idea for a while because… I don’t know; it seemed like a cool concept. That’s my whole motivation. Here’s a Raspberry Pi and Python-powered pachinko machine. This project is really basic, so it’s good for beginners, and the result is a lot of fun to mess with and would make a good desk ornament to fidget with or something for the kids to build on a rainy day to learn about coding and circuits and all … Read more

Home » Programming » Python

isdigit(), isalpha() and Other Checks in Python, With Examples

Python isdigit isalpha

Both Python versions 2 and 3 provide several helpful string methods that let you find the string’s contents.  Here’s how to use them. The functions in this article are useful if you want to validate user input – for example, you may want to check that a user-inputted string is a valid number before converting it to a number variable to prevent errors and warn the user that they need to try again. isdigit() and isalpha() are commonly used methods to identify a string’s contents, but … Read more

Home » Programming » Python

How to Round Numbers Up/Down/Nearest in Python

Python Round Numbers

This article will show you how to round numbers up, down, and to the nearest 1, 10 100 (or any number) in the Python programming language. Truncation vs. Rounding Truncation isn’t technically rounding, but it serves a similar purpose, so I’ll include it here. It’s the simplest way to shorten a number – just cutting off the characters at the end until it’s in the format you require. Rounding gets the nearest number, whereas truncation cuts off digits from the number. For example: To round 2.9 to … Read more