Python Day #4: Nesting Conditional Statements with Advanced Logic
Learn how to master Python conditional statements using if, else, AND, OR, and nesting. Perfect for beginners who want to write smarter, cleaner logic fast.
Welcome back to the Python Roadmap to Success, where I am giving all my readers a fair chance at learning Python the right way, following a proven roadmap that I’ve structured and taught for over the last four years.
In our last article we covered the third phase in the Roadmap to Success, it covered how to get started with Conditional Statements and Expressions.
In this lesson, we are going to expand more on managing flow in code. We are going to look at how we can create more advanced decisions, how we nest existing statements, and how we can create multiple expressions using new logical operators.
Over the last 8+ years I’ve been teaching full-time around the world which has given me the opportunity to work with over 1,500+ students. The last four years I’ve been teaching Python full-time.
I’ve spent this time curating a structured learning path that ensures my students have the greatest chance of success and don’t just learn but actually master Python as well as their logical thinking and problem solving.
Every week you’ll be introduced to a new topic in Python, think of this as a mini starter course to get you going and allow you to have a structured roadmap that actually builds to create you a solid foundation in Python.
Each week, I dive deep into Python and beyond, breaking it down into bite-sized pieces. While everyone else gets just a taste, my premium readers get the whole feast! Don't miss out on the full experience – join us today!
In this article, I’m going to assume you’ve been following along on my series, The Python Roadmap to Success and have read the previous articles.
Today I’ll touch briefly back on how computers think and then how we can expand on our last lesson to strengthen your skills, as these will continue to be the essential building blocks of your foundations in programming.
One point I should note is, spend time digesting these when I write them, after six weeks most of my posts are automatically put behind a paywall.
If you haven’t subscribed to my premium content yet, you should definitely check it out. You unlock exclusive access to all of these articles and all the code that comes with them, so you can follow along!
Plus, you’ll get access to so much more, like monthly Python projects, in-depth weekly articles, the '3 Randoms' series, and my complete archive!
I spend a lot of my week on these articles, so if you find it valuable, consider joining premium. It really helps me keep going and lets me know you’re getting something out of my work!
Thank you for allowing me to do work that I find meaningful. This is my full-time job so I hope you will support my work by joining as a premium reader today.
👉As a special Mother’s Day bonus, I am offering my readers a 30% off on all annual subscriptions! Show your mom you care about your career, thanks mom…
If you’re already a premium reader, thank you from the bottom of my heart! You can leave feedback and recommend topics and projects at the bottom of all my articles.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
The Computer Decision Making Processes
As we previously discussed, computers are not actually that smart. They only understand two things, they only understand 0 and 1. This is known as binary.
If we look at what 0 and 1 really mean, they mean No and Yes. Or as a boolean this would be False and True. A boolean is a type of data in Python that evaluates to either True or False. This was covered in our first lesson together.
Every time a program runs, it goes through continuous checks while it completes tasks. Our program then gets to a point it where it gets asked a question (expression). Is the answer to this question, True
or False
?
Based on the answer the computer knows which route to take to get to the next stage of the program. Now this is a very broken down example of how computers work, but none the less this is exactly what is happening.
But really, once the computer answers either True or False, we could then potentially ask it another question. So the computer might see something like: Is the answer a string, if yes, then I we could ask, is this string longer than 10 characters.
You see how we start with a simple question, then based on the answer we can link that into another question to allow the computer to niche down on the answer to get the projected block of code we want to run in that scenario.
The Concept of Nesting
Building on last lesson, we know a condition is simply an expression that evaluates to either True
or False
, that’s really it. Remember an expression is kind of like a question. Is this question True or is it False?
The concept of nesting I want to make rather simple for you guys. I’ll explain this in two ways. Nesting is really when we put something inside of something else. Read that again.
Something you already know at this point from going through our lessons is that we have the ability to nest functions. This not only makes our code more readable but also speeds up Python as it has less lines to read.
A simple example of this would be with input. Let’s say we want to enter a number from the keyboard, this data types should be an integer, but input only outputs a string. We could handle this in two ways.
In the first segment of my code I did this in two lines, then in the second segment I have the exact same code but this time the code is nested, the functions are inside one another.
Just like functions we can also do this with conditional statements. This allows our statements to get more specific and run different types of checks as our code gets more advanced.
👉 If you get value from this article, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Nesting Conditional Statements
Great, now that the word or concept of nesting is fresh in your minds, let’s merge this into our current topic. We can also nest conditional statements to expand on our basic statements to make them more advanced.
There are endless reasons you may want to do this. While you are learning, this is a great way to start creating a solid foundation of your skills and logical thinking as we can direct our computers to do the things we want.
The most common reason to do this would be when we want to check multiple levels of a condition. This is because sometimes, our second condition should only be checked if the first one is true. This helps us control the overall flow of our logic.
If you're handling multiple layers of logic, nesting makes the decisions easier to manage and understand for us.
So here is a rough example of nesting. Now just checking age isn’t enough—I now want to add another condition only if the first condition is True. I can nest the second check inside the first one.
First, my code checks if the person is 18 or older. Then if they are over 18, the code checks if they’re a citizen. If they’re both, they can vote. Otherwise we run our else
clause, I can give different messages depending on which condition fails.
Technically, we could even refactor this and shorten it. If we wanted we could just store all this in a single statement and eliminate the need for the nesting. But you can get the bigger picture and really see how it’s done above.
Time to introduce a new Python keyword for you all, not
. Here you can see I have refactored the original nested conditional statement. What I have written instead now translates to “Also if
this is not True
, then print this code below.”
Now keep in mind these are basic examples, but the ideas should be flowing. Here is a great time to recommend a starter project for you guys to really grasp and practice everything we have covered in these first four lessons of the Roadmap to Success.
A great starter project I often run my students through at this point is for them to create and build a chatbot using all the tools and skills they’ve learned from the first four lessons.
This project is great because you will need to use everything we have talked about so far from data types, variables, functions, and conditional statements!
So give it a try, for example, I like travel. Therefore I could build a chatbot with conditional statements that will recommend me a place to travel based on a series of questions and answers.
You should use a lot of nesting here as this will be a great opportunity to practice fro you guys!
👉 If you get value from this article, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Stop Struggling—Get Fluent in Python Faster!
Most people waste months bouncing between tutorials and still feel lost. That is not going to happen to you, at least not on my watch!
👉 I’m giving you my exact system that’s been proven and tested by over 1,500 students over the last 4+ years.
My Python Masterclass gives you a clear roadmap, hands-on practice, and expert support—so you can gain Python fluency faster and with confidence.
Here’s What You Get:
✅ 135+ step-by-step lessons that make learning easy
✅ Live Q&A & 1-on-1 coaching (limited spots!)
✅ A private community so you’re never stuck
✅ Interactive tests & study guides to keep you on track
No more wasted time. No more confusion. Just real progress.
Take your career to new heights—secure your spot today!
P.S - Save over 20% with the Code: learnpython20
🎁 - Get a free one-on-one coaching session with any course!
Still hesitant?
Go through the entire course, complete all material, and attend the Q&A's, if you still feel like your struggling I'll personally work with you one-on-one until you're confident!
Expanding Conditions with Logic
Alright, finally we can talk about two simple but super useful keywords we have Python for working with logic: and
and or
.
These are like connectors. They help you check more than one thing at a time in your code—all in one line. That way, our logic can be more flexible and feel more like how we talk in real life.
I’ll start with the and
keyword. In regular English, when you say “and,” it means both things need to be true. Same deal here in Python. If you use and
, then both parts of your condition must be true for the whole thing to work.
Here’s what’s going on now in the code above. First, Python checks if the person is 18 or older. Then it also checks if they’re a citizen. If both of these are true, it prints the message that they can vote. If even one is false, it shows the else
message.
Now we can look at the or
keyword. The word or
means just one of the things has to be true. If at least one condition is true, then the whole thing counts as true.
In this case, even though has_password
is False, the user is an admin—so Python says, “Cool, one of these is true,” and gives access.
Long story short, and
means both must be true. or
means at least one must be true. You’ll be using these a lot, so getting comfortable with them now will really help later on.
Oh also, these will come handy while you are creating that project I spoke about above, try to use these in there too!
👉 If you get value from this article, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
My Best Starter Resources
Here are the best resources I have to offer to get you started with Python no matter your background! Check these out as they’re bound to maximize your growth in the field.
Code with Josh: This is my YouTube channel where I post videos every week designed to help break things down and help you grow.
Zero to Knowing: My Python Masterclass platform designed from 8 years teaching experience and that has helped over 1,500+ students succeed with Python. (All my readers gain a special 20% discount, use code: learnpython20)
My Books: Maybe you’re looking to get a bit more advanced in Python. I’ve written 3 books to help with that, from Data Analytics, to SQL all the way to Machine Learning.
The Nerd Nook: This is where you are right now! I write here multiple times per week with the sole goal of breaking down this larger than life topic to help you excel and learn Python faster than you thought was possible.
This article will be a piece to a larger roadmap but will go behind a paywall after 6 weeks! To help support my work in teaching others consider joining as a premium reader to unlock all these articles, monthly Python projects and in-depth dives.
Wrapping Up Lesson Four
By now, you should have a pretty solid understanding of how nesting works, and how and
and or
can help you write more powerful and flexible conditions in your code.
These are the kinds of tools that take your basic logic and let you build way more interesting and real-world programs.
Whether you’re creating a chatbot, building a game, or making some kind of smart app—these concepts are the building blocks that help you control what your code does, and when it does it.
As always, the best way to get better at this is to practice. Play around with different conditions, mix in and
, or
, and not
, and try building a little project like the travel chatbot I mentioned earlier.
Don’t worry about making it perfect—just start small and keep adding to it. You’ve got this. Every line of code you write is a step forward.
Let’s keep moving forward with Day #5 next week.
Hope you all have an amazing week nerds ~ Josh (Chief Nerd Officer 🤓)
👉 If you’ve been enjoying these lessons, consider subscribing to the premium version. You’ll get full access to all my past and future articles, all the code examples, extra Python projects, and more.