Keywords are the nuts and bolts vocabulary of the Python programming language. They serve as fundamental building blocks and tools for communication between you and your computing device. This guide will introduce an overview of all keywords within Python, provide an understanding of what exactly a keyword is and how to make simple codes with them. So come along on the next step of understanding for your programming escapades with Python.
Hey gang! I'm Tim from Core Electronics and welcome to our next step on our Python Odyssey. Today we will be nose diving headfirst into all the keywords within Python and simple code examples for each of them. Keywords are the nuts-and-bolts vocabulary of any programming language, these are the reserved words that make up the syntax and vocabulary of programming languages, they serve as a fundamental building block and tools for communication between you and your computing device (such as a Raspberry Pi or similar development boards) because of this they will come up in basically every piece of code you ever write a key word is a combination of letters usually forming a word, that has a special meaning this special meaning can be command or parameters which will be executed as soon as the computing device encounters the key word when the code is run, this is the reason keywords are reserved by programming languages, reserved means that they cannot be used in defining variables, classes or functions therefore key words are often referred to as reserved names. They are also case sensitive.
By having a firm grasp with the scope of possibilities that key words bring to the table you'll be better equipped in your daring voyages through the Python programming landscape, so let's get into it. Starting off let me introduce you to the reference piece on our Core Electronics website, this over here will give you a table of every keyword in Python and a description of their purposes in layman's terms, which could be found right here they're also loosely organized with a programming beginner in mind with keywords most likely to encounter or utilized found at the top whereas the keywords least likely to be found or utilized down at the bottom.
Now it's worth understanding that a number of these keywords have great depth to them when utilized in code such as those associated looping structures and this greater depth will be investigated in further tutorials. Looping structures allow you to run one or more lines of code repetitively but this guide is more so you can feel grounded in the lingo and feeling of Python whilst being aware of the tools it supplies you with. Let's start with the keyword “none”, “none” is useful as it represents a null value or no value at all it's important to realize that this is different than a zero it's not an empty string and it's not a Boolean variable type, only “none” can be “none” and because of it, it has its own data type so on the screen right now you can see I've made variable called A and I've given it’s “none” type so if I was to go print type A you'd be able to see the type of “none” yes I want to save it and you'll see it's classed as a “none” type.
Next are the keywords “true” and “false” these keywords are the two Boolean values found in Python when you're talking Boolean, it's either a statement is "true" or the statement is "false" now note that there's a capital letter for both these keywords so also worth noting the "true" keyword is the same as one whereas the "false" keyword is the same as zero, so this is them used as keywords in a program but they also display as a result of a comparison operator. So right here as a comparison operator, is 5 less than 6? So if I was to run this program over here, print five is less than six will come up as "true" because that's a "true" statement.
The next key words were going to look at is if statements and also including the keywords “elif” and “else”, these keywords have a lot of depth to them and deserve being investigated significantly more than what I will through this overview, so these keywords are used in conditional statements, “elif” is shorthand for “else if”, the “else” keyword imagine if not this then do this that's the kind of mindset so these keywords are all about asking a computer a question about a variable or data and then based on the results of that question performing a certain task, I have over here an example of this on the computer so let's say A equals 150 and we'll say for this example 150 is 150 centimetres so let's say if A equals 150, print “Wow you'll just tall enough for this ride” “elif” so, "else" if, if a is greater than 150 you'll print to the shell “you're good to go on this ride kid” “else” print “sorry kid you'll just not tall enough for this ride” it's like every time you go to the carnival when you're a child, so if I was to run this when it has 150 the computer is going to go to the if statement see a equals exactly 150 and print that result so I'll demonstrate this right now. Boom “Wow you're just tall enough for this ride” however if I were to make this individual slightly taller 155 this will run it through the “elif” say a equal 155 if a equals 150 so it doesn't in this case it jumps down to the next one, “elif” a greater than 150 so we'll see what that does naturally it will print “you're good to go on this ride kid” great feeling as a kid and the worst feeling as a kid is when you're not tall enough for the ride, so if I was to say A equals 145, run this, save it, “sorry kid you're not tall enough for this ride” hopefully that gives you a brief understanding of if “elif” and “else” statements and also the syntax with Python when using this kind of key words.
While key words are next these have to do with looping and this concept of looping will be investigated in further tutorials so the "while" loop lets you create a repeating set of statements so long as a condition remains true, on screen now is a code example of a “while” keyword so following this through I've set up a variable called A equals 1 and while A is less than 15 print A so that will print it to the shell and then the next step will be A equals A plus 1, so originally A will equal 1 once it goes through here, A will equal 2, once it goes again it will equal 3 once it goes 4 and this will continue until it gets to 15, so for us to run this module you'll see starts at 1 and follows all the way up through to 14, once it gets to 15 the "while loop" is complete and the code ends so this code gives a good idea of simple syntax for a looping structure.
More keywords now, these are another looping structure which will let you execute a set of statements once for each item in a list, so I've given a simple example of four loops in the code on the computer so this code will add up every "element" in a list so starting up here I made a variable called B which is a list and it has all these numbers in it I've also set up another variable called total, total currently is 0, so 4 this is the keyword “element”, so “element” is when you're talking about less an element is each singular piece of data in that list so for each element in this variable B the total this value becomes the total plus element. So what's effectively happening is every item in this list is being summed together so once this full loop is complete, print the total and also print the type of B, so if I run this module you'll see the sum of all these numbers is 44 and the class type or the type of this variable B is a list so this code gives a good idea of the simple syntax for a full loop.
The next key words worth talking about are “continue” keyword, “break” keyword and the "pass" keyword “continue” allow you to stop the current iteration and continue the cut to the next section of the code, “break” lets you break out of a loop even if the while or false condition is still true and passes like a null statement so here on the screen is an example of break being used in code to prevent the iteration occurring if A is a negative number so currently A equals 1 so while A is less than 15 added complexity an if statement a a less than zero “break” this is the new keyword print A equals A plus 1 so what's going to happen is this while loop is going to go round around and round until it gets up to 15 and then stop so if I run this code right now you'll be able to see A printed all the way up to the value of 15 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, all the way up to 15 so if however A started at a negative number this will run A equals negative one while A less than fifteen if a is less than zero, "break" what this will happen is it will stop the code, and nothing will be printed so this is how you can use "break" in a simple code, you can use the same syntax to put "pass" and "continue" into this code, if it's a negative one value and instead of using "break" I decided to instead use "continue" what's going to happen is this is a negative one value and the continue will effectively do the same thing as break and it will get you out of this loop, if I was to change continue to "pass" and run it like this you'll see that the numbers still get added up that's because "pass" just effectively goes on to the next step so A equals negative one while A less than fifteen if A less than zero and it is in this case then "pass", so it just goes on to print A equals plus one and then it goes back to the beginning of the wildly, the "break", "continue" and "pass" statements in Python will allow you to use "for" loops and "while" loops much more effectively in your code.
The next key words worth looking at are "in" and "not in", these are methods to check to see if a value is present in an object or if the value is not in an object these return Boolean true and false value. The code example I brought you can see has a string variable and A list variable so print A and B so in this case a is gonna be so the value for A is gonna be looked for inside B and because A shares orange with B that will print true. Now print A not in B because A is present inside B, that will return false so if I run this right now, true for this one, false for that one.
The next keywords worth looking at are “is” and “is not” these are two methods to test to see if two variables are equal or not equal, they return Boolean true and false values so looking at the code example I brought along you can see I've created a variable with a string literal, and the string literal is banana and the list I have here has some numbers and also has banana, so print A is B because A is not B, this will print “false” and print “A is not B” because A is not B that will print “true” and that's what you can see down here now if I has to change B to only banana, you'll now see print A is B to be true and print A is not B to be false.
Okay next keywords are “and”, “or” and “not” these are all logical operators and our demonstrating code how they function and how to use them with Python syntax and here we will return true because 5 is greater than 4 and 5 is less than 10, over here B, 5 is greater than 9 that's not true but 5 is less than 10 so print B, will be true because one of these statements is correct so not gives the reverse or whatever the Boolean result is so if I was to make a variable C with the Boolean false by printing “not” to see that will in fact print “true” so running the code you'll see that all of these things come out true.
The next keywords we're thinking about are “import”, “from” and “as”, so “import” allows you to import a module and consider the module to be the same as the code library, a module is a file containing a set of functions using the “from” keyword allows you to import the specific parts of the module and not all of it, the as keyword can be used to create different names for things, so jumping into the code import “date/time”, so “date/time” is one of these libraries full of modules so if you did that you get everything out of the “date/time” from “date/time” import just the time modules so that's what this does and import “date/time” as A, so that means if you ever referred to “date/time” in your code you can just type A, so first to run there's nothing will pop up but all those things would be happening in the background.
Now for “class” keyword a “class” is like a blueprint for creating objects and this keyword is used to define classes, the code for this has created a class called A class and inside it there is a property named B, this is a keyword with a lot of depth so more information about it will be dived into in later content.
“Def” and “return” these are both keywords used in the creation of functions, so a function is a group of statements that perform a specific task for example looking at the code in the screen, this is a method of getting the user to type the function and their name into the idle shell and then it will print “hello 'person's name' good morning” let me demonstrate. So the function is running in the background so if I'm gonna type “greet me Tim” the computer then responds with “hello Tim good morning” so in this code, the definition of the function, that's the functions name greet and then name this is where you type in what you want to say, print “hello name” that's where you've written your name “good morning” now this return 4 4 this prints and displays to the idle shell 8 because that's necessarily what's happening but see what happens when I move return to before the print, and now press run, everything seems as normal however if I type “greet my name” again and run, press enter now it only displays 8 so this gives an idea of the codes flow so “def” greet that's the function name that we gave name return 4 4 so when you, when return 4 4 happens after that point all of this is ignored and the code line the code finishes, so this return 4 4 ends up here, down here and you never the code never gets the opportunity to get to print “hello your name good morning”.
Moving on, “del” is shorthand for delete and is a simple keyword which you can use to remove the value of variable as you can see with the code over here when I run it the code is deleted the variable A and it produces a syntax error which you can see here, that's because if you follow through the code A equals 42 that's a variable created delete that A variable and then print A
"Lambda" is an interesting keyword it can allow you to create a small anonymous function that can take up any number of arguments, but can only have one expression a code example should clear this up and also demonstrate the syntax when using this keyword jumping into the computer you'll be able to see this over, here this will produce a value of 18 and it makes it very easy to vary results printed so A equals lambda B, B plus 10 print A bracket 8 so effectively B is now given the value of a so running this gives 18.
"Raised” keyword let's you've raised an exception it's useful in looping structures if the information being looped doesn't meet your desired criteria so you can define what kind of error to raise and also the text to print to the user, so check out the computer example, so x equals negative 1 if X is less than 0 "raise" a type error sorry no numbers below 0 so if you didn't want to type error you can instead have an exception error so if I was to replace type error with exception and ran the code now you'll still get an error issue but it'll be a different type of error issue.
“Try”, “accept”, and “finally” are all related keywords for use when testing code, the "try" keyword lets you test a block of code for errors, the "accept" keyword lets you handle the error and “finally” keyword lets you execute the code regardless of the result of the try and accept keywords this is the kind of syntax you'll see when using these keywords. Imagine this is part of a larger code block and then hopefully you'll be able to see how these could be useful as a way of testing code.
So there are a couple other keywords the keywords “global” and “non-local” are useful at higher levels of python so “global” declares a variable as a global from a non-global scope effectively this allows the variable to be used outside of the function it was created in. “Non-local” variables are declared using the non-local keyword these are used to work with variables inside nested functions when the variable shouldn't belong to the inner function nested functions are functions within functions and finally the last key words are “async” and “await” these are complex high-level keywords and are used when multiple lines of code or run simultaneously, this is effectively the computer multitasking and is referred to in Python and other programming languages as co-routines it's too complex for this overview but it's worthwhile knowing that and that's all of them, going through all these keywords should prevent any surprises in the future and cement yourself with a complete overarching python-keyword knowledge hopefully this has grounded you significantly more and prevented you from being caught off guard and losing motivation because of it, instead I hope you're keeping excited by the possibilities laid out for you, until next time stay cosy.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.