Home » Programming » Python

Python Main Function & Method: What Is It and How Is It Used?

Python Main Function Method

As you build more complex Python apps, you will probably want to start splitting your code into separate files. What Is the __main__ Function? Importing and executing code from another file is common in even small scripts and apps built with Python. As your library of scripts increases, you may choose to import previously written code into new projects. The __main__ function in Python is only executed when the script is called directly, allowing you to set different behavior based on how your code is being … Read more

Home » Programming » Python

How to Get User Input in Python [With Examples]

Get User Input in Python

This article covers getting user input on the command line using Python 2 or 3 and includes some useful examples. Get User Input in Python 3 Python 3 uses the input() function to collect user input: myText = input(“Enter some text:”) print(“You entered the text: ” + myText) Get User Input in Python 2 If you’re still using Python 2, you will use the raw_input() function instead: myText = raw_input(“Enter some text:”) print “You entered the text: “, myText # Python 2 uses different syntax to print output Typing Input … Read more

Home » Programming » Python

Python Scatter Plots with Matplotlib [Tutorial]

Python Scatter Plots

Graphs are awesome. If you disagree, you probably shouldn’t read on. The best (and easiest!) way to create graphs and scatter plots in Python is using the package Matplotlib. This tutorial explains exactly how to do so. https://pypi.org/project/matplotlib/ What is matplotlib? I’ll let them introduce themselves in their own words: Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. This article will give you a jump-start on using Matplotlib to create scatter plots. Install Python Dependencies First, you’ll need to install MatplotLib using … Read more

Home » Programming » Python

Python isinstance Function, With Example [Guide]

Python isinstance Function

The Python isinstance() function determines the type or class of a variable. Read on to find out exactly how isinstance works, with an example below. What are Types? The type of a variable determines what it can or can’t do. It determines what value the variable may take and what can be done with that value. isinstance Syntax isinstance(OBJECT, CLASS) Note that: OBJECT is the variable or value to check the type or class of CLASS is the type or class we want to see if the variable type matches The function returns a boolean value (True/False) Types in Python Generally, … Read more

Home » Programming » Python

What is a Tuple in the Python Programming Language?

python tuple

A Tuple is a type of variable in Python – a data structure containing multiple parts. It’s a collection (or array) of data – but a specific kind of collection. It’s Ordered (Indexed) Elements in a tuple are stored in order – stored with an index or position – their position in the collection is fixed, and they can be found by their position. It’s Unchangeable (Or Immutable) Once a tuple is created, elements in it cannot be moved, removed, added, or altered. Duplicates are Allowed Duplicate values are allowed as tuples are indexed, so multiple elements with the same … Read more

Home » Programming » Python

Read a File Line by Line in Python [3 Methods]

Line by Line in Python

This tutorial will examine several methods and best practices for reading a file line-by-line in the Python programming language. Reading a file line-by-line will be useful for parsing log files, CSV spreadsheet files, or even reading files you have generated from an application yourself. Line by Line File Reading – Examples The examples on this page will read from a text file named mytext.txt with the following content: Linux Is Very Cool Using the readlines() Function The following python code will call the built-in open() function, which … Read more

Home » Programming » Python

Python String replace Method [With Examples]

Python String replace

This article shows you how to use the string.replace() method in Python to replace elements in a string of characters and includes useful examples. Python String replace Syntax The replace() function can be called on existing string variables like so: string.replace(find, new, count) Where: string is the existing string variable find is the string your want found and replaced new is the text you want to replace the found text count is the number of instances of the found text you want to be replaced, counted from the beginning of the … Read more

Home » Programming » Python

Raspberry Pi & Python Powered Tank

pi python powered tank

The title pretty much explains this project- it’s a tank, that can shoot BBs – powered by a Raspberry Pi and Python Code. It’s pretty awesome. This is part one of a two-parter and handles all of the hardware and wiring and a Python script for test firing the engines. Click here to see Part 2 of this project where I get a live video stream and buttons to control the tank into a web UI! As with my other projects, I’ll try to keep things simple … Read more

Home » Programming » Python

Python range Function – How to Use It [With Examples]

Python range Function

The Python range() function returns an immutable sequence of integers between a given start and endpoint, incrementing by a given amount. Python range Syntax Here’s the syntax for the Python range() function: range(start, stop, step) Note that: start is the integer to start incrementing from – it will be included in the range If it is not supplied, it will be assumed to be 0 stop is the integer to end the range at – it will NOT be included in the range – the range will end before the stop value If start and stop are the same, an empty … Read more

Home » Programming » Python

How to Install Anaconda (Python & R) on Ubuntu

Install Anaconda Ubuntu

We like Python. Anaconda is a popular distribution of Python and the R programming languages, which includes package management and deployment tools, with hundreds of packages pre-installed. Anaconda is aimed at data scientists and people working with machine learning, but it’s useful for anyone. It has a quick and easy install process for Ubuntu, so here’s how to get up and running! Download the Anaconda installer Anaconda has various additions, and we want the Open Source (free!) individual edition, available from: https://www.anaconda.com/products/individual Scroll down and … Read more