Python Tutorial - 8. If Statement

codebasics · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

Using if statement in Python for conditional execution

Full Transcript

hello today's topic is if statement in Python we are going to use py charm today because py Cham is a nice and cool editor we will use Idol in between when you want to try simple on line statements in Python I clicked on py charm icon it it open this dialogue here create a new project I'm going to give it a name lawn python this will be my project name in C code directory you can choose whatever directory you like create so it just created a project for me here I'm going to add a new python file so I will say new python file if so it created if. py file I will explain you uh if statement by working on a sample problem so let's say you want to write a program that asks user to enter a number and the program should print if the number is even or odd for this purpose we will need if statement but before we go there we need to First Take the number from user for that we will use this function called input input will say enter a number so what this function is doing is it will ask user to it will print this text on the console so whoever is running the program he will see this text and he will have to enter a number and once the number is entered it will be is stored in this num variable so I created a num variable and I'm saying num equal to input this okay once the number is entered it will be a string so you need to convert it to an integer using int function so let's if user enter five the five will be string and that five which is stored in Num when you call integer function it will convert that to an integer which is a number and it will put back again in the same variable so number will now have five now let's write if statement so the syntax of if statement is if followed by a condition so to check whether the number is or or even you need to divide it by two and find out the remainder so if you recall from our numbers episode percentage is used to figure out the remainder so if num divide by two and the remainder of that operation is zero then your number has to be even so you'll just print number is even if that is not the case then it will go into this Al block remember you have to enter add a column so column says means it's beginning of the block and you have to also indent it py Cham will indent it for you so when you say enter you see cursor is here it is not here if you have it here then your program won't work it will give you an error you will say print number is odd okay let's run this program so click on run run so here it is asking a user who is me I'm running the program and is it is asking me to enter a number so I enter five and when I hit enter I see number is odd let me run it again you can click this icon as well by the way to do the same thing okay four four is even let's debug this and see how it actually works so I will set a breakpoint here so you can use control f8 or left Mouse click to set a breako it will be highlighted by this red circle and you can say run debug when you do that the first thing is going to do is it will come here on this line and it will wait for user to enter number so here I will enter five after that it will move on to the next line so right now you are you stopped at the next line you didn't execute remaining lines yet so you see here num is string five when you go to the next line using this particular button or by using f8 keystroke the num from in uh from string it got converted to integer and that's because of this int function f8 again and see what happens cool so it didn't go to this line if you noticed so that's why if is called control statement it controls the flow of your execution if the condition is not satisfied then it will go to else and again print f8 we can close this if you go to console you now see number is OD if you do the same thing for let's run debug for number four then it will go to the first line so let me add four here f8 f8 so you see now it went here and it is saying number is even Okay cool so in the condition here we used equal to operator there are other operators which I'm going to demonstrate you uh using idle so I like to use idle always along with py Cham because idle executes one line pretty quickly and it kind of helps in your programming so always use ID along with uh py Cham py Cham you are using to write a main program program but I you are using it for experimental stuff so we saw equal to operator So when you say 4 equal to 4 it prints true if you want to say not equal to then you use this this is called not equal to operator so four is not equal to file and it is printing true but if you say 4 not equal to 4 then guess what will happen yep it says false it says false because we are lying we saying 4 is not equal to two uh other kind of operators are like greater than less than so this one is called greater than operator So when you say two is greater than one it is printing yes two is less than three is printing yes so greater than less than that is greater than equal to operator so this means if this number is either greater than or equal to the other number then print true so this will be true for this condition as well similarly less than equal to we also have U and and or operator so if you want to instead of one condition sometime you want to check two conditions so you want to say 3 is greater than two and 4 is greater than 1 so this returns true the way end works is both the conditions has to be true if at least one of these guys is false then the overall result will be false so let me just show you 3 is greater than two which is true but when you say four is greater than five that is false so overall result is false why because one of these guys is false another operator is R so let me try the same thing with r so it's the same exit condition and see what happens you have any guess on how this works so it will check for both the condition if at least one condition is true then it will return true sort of like a common sense logic another operator is not so when you want to negate a condition so not true means false So when you say 4 = so 4 = to 4 is true but when you negate you're essentially converting it from True to false and from false to True okay so that was a little demonstration on all the operators so let me close this and now let's work on another problem so you want to I will just erase it you want to write a program that asks user to enter a dish name and the program should tell you which Cuisine it is whether it's Indian Chinese Italian what kind of food it is so we need to do a couple of things here first we need to ask user to enter a dish name and that we will do using input function and the second one is we need a list of all the Cuisines not all the cuisine but we'll we'll pick few Cuisines and we'll just enter we'll create a list of all the dish is in that Cuisine so I will use Indian Cuisine so let's say so we'll make list right so for Indian Cuisine I'm going to make a list of some Indian dishes so the first one that comes to my mind is Samosa because I just love it although it is fried and not that healthy it tastes so good so Samosa you have Dal and you have Nan Nan is an Indian bread and you create another list for Chinese cuisine so you're creating a list here and you're are storing it in a variable so Chinese cuisine is AGR pot sticker and fried rice Italian is Pizza Pasta and reso we if you want to make your program perfect then you need to enter all the possible dishes in this list and it will be too long so we just for the Simplicity sake we are just trying few dishes here so now let's ask user to enter a dish name so we'll say enter a a dish name okay after user enters a dish name this variable will have the name of the dish so we now need to do a check against all these lists so if you remember from our list talk there is an in operator that you can use so we'll say if dish in Indian what this means is if the dish that user entered belongs to this list then and print that the dish is Indian okay now if this is not Indian then you need to do another check against Chinese so for that you will use lse if statement and the keyword for that is L if L if means else if if this is Chinese then print it is Chinese okay uh if it's not Chinese then you need to check if it is Italian so you will say Italian if it is not even Italian then so you were doing bunch of lse if checks and now you are at the stage page where you want to write a default case where you you will say based on little knowledge I have I don't know which Cuisine cuine is this dish okay so let's run this program okay let's enter the name of the disc so we'll say pasta cool it is saying it is an Italian Dish let's run it again and this time try Samosa my favorite one Indian awesome and the third thish and you will say burger and see saying I don't have complete knowledge of all the dishes in the world and I'm going to be honest and say I don't know okay so that was a little fun program with if statement cool so that I think we pretty much covered all all the things in if statement we covered if Alf lse and how the control flows based on the based on the evaluation of of the condition okay cool thanks for watching

Original Description

The video will describe “if” statement in python, “if control” statement, the use of “if” statement, how to create “if” statement and the flow of “if” statement. Topics that are covered in this Python Video: 0:00 Introduction 1:32 input() function 2:13 int() function 6:34 Operators Video without background music: https://youtu.be/hNddJ3_hahk Visit http://codebasicshub.com/tutorial/python/if-statement to download answers on exercises. Next Video: Python Tutorial - 9. For loop: https://www.youtube.com/watch?v=nrxA8Pkb90Y&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=10 Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Website: http://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 8 of 60

1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related AI Lessons

Chapters (4)

Introduction
1:32 input() function
2:13 int() function
6:34 Operators
Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →