FactorPad
Build a Better Process

Introducing Python Keywords

Eventually you will memorize all 33 and here we get started with the first 5 for beginners.
  1. Review help - See how to find help on your version of Python.
  2. Operators - Learn how keywords combined with operators create program flow.
  3. True, False - Punch 13 fun examples into your Python interpreter.
  4. and, or, not - Practice with another 11 examples and see the rules.
  5. Next: text - Introduce Python functions for text strings.
face pic by Paul Alan Davis, CFA
Updated: February 21, 2021
Professionals learn programming at their keyboard which reinforces the concepts better than simply reading.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / python-keywords.html


An ad-free and cookie-free website.


Start learning the 33 Python keywords in version 3

Beginner

Video Tutorial

Videos can also be accessed from our Full Stack Playlist 3 on YouTube.

Introducing Python keywords | Python for Beginners (4:52)

Code Examples and Video Script

Welcome. Today's question: What are the top 5 keywords for beginners?

I'm Paul, and I create five minute videos to help people help themselves, because it's so easy to forget words in a new language.

So here we will find that list of keywords from the last tutorial, and start exploring these reserved words in Python, like operators.

Starting with True and False, the focal point of conditional logic in programming.

Then we will play with and, or, not operators before moving on to text functions.

(Commands in Linux)

(Keywords and functions in Python)

Step 1 - Review Where to Find Python Help

Let's head to the Linux Terminal, and currently I'm using Python version 3.4.2.

paul@fullstack:~$ python3 -V Python 3.4.2
Locate help on python.org

Online documentation can be found at https://www.python.org/doc/versions/ by version number.

See all 33 keywords

Now let's find those keywords locally.

paul@fullstack:~$ less notes/python_keywords.txt

Here is a file with the 33 Python keywords, and I'll use this file in the future so we can keep track of our progress, including five in this tutorial (number 29).

Python 3.4.2 33 Keywords False (29) def if raise None del import return True (29) elif in try and (29) else is while as except lambda with assert finally nonlocal yield break for not (29) class from or (29) continue global pass Access help documentation from within Python using: 1) Interactive help >>> help() help> keywords 2) Object help >>> help('keywords') notes/python_keywords.txt (END)

Below that are instructions for how to access this list in Python yourself, which is often less distracting than a Google search, right?

Find help on the local machine

The last tutorial demonstrated how to access documentation on the local machine using interactive help and object help.

Run python3, then type help to identify the two types.

paul@fullstack:~$ python3 Python 3.4.2 (default, Oct 8 2014, 10:45:20) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> help Type help() for interactive help, or help(object) for help about object. >>>

First, interactive help, using help() like this and it gives you a different command prompt.

>>> help() Welcome to Python 3.4's help utility! If this is your first time using Python, you should definitely check out the tutorial on the Internet at http://docs.python.org/3.4/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". help>

Step 2 - Finding Keywords is Similar to Finding Operators

Type keywords and there they are in alphabetical order, the first three starting with capital letters.

help> keywords Here is a list of the Python keywords. Enter any keyword to get more help. False def if raise None del import return True elif in try and else is while as except lambda with assert finally nonlocal yield break for not class from or continue global pass help> quit You are now leaving help and returning to the Python interpreter. If you want to ask for help on a particular object directly from the interpreter, you can type "help(object)". Executing "help('string')" has the same effect as typing a particular string at the help> prompt. >>>

To return to a Python prompt, type quit.

Now, object mode, accessed from here, using help and an object like a function name (type).

>>> help(type) Help on class type in module builtins: class type(object) | type(object_or_name), bases, dict) | type(object) -> the object's type | type(name, bases, dict) -> a new type | | Methods defined here: | __call__(self, /, *args, *kwargs) | Call self as a function. | | __delattr__(self, name, /) | Implement delattr(self, name). | | __dir__(...) | __dir__() -> list | specializedd __dir__ implementation for types | | __getattribute__(self, name). | Return getattr(self, name). | | __init__(self, /, *args, **kwargs) | Initialize self. See help(type(self)) for accurate signature. | | __instancecheck__(...) | __instancecheck__() -> bool | check if an object is an instance | :

(Type q to return to the Python prompt).

