Python Programming Tutorial #4 - IF/ELIF/ELSE

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

Key Takeaways

Introduces the if, elif, and else keywords in Python programming

Full Transcript

hey guys uh welcome back to the fourth video in my Python Programming tutorial Series today we're going to be talking about decisions so we're going to be using the if else and L if statements in Python so pretty much I gave you a little example last time on what these are but if condition equals true then do this sorry not his do this okay so pretty much the Syntax for the basic if statement which is what we are going to cover first is like this so if you remember my last tutorial I talked about conditions if you haven't seen that go back and look at that first because that's going to be an important part of today's lesson and you have to understand that so if condition is true then we are going to do this now let's just dig into the syntax a little bit here um so you're going to start with the keyword if then we're going to put a condition so the condition could be something like this if one is less than two if tree is equal to plant something like that okay anything that can return to true or false value you can use variable names you can say if x is equal to Y anything like that is condition and it uses a conditional operator like I talked about in the last video and then you are going to end your condition with a semicolon or sorry not a semicolon like this with a regular colon and then you're going to Simply click enter and it should tab you in one line now it's very important that you have this indentation if you have your code like this it's not going to work python reads the lines because of indentation so it's very important that you have whatever statements you want to run after the condition returns true indented properly and this will also be very important as we move further on in different tutorials and we have lots of different indentation levels within the program okay so let's do a real example now instead of uh this kind of pseudo code here so we'll start off by just getting some input from the user so let's say let's get their age okay so age is equal to input like this and then just if you put something inside of the input like this it actually gives a prompt to the user so we'll say input your age like that and then we will say if the age is equal to 16 then we will print to the screen hey your oops 16 like that okay um all right so let's go ahead and just test it out right away I know I haven't really explained it but I just want to show you how this works okay so we have input your age so I'll say 2 as my age and we can see that nothing happens okay um now I want to show you one more I hope you've caught this trick already from what I talked about in the last videos but we'll see now you notice here I'm going to put in 16. and nothing's gonna happen now the reason for that is because 16 here is different than the 16 that we get from age if you remember whenever we're getting a number or anything from the console we actually get it as a string so this 16 that we're getting actually looks like this now in Python again we have different data types right so this is an integer data type well this is a string so when we're comparing integers and strings they're different so we have to convert our age variable into an integer before we can compare it like that okay now we'll try again say if int age equals 16 so now we put 16 in and says hey you're 16. like that okay um perfect so that's pretty straightforward um now we can also do some other conditions so let's do a greater than 16 now okay so this is another conditional operator that we talked about before um and yeah we'll go ahead we'll try it now so we'll say 15. now nothing happened because obviously 15 is not greater than 16. now if we put in 17 it works okay great um all right and also uh I just want to show you there's another operator that we didn't talk about last time a conditional operator and it's the greater than or equal to and the less than or equal to so to convert your um greater than sign into a greater than or equal to sign all you have to do is add an equal sign like this and now anything greater than or equal to 16 will work whereas before it had to be strictly greater than 16 and same thing with the less than sign like that okay I just forgot to talk about in the other video so I figured I'd put it in here all right um great so let's go into another example then so let's say um it's great it's telling us we're 16. um or let's in this case we're gonna be older than 16 right so hey you're older than 16. but what if we want a message when we are younger than 16. well the way to do this is using something called an else statement so the Syntax for this uh it simply has to come after an if statement you can't just leave your own else statement like this that will not work it has to be after an if statement is placed and then all you have to do is put a colon and click enter and make sure the indentation is the same as the indentation for the first if okay so now we're just going to print U are younger than 16. okay and we can go ahead we can try this to see if the else is going to work and we say over 15 and you are younger than 16. great okay um so just to explain this a little more how this really works when we're reading through the code well python reads code line by line like this now when it reaches this if statement it checks this condition to see if it is true or false so since this condition um was false in the last example we say if false it says Okay so we're not going to run this line of code we're going to skip to the else statement and we're going to do this okay so if we had typed anything in that does not return true so makes this condition not return true then it would go to this else statement like this okay um yeah and then same thing say we type in a number 17 and that is greater than 16 it's not going to do the else statement it's just going to do the if statement okay I hope that makes sense now we're going to go into another layer okay so let's get into a new example here um let's talk about height for a roller coaster okay so for some roller coasters uh you have to be taller than a certain height and you actually can't be too tall right so again we'll get the input from the console so we'll just say height is equal to input and we're just going to do this in meters like one meter two meter three meter just to make it really easy okay I know that's not a realistic height but just for the purpose of this is example so we say height is equal to input um again we have to remember that one we're going to check the condition we have to make sure we put it in the int because as we get it from the console it's going to be a string okay so we'll say if height is less than one meter we're going to say you cannot ride so we're going to print to the screen you cannot ride okay now we only want to allow the user to ride if their height is in between one meter but less than two meters okay so as you can see I've introduced a new word here called L if all right so this means if this condition up here is false then we're going to go to this one we're going to check if this condition is true or false if it's true we're going to run whatever's in here otherwise we're going to keep going so you can have as many l ifs as you possibly want in a decision statement like this okay you can only have one else though because else is just the default so anything you type in um that doesn't equal any of these statements so like if isn't true all if isn't true the other LF isn't true then it'll go to the else statement okay so elif the height is so if it's less than one you can't ride and if it is greater than two you also can't ride so we're going to say you cannot ride I'm gonna say and we'll say over 2 meters and then we'll put here just so we can distinct them under one meter okay and then now we're going to add in the else statement like this we're going to say print you can write okay so I know I just did a lot there so we'll go through it quickly we pretty much have a condition up here we already talked about the if statement so if this condition is true we're going to print this and we're going to skip everything else we're not even going to bother reading it because we know that it's not going to be any of those conditions okay so now we do the elif here right so we say well if this condition is false we're going to go ahead and we're going to check this condition so we say oh well this one's true so then we're going to execute this line of code which is the print and we're not going to do this else statement now say we go through these two and they're both false okay so this returned false and this return false then we're going to go to the else and we're automatically just going to print whatever is in the else statement like that okay so we'll go ahead uh we'll run the program and we'll try it out make sure I haven't made any errors here okay so the input I didn't give a prompt so I'm just going to type in the height uh a number all right so we'll say one you can ride great okay that is because again we have the strictly less than sign now if we wanted it to be you have to be over one foot we just put an equal sign here and then same thing here okay so we'll do that and now I'll show you again if I put in one you cannot ride under one meter okay so less than or equal to one right okay um let's try again now we'll do it maybe in number greater than two so let's say we put four it says you cannot ride because you are over two meters and then again if we do like a decimal number maybe uh 1.1 Okay so we've run into an error there that's fine just because we can't convert a floating decimal point into an integer but yeah I hope you get the point from that example here these are just the basic if elif and else statements I'll do one more just to show you here we could do another LF here saying for example um if int height is equal to remember we do two equal signs right not one then we're going to say print wow you are tall okay so as you can see we can do infinite elf State L if statements like this and we can only have one else statement and one if statement at the beginning okay uh so we'll run this one just to show you one more time how this works I'll put in five so five uh you cannot write over two meters okay so that's because what actually happened here is we went if we checked this condition now it turns out that um 5 is greater than two so we did this one and we didn't bother reading the rest now if I wanted to change this uh so that it would check the five first all I would have to do is put this alif statement I'll do it right now above the other L if statement like this and now if I type in five it'll work wow you are tall okay so uh that's it for the if elif and L statements in the next tutorial we're going to go into something called chain conditionals and do some more advanced examples of this we're going to do some addition subtraction and some more things with other operators okay so I hope you enjoyed um please don't forget to like And subscribe the video and I'll see you again tomorrow

