Home » Programming » Python

How to Clear the Terminal/Screen in Python

How to Clear the Terminal/Screen in Python

This tutorial will show you several ways to clear the terminal/screen in your Python scripts, with code examples that you can copy and paste into your own project. Clearing the screen will remove all text from the screen, blanking/resetting it. This is useful to hide output from the command line or previous Python statements so that you can more easily see newly added output, and keep your command line apps looking tidy. There are a few ways to achieve this, so here they are: Clearing … Read more

Home » Programming » Python

How to Create a Dictionary in Python from a JSON API/URL

How to Create a Dictionary in Python from a JSON API/URL

This tutorial will show you how to create Python dictionary from JSON data retrieved from an API via HTTP. This is useful for retrieving data to use in your Python scripts and applications. Step 1: Install the request Library The easiest way to interact with web resources in Python is using the requests library. Install it by running: You may want to use a virtual environment to keep the dependencies for all of your Python projects separate. Step 2: Know the Format of the JSON Data you are Retrieving JSON … Read more

Home » Programming » Python

Python NumPy Crash Course – A Simple Tutorial and Example

Python Numpy Course Tutorial Example

Python’s NumPy library is the standard for working with numerical data. This article will explain why that is, and how to use it. In addition to this, code examples for basic usage will be shown, and we will build a real-world example that implements NumPy to work with some example data. Python and Arrays Whether you are working with data collected for scientific or business reasons, it is usually collected in the form of arrays of data presented as tables with single or multiple columns. … Read more

Home » Programming » Python

Python Virtual Environments with virtualenv and Anaconda

python virtualenv

This article will explain and demonstrate virtual environments in Python using virtualenv and in Anaconda. What is a ‘Virtual Environment’? In Python, a virtual environment is an isolated directory that contains all of the dependencies of a Python project. By default, when you install a package using the pip package manager, it is installed globally – that version of the package is available to all Python scripts running on your system. This can cause problems. It clutters up your global Python installation, and all projects … Read more

Home » Programming » Python

Printing Text in Python Without a Newline

Python print without newline

This short tutorial will show you how to print text in Python, without a new line appearing. The Newline Character in Python In Python, newlines are represented by a \ (backslash) followed by n: \n You Can Add New Lines to Printed Strings The \n won’t be shown to the user – a new line will be inserted instead (unless an escape character is added). So, when the below code is executed: print(“Linux\nScrew”) It will appear as: Linux Screw Print Statements Add a Newline By Default By default, a newline … Read more

Home » Programming » Python

How to Read CSV Files in Python, With Examples

Python read from csv file

This article will show you how to read CSV (Comma Separated Values) files in the Python programming language, 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 information … Read more

Home » Programming » Python

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

Python use setattr() to set object attributes

The setattr() function is used to set 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 Sort a Dictionary by Value in Python [Examples]

Python sort dictionary by value

This article will show you how to sort a dictionary by the values it contains in the Python programming language. Code examples are included. What is a Dictionary? A dictionary is one of Pythons built in variable types for storing data. Dictionaries are similar to associative arrays or hash tables in other languages. A dictionary contains a collection of values or objects. Unlike lists and arrays, rather than their values being stored at a specific index (the ordered position in the sequence of stored values), values in a dictionary … Read more

Home » Programming » Python

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 » 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