Python Tutorial - 8. If Statement
Skills:
Python for Data70%
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
2
3
4
5
6
7
▶
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
More on: Python for Data
View skill →Related AI Lessons
Chapters (4)
Introduction
1:32
input() function
2:13
int() function
6:34
Operators
🎓
Tutor Explanation
DeepCamp AI