andrewthenerd

a nerd enjoying life

professional -> software development -> python

Python

This will be my exploration into Python from installation through to developing some projects that will be of interest to me to expand my knowledge. It will be a step by step process, but some simpler things may be left out due to my previous software background.

Introduction

Python is a high-level language, that is interpreted line-by-line and not compiled like languages such as C. I am not going to go into too many of the features of the language here, but here is a list of a few of them:

Installation

My initial installation will be on my existing Ubuntu server that I have hosted with Fasthosts. It ius just a bare system, so I have to generally install everything from scratch.

To make sure Python is installed type python3 on the command line.

~ python3

On my set up I had the response:

Python 3.10.12 (main, Nov 6 2024, 20:22:13) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

So it is already installed. If it is not present though, you just need to install with sudoi apt install python3

Development Environment

I am not using anything special for development. My package of choice for simple software programming is Texttastic. This is just to allow quick uploading to my Linux server where I normally run software, plus where my websites like this one are stored. When learning a new language I prefer an environment that doesn't try to give to much advice on what to type, so that I have to learn keywords, functions and structures.

First Program

I am not going to start with the traditional Hello World!, but dive in with a bit more of a useful set-up. A main body, calling a function, getting user input, and an output based on that input.

To be original let's call this program first_one.py. It is not a requirement to have a .py extension for python progam, but it does aid clarity, and in IDEs it will automatically apply syntax and formatting for Python if you name the file this way.

def getnumber():
  while True:
    try:
      value = input("Please enter a number: ")
      value = float(value)
      break # Exit the loop if successful
    except ValueError:
      print(f"The value {value} is not a number. Please try again.")
  return value

def squared(val1):
  return val1*val1

def main():
  a = get_number()
  i = squared(a)
  print(f"The square of {a} is {i}")

#this states which function to start from when the program loads
if __name__ == "__main__":
  main()

As I am developing mainly on my Ubuntu server, I will then upload this to the development directory. To run the program, on the command line, will be:

~ python3 first_one.py

The program will then run something like this:

Please enter a number: 2w
The value 2w is not a number. Please try again.
Please enter a number: 5.5
The square of 5.5 is 30.25

Now I have dived right in there with this program, but just these few lines of code show a good overview of quite a few concepts of Python.

The program has been broken down into several functions; get_number, squared, and main. Functions are useful to make code more readable, and broken down into discrete functionality that can aid reusbility and make testing easier. So, for example, because the squared function is separate from getting the number this code can be re-used. Plus once you have tested squared you do not need to re test it if the get_number function is changed. OK, it is a very simple function, but if you had much more complex code this becomes an important factor.

There is also the ability to get information into the program, and output from the program. Programs do not work in isolation, they will take inputs from the keyboard, devices or files, process them, and output somewhere. Keyboard input and screen output are the simplest form, but we will go into others as we explore Python.

A loop is included, in this case to keep getting an input until it is a number. As with other languages there are many loop types to use, depending on the need of the program.

The loop also included exception handling, so if something goes wrong the program doesn't just crash, but can be handled elegantly. In this case the program attempts to cast the input to a float, if it fails (because it isn't a number) then this is captured by the except, and the user is asked to try again.

Let's Begin

So that was a bit of a dive into things, so now we can go back to the beginning and understand the basics of the language builiding up a project as we go.

Get started with Python (click here)