Python Programming Tutorial #5 - Chained Conditionals and Nested Statements

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

Explains chained conditionals and nested statements in Python programming

Full Transcript

hey guys uh welcome back to another video this is the fifth video in my Python Programming series and today we're going to be talking about chained conditionals and nested if statements um so pretty much chained conditionals are just adding multiple conditions in one uh one line with using words like and and or okay uh we also have keyword not that we're going to talk about as well so if you haven't seen the last videos in my series go ahead and get watch those now because they are prerequisite for this video all right so let's get right ahead and start um so if you remember before we have something called conditions so for example if we have the variable X I'll set that to two and the variable Y which is equal to three we could have a condition like x equal equal y now obviously we know from the last video that this is going to give us a value of false oops and I cannot type today sorry um and that is because 2 is not equal to three pretty straightforward I hope you have all understood that from the last video all right so what we want to do in this now is we're going to want to check multiple conditions um so we'll put a condition here so if x is equal to Y and then we can put a word in like this the andw um now we'll add another condition so if x + y is equal to five okay so this first condition is going to give us a value of false so if X is equal to Y because they are not the same but now x + y does indeed equal 5 so that is going to give us a value of true so where I'm highlighting we have true and then where I'm highlighting now again we have false so because we have the keyword and this if statement is not going to run so I'll put a print here just to show you we're going to print out true or let's just say ran and you'll see when I run the program nothing is going to happen that is because when we use the and keyword it means both conditions on either side must be true so if we change this to something like if Y is equal to 3 now that is going to be true and we'll print out ran okay so that's pretty basic now let's do the or keyword so we'll put or right here and we'll change this condition back to if Y is equal to X and now take a guess on what you think is going to happen well pretty much with the or keyword it means either of the conditions have to be true so if Y is equal to X or x + y is equal to 5 then we are going to run this line now since x + y does equal five we have a true here and again Y is not equal to X so we have a false and you'll see that the program is going to run that's because only one of the condition conditions has to be true for it to run Okay so so that is the and in the OR keyword they are pretty straightforward and pretty basic now I'd like to point out that you can actually add as many conditions as you'd like so now I can have an or I can have an and um I could have another condition I could do as many as I possibly can imagine as many as I'd like to do um so now we'll put an lse statement in here and we'll change this just a little bit to use another keyword we'll just put a sad face here to show that we didn't run it that we ran the else okay um so now there's something called the not word okay so we're going to put not right here around it and then I will show you what it does so pretty much notot reverses anything that you have inside of the brackets of the knot so in here we have if Y is equal to X or x + y is equal to 5 so we know again that this gives us a value of true um so now since true is inside of the not true becomes false now we have if false um so obviously that's going to bring us to the else statement um which is going to print the sad face so I'll show you that that does indeed work so we have the sad face right now if we change these so that both of these conditions are false you'll see that we have false false um which is going to give us an overall value of false um and then we're going to have the not which is going to change that into a true and will allow us to run it just like that ran um so yeah so that's how the not the end and the or work uh and now let's get into nested Loops or sorry not nested Loops Nest nested if statements so we'll do a basic if statement here if x is equal to 2 um if Y is equal to 3 um and then we'll add our El's like this and we'll put some print statements in here okay uh just give me a second while I type out the print statements and then we will go into what this is going to do x = 2 and Y = 3 um so you may have guessed already but pretty much we can actually add as many if statements embedded or nested as we'd like so I could add another if statement here and just constantly keep checking um now the reason I showed you chain conditionals is because if I didn't want to go through the hassle of adding all of these uh nested statements I could just do an and right here and check for another condition but sometimes we do want to do uh nested statements so we'll say here x = 2 y does not equal uh what is it three okay so uh I know I just did that pretty quickly but let's walk through it now so we have if x is equal to 2 then we're going to run whatever is in here because it is indented uh so now we come in here and we say well is y equal to 3 let's say it is now we're going to print out this statement and we're going to be done with this loop with this uh bit of code so we're going to skip down to the end of the program where we have nothing else um then so let's say we do another example X is equal to 2 but y equal 4 so that means we're actually going to run the else which means now we've got print X is equal to 2 but y does not equal 3 and then the last case is X is not equal to 2 so we don't even bother checking if Y is equal to 3 we just go down and we print X does not equal 2 so we'll change around the variables and we'll show you how this works just very quickly so we can see X is equal to 2 and Y is equal to 3 that is because obviously up here two and three now if I want to change Y and we change it to four we get X is equal to 2 but Y is not equal to three so that means we ran this one in here now let's change it so that X is equal to 4 as well and we can see we get X does not equal 2 all right so that's uh the basics on nested statements and chained conditionals I hope you found this video helpful um stay tuned for more videos we're going to be getting into some more advanced topics and in the next video we're going to be covering Loops um yeah so if you like the video uh please subscribe and like and I'll see you in the next one

Original Description

This is the fifth video in my python programming tutorial series. In this video I talk about chained conditionals and nested statements(if/else/elif). If you missed the last video go watch that first! As always please remembers to LIKE and SUBSCRIBE! Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/beginner-python-tutorials/chained-conditionals-nested-statements/ Last Video: https://www.youtube.com/watch?v=vA4r_MPRNsg&t=15s Video Tags: python,python tutorial,python language,python full course,python course,learn python,learn python programming,python tutorial for beginners,python tutorial 2018,python programming tutorial,python programming language,software development,programming tutorial,freecodecamp
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 6 of 60

1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →