Home » Programming

Square Numbers and Exponents in Python, With Examples

Square Numbers and Exponents in Python

This tutorial will show you how to use exponents (e.g., calculating the square root of a number) in Python, with some code examples. This tutorial covers several ways to calculate exponents in Python 3. Using ** (Power Operator) The ** mathematical operator (known as the power operator) will calculate an exponent, raising the number on the left to the power of the number on the right of the operator: 4**5 # Will evaluate 4*4*4*4*4 and return the integer value 1024 Note that: You will receive a ZeroDivisionError if you try to raise 0 (zero) … Read more

Home » Programming

How to Use ‘for’ Loops in Python, With Examples

Python for Loops

This article will show you how to use for loops in Python 3 and provide examples. Loops are one of the fundamentals of programming. So you’ll use them almost constantly. Data in computer software is often stored in arrays (or lists), for example, a record of students enrolled in a course, with each item in a list representing a student. These arrays of data need to be processed – and this is done using loops that step through each item in the array and perform some … Read more

Home » Programming

Python lstrip and rstrip Methods – With Examples

Python lstrip and rstrip Methods

This short tutorial will show you how to use lstrip and rstrip to remove whitespace (or any other characters) from the beginning/end of strings in Python. The lstrip and rstrip methods are available on any string type variable in Python 3. Python lstrip String Method The lstrip (Left strip) method will remove leading characters from the string – that is, characters starting from the left of the string. Syntax Here’s the syntax for the lstrip method: string.lstrip(characters) Note that: string can be any string value or variable with a string value characters is an optional parameter for the lstrip method These are the characters that … Read more

Home » Programming

How to Generate Random Numbers and Strings in Python

Python Generate Random Strings Numbers

This article will outline several methods for generating random numbers and strings in the Python programming language. Generating random values is a common and important task when programming almost any kind of application of moderate complexity. Random strings and numbers are used to generate unique identifiers (e.g., for the short URLs in URL shortening services), for identifying unique records in a database, and (most importantly) are frequently used to determine the behavior of gameplay elements in video games (for example, simulating a coin flip, or … Read more

Home » Programming

How to use the Python zip() Function, With Examples

Python zip

This tutorial will explain how to use the python zip() function to merge or aggregate several iterables (including lists and tuples). First up, the Python zip() function has NOTHING to do with compressing files. It’s got nothing to do with making a .zip file. It’s a totally different thing. So if that’s what you’re looking for, this isn’t it! What are Iterables? In the Python programming language, iterables are data types that can be iterated over item by item. It includes variables called lists, tuples, sets, and dictionaries … Read more

Home » Programming

Python: Calculating a Number’s Root (Square/sqrt, Cube), With Examples

Python Calculate Square Root

This article will show you how to calculate the square root, or any root, of numbers in the Python programming language. We previously covered squaring and raising numbers to a power in this article. What is a Root in Mathematics? The root of a number x is the number that must be multiplied by itself a given number of times to equal the number x. For example, the second root of 16 is 4 as 4*4 = 16. It is the second root because 4 must be multiplied by itself twice to reach the required result of 16. The third root of 64 is 4 as 4*4*4 = … Read more

Home » Programming

Best Way to Check if a File/Directory is Writable [Python]

Python - Check if File/Directory Writable

This quick tip will show you the best way to check if a file or directory is writable in the Python programming language. Python: The Best Way To Check if a File or Directory is Writable Just try and write to it. That’s it. If you can write to the file, it must be writable Try and write to it, and handle any failure which occurs. try: with open(‘newfile.txt’, ‘w’) as file file.write(‘hello!’) file.close() except IOError as error: # You could also catch Exception instead of IOError to … Read more

Home » Programming

How to Use if…else Conditional Statements in PHP [Examples]

PHP If...Else

Here’s a handy guide on using if/else/elseif conditional statements in PHP to execute code based on certain conditions. if/else statements are found in pretty much all programming languages. Computer programs need to perform actions based on a user selection, or the result of a calculation, or the value received from a sensor – and if/else statements are how this is programmed into the system. When building even the most basic programs, you’ll use them, so they’re worth getting to know. PHP Syntax There are several parts to an if … Read more

Categories PHP

Home » Programming

Appending Items to a List in Python with append() [Examples]

Python - Append to List

This article will show you how to add items to a list in Python using the append() method. Lists are a type of Python variable that can hold any number of member elements. They’re analogous to arrays in other programming languages. Lists can be defined and altered in several ways: Using the Python ‘filter()’ Function to Filter a List, with Examples Using the Python ‘map()’ Function to Process a List, With Examples Python List ‘sort()’ Method – Sorting Lists in Python Easily How to Reverse a List in … Read more

Home » Programming

How to Use the PHP echo Statement, With Examples

PHP echo

This quick tutorial will show you how to use the PHP echo statement to print/display information on screen. A computer program isn’t much good if it doesn’t display some output to the user. The echo statement prints a simple string of text in PHP – usually to the web browser. Syntax echo is a simple statement with a simple purpose – and it has simple syntax: echo expressions Note that: expressions should be one or more strings containing the text or characters you want to display on screen If more than … Read more

Categories PHP