huw-rhys-jones / Project-Vulcan

Attempting to read manuscripts from Herculaneum
GNU General Public License v3.0
1 stars 0 forks source link

Preparation 3: Basic Python Programming #6

Open huw-rhys-jones opened 1 year ago

huw-rhys-jones commented 1 year ago

Overview

In this homework, we are going to learn some Python skills that you might use throughout this project. The first things you will learn about are:

These are some very basic skills and they aren't hard to learn, but they are fundamental tools to use in python.

Commenting

Commenting is very important for a number of reasons. Any line that starts with a hash (#) symbol is essentially ignored by Python, it is only to be read by human beings (like you!). It might be instructions to the user, an explanation or a reminder to do something etc. Another reason to comment out a line of code is that we might want to try out not running a line of code. Perhaps we want to temporarily disable some feature we've added, but we don't want to delete it outright - we just toggle it off by putting a hash at the start of the line. See the two lines of code below. One of them is commented out, one is not. Can you tell which is which? What would we do if we wanted to toggle a line on or off?

# print("This is one thing we could do") print("This is another")

Have you heard the saying that most of the human genome doesn't actually do anything and is just junk that doesn't do anything? A way to think of this is that most of our DNA is commented out: it's encoded in there, we can read it, but it effectively does nothing.

Regular (uncommented) lines

If a line of a Python file is not commented then you are telling Python to run this code. For this to happen successfully when you come to execute your Python file, it needs to be valid i.e. it has to follow a very specific set of rules. Look at the line of code below:

prnt("this is a print statement")

You can probably guess what I wanted to do here, but I have made a mistake. Now you and I may understand the meaning of this line, but to Python this might as well be completely random bashings of the keyboard. It has to be perfectly correct for Python to understand and execute your code.

If the cursor (that little vertical flashing line) is on a line and you want to toggle it as commented/uncommented, you can press Ctrl+/ or you can simply add or delete a hash (#).

Defining variables

Variables are bits of data that can be used by our Python program. Variables have a range of different types depending on what kind of data we want to store in them. For example, if we wanted to store a whole number, we would use a variable of the type integer (or int). See the line below of how an integer variable with the name number would be defined with a value of 3:

number = 3

Some important things to note here, forgive me if these are obvious to you:

Now we are quite lucky in Python that we don't have to state the type of a variable, it is implied by the value we give it. In the above case, Python knows that we want the variable named number to be of type integer because we are giving it a round number as its assigned value.

Lets look at another example:

words = "This is a variable of type string"

We've defined a new variable, this time called words. In this case, the value is not a number, but a set of words contained within inverted commas. If you put anything within inverted commas in Python, you are telling it to essentially store this as text, just like a human would read. This variable is of type string. What's the difference between a string and a commented out line as seen above? The key difference is that a commented out line is essentially ignored by Python, whereas a string is stored allowing us to do something with it later. For example, we can use them as part of a print statement:

print(words)

What would happen if we ran the above line in Python?

print("words")

How about this one? What's the difference? Latter on you can try both and see what happens.

Print statements

The print statement is what is called a built in function in Python. We will go into detail as to what exactly a function is in Python, but for now we will just say it is a command to do something specific. In this case, the print function tells us Python to write something to the terminal. As I understand it, in the olden days when code was written on bunch cards, such a function as this gained the name print because it was used to print to the screen. It is still very useful today for a range of reasons such as updating the user at runtime of the progress of the program, outputting data and is wildly used by stubborn/lazy programmers (including me) in a process called debugging (figuring out why something's gone wrong).

image

Functions are used in a very specific way: we write the name of the function (in this case it is print) then we add some rounded brackets and place an argument inside it. Essentially the argument is us telling the function what we want it to do:

print("This string is entered as an argument of a print function")

Above, we are giving the print function an argument of a directly defined string. Depending on the exact function we are using, we may be able to define it more than one argument, there may be optional arguments, or it might not need an argument at all. For example, both of the below lines of code are valid. What do you think they do?

print()

print("Hello", "Goodbye")

words of encouragement

All of this might seem a bit overwhelming, especially if this is your first foray into the world of programming. There's lots of weird phrases, everything looks a bit alien and you're afraid of doing it wrong. This is normal and there really isn't anything to worry about. If you get stuck, ask me, there really are no stupid questions here.

The thing to remember is that you can't strictly do programming wrong or break it in a permanent way. You can always go back to an earlier version of your code where it worked and start again from there, or you can comment bits out that may be breaking it. It's just like in a computer game when you lose, you can just start again from the last save game.

image

The actual homework assignment

So what are you actually going to do in this homework assignment? The first thing you are going to do is create a Codespace in GitHub - this is like a virtual environment that we discussed in Issue #2 using Docker. The first thing you want to do is switch over to your own branch that you made during Issue #5. You can do this from the main repo page.

image

Next click the <> Code button (blue), then the Codespaces tab (yellow), then finally the plus symbol (+) to create a new Codespace on this branch.

image

After some set up screens, you should be prompted to install some extensions: I'd recommend accepting for the Python one, but the Pylint one has caused me nothing but annoyance. YMMV.

image

Finally, you should see a new window similar to what is shown above. If you followed the instructions of Issue#2, this window might look awfully familiar to you, almost like it is Visual Studio Code … in a browser. On the left hand side of the page, you should see the project tree this is all the files in the branch of the repository you just selected. Click on the file named example.py (yellow) which should open it up in the main panel of the screen. Note that the file extension (.py) denotes this to be a Python file. Your homework assignment is to read through this file and follow the instructions. Note that you can run this Python file at any time by pressing the play button (⏵) that is in the top right hand side of the screen. If you have read and understood everything above, this homework assignment should be a complete doddle.

Good luck!

apathtea commented 1 year ago

I'm getting an error:

error: pathspec 'homework' did not match any file(s) known to git error: pathspec 'task1.py' did not match any file(s) known to git error: pathspec 'task2.p' did not match any file(s) known to git

RyanMFox commented 1 year ago

done

Corvuscorax888 commented 1 year ago

done