8. If Statement [Python 3 Programming Tutorials]
Key Takeaways
Explains the if statement in Python and its control flow
Full Transcript
today's topic is if statement in Python we are going to use py charm today because phm is nice and cool editor we will use Idol in between when you want to try simple one-line statements in Python I clicked on py charm icon it opened this dialog here create a new project and I'm going to give it a name on Python this will be my project name in C code directory you can choose whatever director you like create so just created a project for me here I am going to add a new Python file so I will say new python file if so it created f dot py file i will explain you 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 call input input will say enter in number so what this function is doing is it will ask user to it will print this text on the console so where 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 stored in this num variable so I created a gnome variable I am saying Nami cual 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 say user and five the five will be string and that file which is storing 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 each statement is if followed by up condition so to check whether the number is 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 divided by two and the remainder of that operation is zero then your number has to be even so you're just print number is even if that is not the case then it will go into this else block remember you have to enter add a colon so Collins means it's beginning of the block and you have to also indent it py Chun 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 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 breakpoint it will be highlighted by this red circle and you can save 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 5 after that it will move on to the next lines right now you are you stopped at the next line you didn't execute remaining lines yet so you see here numb is string five when you go to the next line using this particular button or by using f8 key stroke the numb from indeed from string it got converted to integer and that's because of this in function f8 again and see what happens cool so it didn't go to this line if you noticed so that's why if it's called control state when it controls the flow of your execution if the condition is not satisfied then it will go to else and again put into f8 you can close this if you go to console you now see number is odd if you do the same thing for this around debug for number four then it will go to the first line so let me add 4 here FA 8 f8 so you see now it went here it is saying number is MN cool so in the condition here we use equal to operator there are other operators which I am going to demonstrate using idle so I like to use idle always along with py charm because idle execute one line very quickly and it kind of helps in your programming so always use idle along with pycharm by time you are using to write a main program but either you are using it for experimental stuff so we saw equal to operator so when you say 4 equal to 4 it plays true if you want to say not equal to then you use this or this is called not equal to operator so 4 is not equal to 5 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 are saying 4 is not equal to 2 other kind of operators are like greater than less than so and this one is called greater than operators when you say two is greater than one it is printing yes two is less than three is spending years 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 printer so you this will be true for this condition as well similarly less than equal to we also have and an or operator so if you want to instead of one condition sometimes you want to check two conditions so you want to say 3 is greater than 2 and 4 is greater than 1 so this returns true the way n 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 2 which is true but when you say what is good in file that is false so overall result is false why because one of these guys is false another operator is or so let me try the same thing with or so it's the same exit condition and see what happens you only 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 it's 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 for equals so 4 equal 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 say raise 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 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 pick few cuisines and we'll just enter we'll create a list of all the dishes in that cuisine so I will use Indian cuisine so let's say 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 down in your hair none none is an Indian bread and you create another list for Chinese cuisine so you're creating a list here and you are storing it in a variable so Chinese cuisine is a grow potsticker and fried rice Italian is Pisa pasta risotto if you want to make a program perfect then you need to enter all the possible dishes in this list and it will be too long so they just for 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 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 this lists so if you remember from our list talk there is an in operator that you can use so we will say if dish in Indian what this means is if this third user entered belongs to this list then 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 else if statement and the key word for that is LF L if means else if if this is Chinese then print it is Chinese okay 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 else--if checks and now you are the stage where you want to write default case where you you say where is known little knowledge I have I don't know like cuisine scene 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 favourite one Indian awesome and at the third days and you say burger and sit same 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 but it's statement cool so that I think we pretty much cover all all the things in if statement we covered if LF else and how the control flows based on the evolution of the condition okay 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.
Exercise: https://github.com/codebasics/py/blob/master/Basics/Exercise/8_if/8_exercise_description.md
Topics that are covered in this Video:
0:00 Overview
1:08 input() function
1:48 int() function
5:42 Operators
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
🔖Hashtags🔖
#Ifstatement #python #ifpython #pythonifelse #ifelseinpython #pythonif #elifpython #elseinpython #pythonifelseladder #ifelsepythonexamples
Next Video:
9. For loop [Python 3 Programming Tutorials]: https://www.youtube.com/watch?v=3ykIpmAxdoY&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=10
Website: https://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 · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
Related AI Lessons
Chapters (4)
Overview
1:08
input() function
1:48
int() function
5:42
Operators
🎓
Tutor Explanation
DeepCamp AI