Home » Articles by: Brad Morton

How to Use Nested For Loops in JavaScript, with Examples

JavaScript nested for loops

This article will quickly explain nested for loops in JavaScript, how they are used with multidimensional arrays, and show some examples. For Loops We’ve already covered for loops – check out our article here. To sum up the article, for loops repeat a block of code for a given number of iterations: for (let i = 0; i <= 3; i++) { console.log(i); // Will output the numbers 0-3 to the console } Above, a for loop is defined that defines an initial variable i that is used to control the loop, sets a … Read more

Home » Articles by: Brad Morton

How to Use the Bash echo Command, With Examples

How to use the echo command in Bash

The Bash echo command serves a simple purpose – it outputs (echos) text. Here’s how to use it, with examples. echo Command Syntax The echo command is very simple and has the following syntax: echo OPTIONS TEXT Note that: OPTIONS should be one of the following options -n Do not output a trailing newline -e Enable interpretation of backslash escapes This means that escape characters can be used to insert special characters into the output \\ backslash \a alert (BEL) \b backspace \c produce no further output \e escape \f form feed \n new line \r carriage return \t horizontal tab \v vertical … Read more

Home » Articles by: Brad Morton

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 » Articles by: Brad Morton

How to Calculate Natural Logs/Logarithms (ln) in Python

Python calculate natural log:logarithm

This article will show you how to calculate natural logs/logarithms in the Python programming language, with examples. What is a Natural Log/Logarithm (ln)? A number’s natural logarithm is it’s logarithm to the base of e. It’s a bit more advanced than the usual arithmetic you’re probably used to seeing as you learn to program, so here’s a bit of explanation: A logarithm is the reverse of an exponent. The logarithm of a number is the exponent that another number must be raised to to produce the first number. You’re … Read more

Home » Articles by: Brad Morton

Python – Add floating numbers accurately with math.fsum()

Summing floating point numbers Python fsum

The Python math.fsum() function takes a list of floating point 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 … Read more

Home » Articles by: Brad Morton

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 » Articles by: Brad Morton

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 » Articles by: Brad Morton

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 » Articles by: Brad Morton

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 » Articles by: Brad Morton

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