In this table I will compare some basic programming syntax and conventions between the Python and Kotlin programming languages. Programming element Python Kotlin Commenting code: # This is a comment // This is a comment Multi-line comments: “”” This is a comment. This is a comment, too. This is a comment, too. “”” /* This … Continue reading Python and Kotlin: A comparison of the language basics
Python
Python: How to strip HTML and convert to ASCII
Let’s say you have an API feed or a form on a web page that passes in a block of text that could have HTML and UTF-8 or other specially encoded characters. Now let’s say we need to clean this feed upon arrival such that there are no HTML tags left in it, and all … Continue reading Python: How to strip HTML and convert to ASCII
JavaScript: How to call a Python script and get the results via jQuery
Let’s say you have a situation where you need to read JSON data from an API and you don’t have plug-in credentials to access the API directly, but you can see the JSON data that is output from the API on a page. In this case, you can still get the data if you “scrape” … Continue reading JavaScript: How to call a Python script and get the results via jQuery
Python: How to play a video (with sound) in Pygame
When creating a game in Pygame, the trick to getting a video with sound to play is to place pygame.mixer.quit() right after pygame.init(). This is because pygame.mixer doesn’t play well with pygame.movie. For example: pygame.init() pygame.mixer.quit() Here is what that looks like in the context of a larger block of code: import pygame FPS = … Continue reading Python: How to play a video (with sound) in Pygame
Python: How to implement a LIFO stack
Stacks are a computer science data structure useful for special types of computation where you essentially add information to the top of the ‘stack’ and also remove information only from the top of the stack. LIFO stands for Last-In-First-Out, which is an acronym that aids in remembering what is going on in a stack, i.e. … Continue reading Python: How to implement a LIFO stack
Python: How to open and read a .docx document
To open and read a Word (.docx) file in Python, we need to import the docx module. If you don’t have the docx module, it can be downloaded with pip. Specifically, we will use the opendocx and getdocumenttext functions of the docx module: from docx import opendocx, getdocumenttext The opendocx() method will be used to … Continue reading Python: How to open and read a .docx document
Python: How to write to a .csv file
Just like reading from .csv files, writing to .csv files also requires importing the csv module. Python: How to open and read .csv documents First define the path and name of your output file, then carefully build your output such that every line you want to write in the .csv file is a list with … Continue reading Python: How to write to a .csv file
Python: How to open and read .csv documents
Unlike text documents, which Python has a native open() function for: Python: How to open and read text documents Opening and reading .csv files requires importing the csv module and using a csv.reader(). For example, this code will open and read the first 10 lines in a .csv file: import csv csv_path = r’C:\Users\Chris\Desktop\Python Scripts\Python … Continue reading Python: How to open and read .csv documents
Python: How to install WinPython and Jupyter Notebook
To use Python with a Jupyter Notebook for Data Science and other projects, you will first need to make sure you have Python 3 and Jupyter Notebook installed. There are Jupyter Notebook installation instructions here: https://jupyter.readthedocs.io/en/latest/install.html This site recommends installing Anaconda, which bundles installation of Python 3 with Jupyter Notebook, which is just fine. However, … Continue reading Python: How to install WinPython and Jupyter Notebook
Python: How to open and read text documents
This article will details some basic Python techniques for opening and reading text files. This applies to any plain text files, they don’t have to have a .txt file extension. I will detail three different techniques. Example 1: Open text files and read one line at a time with .readline() This technique demonstrates probably … Continue reading Python: How to open and read text documents