Keywords in python | learn keywords in one video

Keywords in python

See this video for better understanding
Or read this article

Keywords in python

welcome to our next step on our

Python Odyssey today 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 because of this they

will come up and 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 lost being aware

of the tools it supplies you with let's

start with the keyword nun nun 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 nun 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 is

none tight 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 nun type next to 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 five less than

six 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


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 uh for this

example hundred 50 is 150 centimeters so

let's say if a equals 150 print Wow

you'll just tall enough for this ride LF

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 and 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 shall

make this individual slow taller 155

this will run it through the Elif say a

equals hundred fifty-five if a equals

hundred fifty so it doesn't in this case

it jumps down to the next one

l if 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

a hundred and forty five 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 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 one

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

one so originally a will equal one once

it goes through here a will equal to

once it goes again it will equal three

once it goes again two or four 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 or 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 for 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 a 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

one two three four five six seven eight

nine ten eleven twelve thirteen fourteen

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 for

us 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 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 values 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 we're looking at 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 called

with 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 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


the next keywords we're thinking about

are import from an 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 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 day

time from date time import just the time

modules so that's what this does an

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


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 great 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 four plus four this

prints and displays to the idle shell

eight 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 great my name again

and run presenta 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 I

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

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 esteem 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 -

keyword knowledge hopefully this is

grounded use 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 cozy



Popular posts from this blog

Object oriented programming in python

what we can do with python programming

What is syntax errors in programming language