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