Python for Absolute Beginners Transcripts
Chapter: The big ideas of software development
Lecture: Examples of code
0:00 Now let's look at a couple examples of simple, software code that you're going to write or could write, through this course.
0:08 What we have on the screen back here is a little bit complicated but it's not too bad. And you know how you feel about that, we're going to get there
0:16 but we're going to start much simpler than this. The ideas we write in a very structured way to tell the computer what to do.
0:22 So here, is a very simple Python program or more accurately maybe a piece of a program. It's what's called a function it says check_access
0:31 and it takes this thing called an age. Now idea is, we should be able to use this piece of functionality anywhere in our program
0:37 to ask the current user whose logged in if we know their age, assuming that we do we can ask are they allowed to create an account
0:44 are they allowed to access this part of our site or whatever. So right at the beginning of that purple line with hash
0:50 that is a comment, that's meant for us. Much about writing software and computer code really has to do with writing for humans
0:58 not writing for the machines. Yes, of course, ultimately it's transformed for the machines but humans have to understand it and maintain it
1:05 and it's read many more times than it's written so we need to write it in a way that's super clear. So we have this thing of a function
1:10 it's called check_access and given an age like 12 and it said no, no, no, no, we can't have anybody who is 13 or younger, using our service.
1:18 So we can send out a little message, right sorry you can't use this service and we can tell whatever other part of the program
1:24 you did to use it, no, they're not allowed to do this. In Python we say return False, so return the value give back the value whoever asked
1:32 whether about you could use, this person can access this part, tell them no and we don't have yes or no we have true and false.
1:39 So if they're too young then we say, False. But if we get past this check, if we're not true that they're less than 14 they must be, 14 or older.
1:47 So then we can return true, yes anyone who is 14 or older can use our service. So, maybe you've not written a single line of Python yet
1:55 maybe you have, but possibly haven't, even so spend a few moments with this I think you could make sense of it.
2:01 That's one of the really cool things about Python is it's actually pretty easy to understand. Now they're very specific rules that we have to follow
2:08 alright it has to be if with a space and then this test and then a colon and then indented, for the code that it's going to be within that case.
2:16 But once you learn the rules it's actually not too hard at all. Well, that was Python. Notice, it's the same thing up here in the left.
2:23 Let's look at a couple of other languages that has exactly the same behavior and I think what you'll notice is a lot of them have more symbol talk
2:32 or they're much more verbose. They all accomplish exactly the same thing but they're not necessarily as clear or simple as Python.
2:40 Next one we have here in the middle, is C++. This language is, one of the core languages that build so many of the things that you use day to day.
2:50 Its been around since, I think the eighties it's based on a language called C which has been around even longer than that
2:55 and Windows was built with that. A lot of video games was built with that Linux was built with that either C or C++
3:04 and this is one of the, low level languages at least for software developers not necessarily for computers but you can see it does the same thing
3:11 and notice on this one we have to say what kind of data is returned. We have to say bool, check_access and we also have to say what type of data
3:18 is accepted in integer. Not a floating point number like 7.2 but seven, or eight. And then of course this Boolean is also a true and false
3:28 it's a very common idea. Notice we have this if, basically the same test but there's a lot more, curly braces
3:34 to indicate the ranges where parts running the blocks have code, and then there's semicolons to punctuate each line, C++.
3:42 This is common through many of the languages. Over here we have visual basic. Visual Basic is very very wordy
3:49 and Python is also kind of wordy doesn't have as many symbols but for some reason the Visual Basic words just get in the way
3:56 rather than clear things up so here you could explicitly say we have a function called check access we're going to pass the age by value
4:02 which isn't integer and then it's going to return a thing which is a Boolean. Whoo, that's a lot to write. But none the less it means the same thing
4:10 and we have another test. If the age is less than 14 then we're going to tell you some other stuff. Else, we're going to say, you know
4:17 check access is false or check access is true the way you, return the indicated value and Visual Basic is to set the value
4:31 it's the foundation of much of the interactive internet at least on the browser side of things.
4:36 Here we're going to create a things that's var check_access as a function that takes an age. And again, from there on it's very very similar
4:43 to the C++ side of things with curly braces, semicolons, and so on. The way it actually runs is quite different. And finally, we have C#
4:51 this is the .NET Microsoft language along with VB as well. And here we're going to have a program and a thing called a class
4:59 and a public static function there's a lot of descriptors in this and the cases and scenarios which you can use them
5:13 curly braces to describe the blocks semicolons to end the lines, and so on. So, you can see a lot of commonalities but also a lot of differences
5:22 amongst these different languages. Python and VB are both more, word-driven they don't have as many things
5:29 they don't have things like semicolons and curly braces but VB still defines all its blocks with like, closing type things like if and then
5:35 then and then, and if, and so on. So, it's similar but also different and then you can see the big influence
5:41 with the C++, Java Script, and C#, with there C heritage curly braces, semicolons, and that sort of arrangement. So here is the same program that
5:51 piece of program, piece of functionality that we wrote before in five different languages.