8. If Statement [Python 3 Programming Tutorials]

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

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 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
8 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)

Overview
1:08 input() function
1:48 int() function
5:42 Operators
Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →