Featured

Automating Web Browsers with Python

If you use the same tabs in a Web browser every day, it can seem menial and time consuming opening each tab individually. So, today I’m going to show you how to open all of the tabs you use with one double click.

To start off with, we’re going to need to open a text editor. For this, I’m using Visual Studio, but you can use any text editor. Just remember to save the file with a .py extension. For example: Workflow.py

To begin with, we need to import the ‘webbrowser’ module. This module is responsible for launching and controlling Web Browser Functions. If you are working on Windows, the program will use whatever browser is set as the default. In this case, the tabs will open in Firefox.

After importing the ‘webbrowser’ module, it is time to set up opening Firefox. For this demonstration I am going to load https://www.google.co.uk. The code for that is ‘webbrowser.open_new(“https://www.google.co.uk)

Now that we have written the open browser function, it is time to amend it, so that it will open the following web page in a new tab. This is simply done by amending the previous code to add ‘_tab’ on the end like so: webbrowser.open_new_tab(https://www.facebook.com)

To open any additional tabs from here will also use the ‘_new_tab’ function. This can be used to open as many tabs as needed. Printed below is the full program that I have written for this demonstration.

As displayed in the program, the file is saved as ‘WorkFlow.py’, this is a file that I have saved to the desktop. The image on the desktop shows the Python logo to confirm it has been saved as a Python executable file.

To run the program, just double-click and as you can see in the Screenshot below, it has opened the three webpages that we were setup in the program.

Congratulations, you have just automated the opening of various webpages using Python programming.

Featured

Getting Started with Python in Linux

Getting started with Python in Linux

There is no need to install Python on Linux as it comes preinstalled. After opening the Terminal, the first thing to do is to create a folder that will contain all of the Python files.
To create a folder named Python, type ‘mkdir Python’.

mkdir Command

After creating the Directory, we are now ready to create our first “Hello World” program.
To get to the Directory that was created in the previous step, use the command “cd Python”

Change Directory Command

Once we have created and moved into the Directory, we are now ready to start our first program on Linux.
To create the our first file, we are going to use the ‘Touch’ command, and name our file something that is easy to remember for later on. In this instance we are going to call the file, “Hello.py”. We have the .py extension because it is a Python File.
When we use the ‘Touch’ Command in Linux. It doesn’t look like anything has happened. However, if you type ‘ls’ and press enter. This will list all files in the current directory, and as you can see from the screenshot below, there is currently only one file in the directory.

Demonstrating File Creation and Listing in the Terminal

It is now time to edit the file, and to do this we are going to use the VIM editor. Similar to before we are going to type ‘vim’ followed by the document name. e.g. vim Hello.py
It may be difficult to see in the image below. However, there are some key things to point out. Firstly, in the bottom left corner of the window, you can see the file name that we are currently working on.
Secondly, vim opens in a ‘read-only’ mode. For us to be able to edit it, we need to press ‘I’ on the keyboard, which will allow us to start editing the document. When you start editing the document, the file name will disappear from the bottom left of the screen and be replaced with the word ‘Insert’. This lets us know that we can start making changes.

Vim Editor

You can write anything you want for your first program using Python. Type the following code:
print(“A string of Text”)
Once you are happy with the code that you have written, we need to exit vim and go back to the Terminal window. To exit vim, enter the following command: Esc key :wq!
That command will convey to the editor, that you have finished typing and that you wish to write the changes to file and quit.
It is now time to run our Python Program. To do this type: python3 Hello.py
It will display the string of text in the Terminal window, as shown below.

Output of First Python Program in Linux

Congratulations, you have just learnt some basic navigation through the Linux Terminal and created your first program in Python using Vim.

BMI_Calculator.py

BMI Calculator Script

I have been playing around with if and elif statements and created this BMI Calculator.
There are a few different elements that needed implementing to get this to work properly.
Firstly, the program needs variables creating that will take input from the person using the program. I have used a mixture of integers and floats. However, it is best to use a float to enable the user to enable a decimal place.
To calculate the BMI output the weight input needs to be multiplied by 703 and divide that number by height in inches squared.
The program needs to be completed with if and elif statements to give a predetermined output, ranging from ‘underweight’ to ‘obese’.
I have ended the program with a print statement that states any BMI outputs should be used as a guide only.

BMI Output

Creating ‘Rock, Paper, Scissors’ using Python

In order to create ‘Rock, Paper, Scissors’ we’re going to need to make use of the loop functions that are available in Python.

Firstly, we are going to need to call upon the ‘random’ module which will allow us access to the ‘randint’ method. I will explain why this is important, a bit later.

from random import randint

Once we have defined which module is going to be needed, it is time to create the variables needed to play the game. In this scenario, I have labelled the variable ‘g’ for game, but you can define it any way that works for you. We can add multiple values in this by use of square brackets and separating each variable with a comma.

List variable

Now that we have our variables, it is time to create the players. We are going to start with creating the computer, as that is who we will be playing against. When creating the Computer, we will need to combine ‘randint’ so that a random play can be chosen from the list. As was done with the list, randint is encased in square brackets, with ‘0,2’ in curly brackets. In Python all operators start at zero.

In this instance:

Zero = Rock
One = Paper
Two = Scissors

Computer

When the Computer has been set, its time to add the Player. In this case, I have set the variable as my name. It really makes no difference what you label the variable, so long as you are consistent throughout otherwise, the program will run into errors. The player value needs to be set to ‘False’. The reason for this is when the game starts, the operator will automatically change to ‘True’ allowing the While loop to run.

Player

Now that all the variables have been set, it is time to move onto the main body of the program. Inside the ‘While Loop’ are nested ‘if’ and ‘elif’ statements which control the various outcomes of the game. It is important to define the tie breaker early on and then defining the reasons that the game resulted in either a Win, Loss or Draw.

As you will be able to see from the following screenshot, I have added ‘\n’ at the end of the user input. This will end the string on a new line and makes the overall responses within the game easier to read.

Finally, at the end of the ‘While Loop’, it is important to add an else statement for an invalid play. This will allow the game to continue if an incorrect value is entered by the user.

While Loop

Two more lines of code are required at the end of the While Loop. These two lines are required, so that the game will run from one instance to the next in an infinite loop.

The player variable needs to be set back to ‘False’ at the end of the play for this to occur, and the computer is listed here for the same reason, so that it will continue to be assigned random plays.

Infinite Loop

For any fans of the Big Bang Theory, I am hoping to try and expand on this code to create a ‘Rock, Paper, Scissors, Lizard, Spock’ game, but first I had to work through this code to understand how it should work. In addition to this, I want to try and build on it further by adding a graphical interface.

I also want to say that I got the code for this game from this website, they have a lot of great tutorials and content for anybody learning Python.

Creating Mad Libs using ‘F-strings’

What is F-String?
An F-String is a formatting concept that allows us to call upon Python functions within strings of text. F-string stands for ‘Formatted string’.  The use of F-string is only available in Python version 3.6 and above. Previously to this, you would have had to use .format() instead. A more up to date version of Python can be downloaded from https://www.python.org

To create a Mad Lib using an F-string, we first need to create a couple of variables that will be used in our string of text. In this example we’re going to be using two variables. A subject and a noun.

Creating Variables

Once the variables have been created, it is time to implement the ‘f-string’. F-strings are easy to spot within the program, as they always start with a letter ‘f’ and the string of text that follows is in speech marks or single quotes. Like so: mad_lib = f”string of {subject}”

Mad_lib

The curly brackets ‘{}’ are required to import the variable that was created earlier. When the program is run, the curly brackets will be replaced by the text the user entered.

Finally, to run the program, a regular print statement is needed.

Print Statement

If everything has been written correctly, and no errors have been reported. When the program runs, the output should look something similar to the image below.

If you are using a version of Python that is older than 3.6. You can create a similar program, however, its layout will look slightly different.

Format String

The ‘.format()’ approach will still work if you are using a version of Python that is later than 3.6. It differs from ‘f-string’ in that the curly brackets are left blank and that they are specified at the end of the string. Once the variables have been created, they can be used in any order. Simply by putting: .format(subject, name) would change the layout of the sentence.

Format Output

It doesn’t matter which version you use to create the Mad Lib. Both of the program’s outputs are identical achieve the same goal.

Automating Desktop Applications with Python

Automating Desktop Applications with Python

This tutorial follows on from last week’s automating web browsers with Python. If you have not seen that one yet, you can check it out at the following link:

The same as last week, I’m going to be using Visual Studio to write the program, but you can use any text editor of your choosing. Just remember to say the file with the .py extension, so that your machine will run it as a Python executable.

To begin with, we need to import the ‘os module’. The module gives access to Operating System functionality and allows the opening of programs.

Import os

Once the ‘import os’ command has been written, there is only one more command left to write and that is ‘os.system(‘start <application_name>’)

This command can be written as many times as needed, just by changing the application name to something different.

os.system

In the end, this program was relatively simple to write. However, it did cause my quite a bit of frustration trying to figure out the syntax needed for the program to run correctly. I have added the code from this program to what I wrote last week to setup an automated link on my desktop, so that it will run with a double-click of the mouse.

Completed Work Flow

This can be expanded on further still by allowing the newly created application to run on start-up.

In the bottom left-hand corner of your keyboard near the ‘ctrl’ key, should be ‘Windows flag’ also known as the ‘Super-user key’ in Linux. Press the Windows Key and the letter ‘R’ at the same time. This will open a tab called ‘run’.

Type: shell:startup

Run Command

In the window that opens, copy and paste the newly saved Python program to that location. This should then run every time the machine is booted up.

File location for Python Program start-up

To stop the file from running at start up, delete the file from that location. Alternatively, open Task Manager using Ctrl + Alt + Delete and navigate to the ‘Start-up’ items. The program should be listed, right-click on the item to enable or disable it.

Start-up items

Finally, save all your work and reboot the machine to test it works when the machine boots up.

Congratulations, you have just created your first automated setup using Python Programming.

Learning SQL

I recently installed the Sololearn Application on my phone and have been working my way through the SQL and Python courses. It has been a great learning resource to use whilst on the go.
The SQL course was a good starting point in learning some of the fundamentals for writing SQL queries and seeing them in action. Whilst there was plenty of information to read through, there is also ample opportunity within the Application to put it into Practice with a sandboxed coding area. There is also a brilliant community to test yourself against in coding challenges.

Getting Started with Python on Windows

Getting started with Python in Windows

To install Python – Head to a Web Browser (Edge, Chrome or Firefox) and type https://www.python.org/downloads

Click on ‘Download Python 3.9.1’. This version could be different, it all depends upon when you’re reading this.

https://www.python.org.uk/downloads

Follow the on-screen instructions to complete the installation of Python.

When it has finished installing, open a Command Prompt and type ‘Python’. Using this Command will start Python running in the Command Prompt and also let you know which version is currently installed.

As you can see from the screenshot below. Version 3.9.1 is installed, and now we are ready to create our first program.

Show Version of Python using the Python Command

I’m going to demonstrate the printing of string of text. This can display anything you want it to. Traditionally, when learning a new programming language, the first program is called ‘Hello World’, because the output of the program is ‘Hello World’. However, in the below screenshot I’ll get it to print ‘Coding is fun!’

When you have finished coding this way, or you need to use the Command Prompt for something else. This mode can be exited using Ctrl + C followed by Ctrl + Z.

Creating the first program

Alternatively, you can create your programs using Notepad. This will require you to save the filename with .py on the end. This is so that your computer knows it a Python file. For example; Hello.py

Creating a program in Notepad

If you have created the program using Notepad. Open the Command Prompt and browse to where the file is located. Mine was saved in the Downloads folder for ease. To get here I used the Command ‘cd Downloads’. (cd = Change Directory)

Once there, type in the Command ‘py ProgramName.py’

Please note, ProgramName refers to whatever the program was named.

Showing the Output in Command Prompt

Congratulations!

If you have made it this far, you have just successfully installed Python on Windows and developed your first program.

Binary System

“There are only 10 types of people in the world: Those who understand binary and those who don’t.”
In Binary, the number ‘2’ is represented by the digits ’10’.

The binary number system is a mathmatical concept that was invented by Gottfried Leibniz in 1689. Binary is a base 2 mathmatical system that consists of two numbers: Zero and One.

The most widely used number system in place today, is the base 10 number system. Otherwise known as the Decimal system. We use this system because we have ten fingers, this makes it more natural for us to count in groups of ten.

The base 2 system is the basis for all binary code and is used everyday all around the world on any device with a processor.
In a computer system, zero and one can represent a number of different things. For example; On or Off, True or False.

In a computer chip; Zeroes represent no flow of electricity and One’s are represented by electricity flowing.
This pattern allows for any sequence of numbers of letters to be translated into binary and transmitted along a network in the form of an electrical signal.

Source: @networkingwizard

The diagram above shows how each value in binary can create a different value. When a ‘one’ occupies every space, that value becomes 256 bits. For every eight bits in binary, that adds up to a total of one byte.