Files

247 lines
19 KiB
Plaintext
Raw Permalink Normal View History

Episode: 282
Title: HPR0282: Python Programming 101: Part 2
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr0282/hpr0282.mp3
Transcribed: 2025-10-07 15:29:32
---
That's alla hit!
Welcome to Hacker Public Radio everyone, my name is Soak.
Programming 101 Part 2, Python 1 with over half the votes and had way more than anyone
else.
So let's learn some Python.
First up we're going to use Python 3.0 because it's the new one and it's not compatible
with 2.0 so it seems silly to learn an outdated version.
I'm running Ubuntu so I simply do a pseudo-app to get install Python 3.0, put them password
in and installs for me.
If you're on a devian-based system that should work, although I haven't tried any others.
If not, you hopefully know how to do a yum install or whatever method you use for installing
the applications.
I'm assuming there's also an install exit for Windows should you really want to use that.
Next we're going to need an editor.
So my current favourite programmer's editor and not just simple text editor but a programmer's
editor that does syntax highlighting and things like that is G-N-E-G-E-A-N-Y golf echo alpha
november yanky.
Again that's a simple pseudo-app to get installed G-N-E. There are many others, feel free to use
whichever one you want.
You can even use Vim though as an age power episode about that but you really want one
that does syntax highlighting so when you do brackets and things you can find what matches
with what.
That makes it a lot easier when you're coding.
Now let's set up a code directory to store all these programs so I open the terminal up.
Let's make one in your home directory.
So MKDIR space tilde 4th slash code.
That makes a directory called code.
We'll then change the directory into it so see the space tilde 4th slash code.
Hit enter, leave that terminal window open for now.
And assuming we've already installed stuff let's get right to our first program in Python.
In the editor of your choice simply write print, space, open brackets, quotes, hello world
quotes, close brackets.
When I say brackets I mean the shift 9 is shift 0 for open and close.
Technically parenthesis but I can never quite say that word right so I call the brackets.
Plus not everyone knows what parenthesis is, parenthesis sounds like a disease that your
mother and father have.
So when I say quotes I mean the shift apostrophe on the usc keyboard and shift tool in the
UK ones.
Don't know about the other layouts but that's the one I did but the double apostrophe.
Look at the show notes I've got all the programs I wrote in there.
I'm going to explain what this does in a moment but let's just run it make sure you've
got everything running for now.
Save the file as test.py, Python files, shouldn't.py so you know what they are.
Although this is more for you than for the OS.
So in the terminal you had opened we can try running the script with pythons 3.0 space
test.py.
Now assuming you saved the file into the code directory the terminal still in the code
directory should simply print out hello world on the screen.
Congratulations you just wrote them on your first pythons script, yay.
Now it's not really that exciting.
It just prints hello world doesn't do anything else but let's explain how this works.
And it's the command that we're running.
In this case it prints out onto the screen.
It is enclosed in brackets to show the command where it starts and ends.
Now this wasn't in Python 2 this is a new thing for pythons 3 I believe but the brackets
show where the command starts and ends.
Basically what it needs to look at.
Some languages enderline with a semicolon like C but not in Python.
We can put other commands within the brackets if we want but I'll cover that in a moment but
for the text we need to use quotes so where the text starts and ends.
Now of course some of you may realize that you can't then use a quotes in the text or
you can but if you just put a quote in the print command thinks that's the end of the
text and it gets confused so we need to put an extra character in to say no we really
do want to quote Mark Honest.
So in the hello world text in the program we've written already put a backslash quotes
in between the words and save the file and run it again.
Now we get the quotes in the output.
Backslash is a standard character to tell languages that you really mean that character
or the next character and not to think that it's the end of the text or whatever.
In fact I can't think of a single language but it isn't used in.
I feel sure there must be but SQL, Java all these ones it's the standard character to
say we actually want the next one.
Now let's try something a bit more interesting we will go into variables.
Variables are variable that is you can set them to be a value and then change the value.
Variables can be called almost anything.
You can't call a variable print for example because that's where the command and also
please please remember they are case sensitive so X in the lower case and X capital are
different variables.
If you typo and get the wrong capitalization it's a new variable and suddenly it's blank
and doesn't have what you think in it and it's confusing but no let's do something simple.
In the same file we can use new one but I'm going to use the same one.
Type in X equals 1 and on the next line print open brackets X close brackets.
Now what we do here is we say I want X to be the value of 1 and then I want to print
X out.
Now because it's a variable we don't require quotes around it.
If we did put quotes around the X it would print the letter X out.
Save the file and run it and remember to run the new file name if you use new file but
again I didn't so I'm just going to hit up on the terminal and run it.
It should print one out onto the screen.
Now you see this is going to be a bit more useful.
Now let's say we want to do some calculation and output the results onto the screen.
Python handles all the usual suspects add, subtract, multiply, divide etc.
Although because there aren't much point divide buttons on the keyboard at least as you
write them the two dots one above and one below the horizontal line for the divide which
is whatever writes and the divide with one actually physically over the other does no
space for that on the keyboard so we use star or the asterisk button which is shift 8
and forward slash which is the bottom right of the keyboard as well as the plus and
the minus the ones in the top right of the keyboard.
So type in the following X equals one new line X equals X plus five new line X equals
X star seven or time seven new line print open brackets X close brackets.
Now should print out 42 when you run it one plus five is six six times seven is 42.
Now of course we could change this code to make it less lines by saying X equals one
plus five times seven print brackets X brackets.
Now run that and it doesn't say 42 anymore and the reason being is it follows the mathematical
rules.
Now when I was at school a few people I knew called it Bodmas but my father always taught
it to me as a bit mass BIDMAS brackets indices divide multiply add and subtract indices
is squared and cubed and forth and stuff to the power of that's the order it runs it runs
brackets first then anything with powers then it does divide and multiply and then add
them subtract so one plus five times seven there are no brackets or indices so it does
the multiply first so it goes five times seven to thirty five and then I'd want to get
thirty six which is what it printed out.
So if we want to get it to print out 42 as in the first example we need to put brackets
around it because brackets are in front of multiply so go and change the code and say X equals
open brackets one plus five close brackets times seven and then keep the print on the
next line and run that and we're hey it says 42 now this is nice however if using multiple
variables it might get a little confusing X by Z so on so forth.
Now as I've said before in the previous episode I'm a fan of using more lines for more readability
I also try and comment my character as much as I can so if you want to explain something
hash or pound or October point warning on set but that shift three on a usc board on
new line hash this is a comment next line is X equals open brackets one plus five close
brackets multiplied by seven you should already have that next line hash print and brackets
quotes five quotes close brackets and the final line print open breaks X close brackets
now the first print command was actually being a valid line print five it is a comment
so it won't run and the comment of the first you could actually explain what the calculation
is for and not just say this is a comment the comments take the entire line they all
start with the hash that's about it you put a comment the compiler or the Python interpreter
ignores it entirely it doesn't care what you put in there it's just so you make it nice
and easy for you or whoever changes the code next to read now I try and do one comment
per line of code to explain it all that way it's easier to add it in the future there
is actually a saying where I used to work and believe other places if you look at a code
that you wrote three months ago it's like someone else wrote it you won't remember a single
thing if you've been get moving on and coding other stuff so put comments in because otherwise
you'll have no clue don't think oh I know what this does comment everything but I'll
get more into that later as it is these programs are going to be so simple so far we're
not going to need any comments now we know how to do some basic maths calculations with
variables but what if we wanted to print some text out well again we can use quote and
do that X equals quotes hello world quotes and on the new line print and brackets X close
brackets now I should point out that whilst variables can be named after anything almost except
for any reserved words as mentioned earlier like print it is useful to name or something useful
to actually know what they mean so instead of saying X we could call it message or something similar
like that that would actually help us now I am a proponent of the Hungarian or similar notation
basically that is you name the variables something useful but put at the start information about
the variable so a string either text of hello world would be S for string so S message with a capital
M would be a good variable name in my mind that way deep down in the code you see S message oh that's
what it is or S output and you know what it is it also means you could use S print for a string
and not have to worry about it being a reserved word because we've got S in front of it I will
cover more of this later including the different types of variables that you can use in their Hungarian
notations there are arguments over both ways I think Linus is a well he hates Hungarian notation
from what I've read but this is how it case some people like it some people don't I find it very
useful for my style of coding other people may not find it so much use it is entirely up to you
but for this at least when we get onto the bigger harder programs I will be using Hungarian notation
I'm going to try and make it nice and obvious so it makes sense now we can do other fun things
with variables we can add them together in similar we did this with x earlier by adding 1 to 5 we
can also do this with strings so if we get rid of whatever code we have there do x equals quotes
hello quotes new line y equals quotes old quotes new line print open brackets x plus y close brackets
now save and run it and notice there is no space between the words because we didn't say
there was we can either add a space in after hello or before world and that would fix it or we could
add a space in in the print statement such so if we edit the print statement so we say still have
x equals hello y equals world then we say print open brackets x plus quotes space quotes plus y close
brackets now we have the space in the middle and also notice we can do some cool things by combining
text and variables in a program so for example we can have s first name that was with the capital
f and a capital n and it's all as one word equals quotes john quotes s surname equals quotes smith
quotes print open brackets quotes deer space quotes plus s first name plus quotes space quotes plus
surname close brackets now hopefully you can see how this would be useful assuming first name and
surname would pull in from somewhere like a database you can have a program address each user by name
in fact many websites do this when you log in you are who for example I believe says hello and then
your name flicker does chow and different languages doesn't it and then you're using name
or you're actually your name depending on what you've got to set up as
and of course we've run this it says deer john smith now what if we want to do something a bit more
clever like say good morning or good evening and their name well let's assume we have x this
is the time I'm not going to fit it out with the actual time because we need to do modules and
importing stuff and okay I couldn't figure out how to do it in Python 3 yet but I'm working on it
we'll come to that later remember I'm learning Python as you guys are I mean I'm a little bit ahead
because I've already done other languages so I'm aware of certain things but I am learning Python
as well I really am going through and doing this as I say it well a little bit before I figure out
how if segments work and then I'll explain if segments my figure out how some of the variables
work and I'll then go through that so I've already read about floats and stuff which we haven't
explained yet but I'm only a little bit ahead let's assume x has whatever the hour is we will set
x to be 13 which is 1pm so it's going to be the afternoon so add a line at the start with
the following in it x equals 13 now we need to know if x is less than 12 to say morning we need
a new command called if if x is less than 12 then say morning else say evening that is literally
how is if else if something is something do this bit else if it's not do something else after
we set the first name of last name add in the new line if x is less than that's the shift comma
that one less than crocodile mouth facing to the right arrow pointing to left whatever you
want to call it less than if x less than 12 colon then tab in the next line to indent it properly
just once I'll explain this when we finish the program though print open brackets quotes good
morning space quotes plus S first name plus quotes space quotes plus S surname close brackets
then on a new line without tab else colon a new line with a tab we can actually copy the one
from above and change it from morning to evening but if not print open brackets quotes good evening
space quotes plus S first name plus quotes space quotes plus S surname close brackets so the finished
code should read x equals 13 as first name equals John as surname equals Smith if x is less than 12
colon print good morning first name surname else print good evening first name surname I'm skipping
a bunch of brackets and quotes colon some things in there but just hopefully that's right again
show notes who got the whole things you can just kind of paste them there if you want
now if you run it you should say good evening don Smith because it's afternoon and well we just
did morning or evening now change x to 11 and then run it again it says good morning now add
in another line at the bottom print open brackets quotes when does this get printed question mark
quotes close brackets so when do you think that will get printed well run it and find out
or guess and run it find out if you want but it depends on if you tabbed it or not if it's
tabbed in it's part of the else statement and it won't get printed if x is 11 if there isn't a
tab it will get printed regardless of what x is set to now this is one of the things I like about
genie it actually marks the statements and they become expandable and it shows you where they finish
making it easier to code because it's obvious where they are now some languages you have to close
the if statement so like basically you have to say if such and such then whatever else whatever
end if and some say fine which is if backwards some like c use curly brackets if open curly brackets
whatever close curly brackets else open curly brackets whatever close curly brackets
but Python uses the tab to show the end or rather the lack of tab to make sure you tab because tabbing
is important tabbing is important even if the language doesn't care because it makes it much more
readable readable so much more helpful when you try and debug check the code it makes easier for you
to read I think that will do it for this episode we've gone through a few things I mean we've barely
touched the language but we've gone through some of this stuff and tried to get you some
introduction to programming writing some very simple programs next time I think we're going to go
through some different types of loops and we'll go through inputting stuff and then we can write
some actual programs to do things actually not very exciting we're just going to do one which I
have to write at university which we input a letter and it prints from a to whatever less you
entered and if you put c for example print a a b a b c a b a all on different lines this introduces
a couple of different loops so we're going to go through some of this and then we're going to
start writing some actual programs and how to write the game of life it's again a fairly simple one
plus the haka public radio has got the little game of life glider in it so we'll have to actually
go through that and explain where this came from so people actually realize again something else
I did at uni and then we're going to go and build up some different things but this is going to
it for this episode I'd like to thank everyone that has given me feedback I've got several ideas of
programs to write for people nothing major just fairly simple things what someone's a random name
generator for example based on you could give it a word for example in Harry Potter you have
Professor Snape and Slytherin and they're all snake snake snake snake Slytherin and that whole thing
and the idea was that you could put a name in and it would come and give you change some of the
letters and give you examples or look up in the source for words that are based off that word
and then change some of those and give you ideas and it's it it's called a little idea I don't
think it should be that hard to do and we may end up doing this so I think that's called if anyone
else has called any suggestions for hey could you write this little program again let me know
not going to promise to do all of them but if they're cool and sound interesting and sort of
simple to do what's simplest to do you know as I said before I'm not going to be writing the
kernel anytime soon that would be really cool but I'm not going to be not in Python anyway
let me know some simple things and we'll see what we can do thank you for listening if you've
got any questions you can email me at zooksorrygmail.com that's xray osca kilo echo
Sierra osca Romeo uniform at gmail.com or you can visit me at zook.org xray osca kilo echo.osca Romeo
golf thank you for your time and you've been listening to Hacker Public Radio thank you for
listening to Hacker Public Radio. hpr is sponsored by caro.net so head on over to caro.nc for all of our