Original Description

This is the fourth video in my python programming series. In this video I explain how to use the if, elif and else keywords in python make decisions. Python is a great language to learn as it is widely used and fairly simple. It offers a great foundation for future programming learning. Stay tuned for more content coming everyday! Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/beginner-python-tutorials/if-elif-else/ As always please remember to LIKE and SUBSCRIBE! 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 · 5 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
Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 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 Reads

📰
Bukan Menggantikan, Ini Cara Memanfaatkan AI Terkini untuk Naikin Produktivitas Kerjamu
Learn how to leverage the latest AI technologies to boost your productivity at work
Medium · AI
📰
Rytr vs QuillBot for Academic Writing 2026: Which AI Tool Actually Saves Your Thesis?
Discover how Rytr and QuillBot can aid academic writing, and which tool can save you 12 hours on your next thesis chapter
Dev.to AI
📰
Automating Exposure and Color Corrections: AI Retouching Basics for Headshot Photographers
Learn how AI retouching can automate exposure and color corrections for headshot photographers, saving time and improving results
Dev.to AI
📰
I Built a Browser-Based YouTube Frame Extractor (No FFmpeg Required)
Extract frames from YouTube videos directly in the browser without FFmpeg, using a custom-built tool
Dev.to · H3l!0s_T3k
Up next
Announcing Lattice MCP
Lattice
Watch →