Or a topic like 'keywords' in single quotes.

>>> help('keywords') Here is a list of the Python keywords. Enter any keyword to get more help. False def if raise None del import return True elif in try and else is while as except lambda with assert finally nonlocal yield break for not class from or continue global pass >>>

Very good.

Step 3 - Introducing Python Keywords: True, False

So what should we know about reserved words called keywords?

Three rules about Python keywords

First, they must be used with exact spelling, so True is True.

>>> True True

But this true creates an error.

>>> true Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'true' is not defined

Second, you don't want to name variables using keywords, so yes, while 'the sky is blue' is true, lowercase true.

>>> true = 'the sky is blue' >>> true 'the sky is blue'

We can't name it True, capital True or Python yells at you.

>>> True = 'the sky is blue' File "<stdin>", line 1 SyntaxError: can't assign to keyword

And third, which illustrates why compatibility can be an issue, keywords do change across versions in Python. Keep that in mind.

Boolean values True and False result from comparisons

So True and False are Boolean values, or outcomes from comparison or relational operators in Python.

Logical programs follow this True and False path, and they translate to number, right?

So True is True and False is False.

>>> True True >>> False False

Does True == 1?

>>> True == 1 True

Ah, so then False == 0, right?

>>> False == 0 True

And here's an odd one, what is True + True.

>>> True + True 2

Not four, it's two.

And we went through an exercise with operators like this back in tutorial 24. Remember 1 < 2?

>>> 1 < 2 True

Then 1 is not equal to 2.

>>> 1 != 2 True

So if we did 1 is less than False.

>>> 1 < False False

We get False.

And 1 is greater than False.

>>> 1 > False True

We get True.

So the takeaway, True is 1, False is 0.

Step 4 - Introducing Python Keywords: and, or, not

The Python 'and' keyword

Let's move on to and, which means that all operands must be True in order for the whole statement to be True.

>>> True and True True

So True and True is True.

>>> True and False False >>> False and True False

True and False is False. False and True is False.

What is False and False?

>>> False and False False

It is False.

The Python 'or' keyword

Next is the or keyword. Here the whole statement is True if any of the operands are True.

So True or True is True, True or False is True, False or True is True.

>>> True or True True >>> True or False True >>> False or True True

What is False or False?

>>> False or False False

It's False. We can't overthink it.

The Python 'not' keyword

Finally not goes like this.

If it's not True it's False. That's basic. And if it's not False it's True.

>>> not True False >>> not False True

And if you wanted to program like a lawyer, and confuse everyone, you might do a not not not False, ;).

>>> not not not False True
Three tables summarize the rules in Python

So to help, here are a few tables. Stick with it until you can generate these from memory.

Python tables for and, or, not operators (video 29) 1. The 'and' table ---------------------------------- | Expression | Answer | ---------------------------------- | True and True | True | ---------------------------------- | True and False | False | ---------------------------------- | False and True | False | ---------------------------------- | False and False | False | ---------------------------------- 2. The 'or' table ---------------------------------- | Expression | Answer | ---------------------------------- | True or True | True | ---------------------------------- | True or False | True | ---------------------------------- | False or True | True | ---------------------------------- | False or False | False | ---------------------------------- 3. The 'not' table ---------------------------------- | Expression | Answer | ---------------------------------- | not True | False | ---------------------------------- | not False | True | ----------------------------------
Test your knowledge of Python 3 operators and keywords

Below is a homework problem. Pause here and write it down.

Homework Problem: Will it return True or False? (7 % 3) <= (7 // 3) and int(9 / 3) != (9 / 3) 1) Start with a pencil and paper 2) Verify with Python

Step 5 - Next: Text functions in Python

Here is where we are heading on our journey, using this software stack.

You are invited to join, simply Subscribe to get new videos and up next we cover functions used for text.

Have a nice day.


What's Next?

For reminders of new content follow @factorpad on Twitter, our email list or subscribe on YouTube.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / python-keywords.html


python keywords
python true
python false
python and
python or
python not
python homework
python3
find python keywords list
list of python keywords
python rules
python logical rules
python examples
python 3 keywords
python tutorial

A newly-updated free resource. Connect and refer a friend today.