Home » Programming » Python

How to use the Python sum() Function to Add/Sum Numbers

Python sum

The Python sum() function takes a list of numbers, and sums them up (totals/adds them all together). Here’s how to use it, with examples. Summing/Totalling/Adding Numbers The concept of summing numbers is simple – take the numbers you wish to get the total of, and add them all together. 1 + 2 + 3 = 6 It’s basic arithmetic that you’ll use daily when writing computer software in any language. Summing Numbers in Python The sum() function in Python sums numbers. It’s a built-in function, which can be used anywhere … Read more

Home » Programming » Python

How to Iterate/Loop over a Dictionary in Python [Examples]

Python iterate loop dictionary

This article will show you how to iterate/loop over the keys and values in a Python dictionary, and provide code examples. What is a Python Dictionary? In Python, a dictionary is a data type that contains a collection of objects – but unlike a list or array, rather than their values being recorded at a specific index (or ordered position in the sequence of stored values), they are stored in a key – a string identifier which both tells you what a value is for, and which … Read more

Home » Programming » Python

Python hasattr() – What it Does and How to Use It [Examples]

Python use hasattr() to check object properties

The hasattr() function is used to check whether an object has a given named attribute. Here is how it is used, with examples. Python Objects and Classes Python is an object oriented programming language. Object oriented means that rather than code being written as a list of instructions, accumulating data and passing the results to the following lines of code (known as procedural programming), data and functionality is contained within the objects it describes. Each object is a self-contained representation of something, that can be modified, and passed between functions … Read more

Home » Programming » Python

How to Generate & Write CSV Files in Python, With Examples

Python write csv file

This article will show you how to generate CSV (Comma Separated Values) data, and write it to a file, with examples. What is CSV (Comma Separated Values) CSV (Comma Separated Values) is a format for storing and transmitting data, usually in a text file, in which the individual values are separated by a comma (,). Each row in a CSV file represents an individual record containing multiple comma separated values. A CSV file looks like this: name,age,favourite colour Fred,54,green Mary,31,pink Steve,12,orange Above, the CSV describes three people, with … Read more

Home » Programming » Python

Python – Concatenating Iterators With itertools.chain() [Examples]

Concatenate:join iterables using chain in Python

This tutorial will show you how to join/concatenate/merge iterables in the Python programming language, and provide code examples. What is an ‘Iterable’ in Python? An iterable is any kind of Python object that can return each of its members one at a time. Put simply, it’s any Python object that contains multiple values that can be looped over. Python’s built-in Iterables include lists, strings, and tuples — all object types which can be represented as a sequence of values. Iterating over Iterables To demonstrate this behaviour, we can create a list of strings, … Read more

Home » Programming » Python

Python getattr() – What it Does and How to Use It [Examples]

Python use getattr() to get object attributes

The getattr() function is used to get the value of an attribute of an object. Here is how it is used, with examples. Python Objects and Classes Python is an object oriented programming language. Object oriented means that rather than code being written as a list of instructions, accumulating data and passing the results to the following lines of code (known as procedural programming), data and functionality is contained within the objects it describes. Each object is a self-contained representation of something, that can be modified, and passed between functions … Read more

Home » Programming » Python

How to Reverse a String in Python, With Code Examples

Python reverse string

This quick tutorial will show you how to reverse a string in Python, and provide example code. Strings in Python Strings in Python are defined using quotes or double quotes, and assigned to variables: string1 = “This is a string” string2 = ‘This is also a string’ Strings are used to store non-numeric values – things like names, words, sentences, and serialized data. There’s a lot you can do with Python strings. Strings are immutable — meaning that once defined, they cannot be changed. However, the variable holding … Read more

Home » Programming » Python

Simple Guide to Python Multiprocessing/Threading [Examples]

Python multiprocessing

This article will provide a simple introduction to multiprocessing in Python and provide code examples. The code and examples in this article are for Python 3. Python 2 is no longer supported, if you are still using it you should be migrating your projects to the latest version of Python to ensure security and compatibility. What is Multiprocessing? Usually, Python runs your code, line-by-line, in a single process until it has completed. Multiprocessing allows you to run multiple sets of instructions simultaneously in separate processes, enabling your … Read more

Home » Programming » Python

Running External Programs in Python with subprocess [Examples]

Python subprocess

This article will show you how to use the Python subprocess module to run external programs and scripts from Python. It is often necessary to call external applications from Python. Usually these are command-line applications which you can use to perform tasks outside of the Python environment, like manipulating files, or interacting with third party services. For example, you might call the wget command to retrieve a remote file. Any application which is accessible on the command line is available to subprocess, and can be executed from within Python. You … Read more

Home » Programming » Python

How to Generate the Fibonacci Sequence of Numbers in Python

Python fibonacci sequence

This quick tutorial will show you how to generate the Fibonacci sequence of numbers in Python. This is a useful example for new coders and those just learning Python as it shows how variables, loops, and arithmetic are used in Python. What is the Fibonacci Sequence The Fibonacci sequence is a procession of numbers in which each number is the sum of the preceding two numbers. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144… Above, the first 13 numbers in the Fibonacci … Read more