#10 How To Debug Python Program using PyCharm- Python Tutorials For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is mukherjuani once again from learn hyphen automation.com today in this video we are going to talk about how we can debug your python program in pycharm debugging is one of the most important part for any other programming language so whether you're working with python java c doesn't matter you should know the debugging because debugging will help you to debug your program easily and you will actually get to know what exactly is going wrong with your code okay so right now what we are doing we are just writing one or two lines of codes we are able to see the output but let's assume that you're working in a real-time project and your program is failing now okay let's say it is filling because of some exceptions so how do you get to know that what is going wrong with my code or what is wrong with this statement so you can actually debug your code with the help of debugger and you can easily rectify what is going wrong okay so i will show you with few examples and don't worry once you move it once you start working with complicated topics or rivals scenarios again i will be using this debugging multiple times so don't worry as of now just understand the basic uh principle of debugging how to debug your programs how to watch the variables okay and once you move it definitely we will be using this for multiple examples again debugging in python is very easy so what i will do let me create a python file first so that i can show you a basic debugging so just go ahead and create a new python file and here i will say debug demo okay let's create some variables so i will say x equal to 10 okay and let's say y equal to 20 again i'm using a very basic example next example will include some more uh different kind of data types as of now i'm taking both numerous integers and finally i will say the sum and this sum includes let's say x plus y makes sense and now if i print sum i should get the output this is what we are doing so far right the moment i say right click run as debug demo and we are getting the output perfect now let's say i want to see what is going on within the statement i want to capture the internal state so how can i do that so in order to start debugging you need to add a break point so using breakpoint you can tell exactly at which statement to stop okay so it will stop at that particular statement then you can debug your program so in our case what we can do let me add the breakpoint at statement number one itself so you can see i have this for statement one two three four in case these numbers are not coming for you just right click here okay and you can see this check box right so if i say uncheck i don't get these options so i i would highly recommend in case if you don't have this line numbers just right click and click on show line numbers it will make your debugging very easy in order to add the debug breakpoint you just need to do a click once okay and you can see this a red circle right this is a break point so now when you execute your program it will stop here and then you can you know execute your program statement by statement so in order to debug you could just do the right click and say debug and the moment you execute and you can see your program stopped at this statement okay so yes you can see right now this special variables the moment you open uh this you can see these are the spatial variables which is internally coming from python right now it's not showing the local variables that we created okay so don't worry as of now don't expand this this is just for your information in case if you want to know these are special variables get loaded the moment you run your program now if i have to execute the next statement you can see i have an option called step over okay i can just do the step over and you can see it will execute the current statement and it will move to the next and you can see we got one value which is of integer type and value is 10. can you see so x is the variable name it is a type of integer and value is 10. same thing if i say step over now i got y as well which is again y is a variable type of integer value is 20. the moment i say sum you can see i got uh some variable now again value is 30 type of integer the moment is execute again now it will execute this sprint okay so you can just go to console and verify the output is 30. now let's change the values and see how it gives a different output okay so in this case if i say 20 dot let's say 15.5 okay and let's debug it once again so again i will say right click debug as and you can see now the debugger started again so let's execute i will say step 4 i got value as 10 type of integer let's execute the next statement now you can see the value right internally it is telling you that it's a value kind of float value is 15.5 and the variable name is y and now as we know when we combine or when we do the addition of integer with float we get the final value is slowed right so let me execute it once more and you can say finally we have variable as some type of float and the value is 25.5 so you can see now how internally we're able to see what kind of value it has and what the value is right now it's very straightforward because we are hard coding the values right but once you move ahead definitely you will be getting these values from a different file or from a different function or from a different class itself in that case it will be very helpful because you will be able to see what kind of values we are getting from the different files or from the different sources and it will make a debugging task little easy as of now hard coded values but yeah you will get these values from a different sources as well once you move it as of now just understand the basic principle of debugging i hope it is clear now let me take one more example that will again make your task little easy so let me take some numbers i will say num1 and i will be using now input so input we already discussed right that it will take the input from the user so let's say i will say please enter number one okay and i will be taking input from the user same thing i will be doing for num2 as well so i will just copy and paste i will just submit the previous debugger and just say number two once we get the numbers i will just say sum okay and i will say num1 plus num2 and finally i will be adding this value and i will say the value is and finally i will combine with sum yeah let's execute i will just remove this and i will add the breakpoint at line number six and let's execute okay so these are the variables and that is anyway showing because this statement got executed right we also executed print sum so if you come back to console you can see that we got the value but now the moment i come back here it stopped here at num1 the moment i say step over or f8 right you can come here and you can see it is asking please enter your number one i will say let's say 90 okay so it is asking the next number okay so we need to exactly do this step over again now it is giving please enter number two let's say 89 yeah so now these two statement got executed so if i come back to debugger can you see i got num1 okay so we did a small mistake here guys we haven't given num2 okay so it was overriding the value so let me do one thing let's uh stop this debugger and execute it once again okay so now it is asking please enter number one let me do this yeah so we come here and we say number any number let's say 12 now it is coming to the next statement again i will say step over it is asking please enter number two let's say 5 6 and if i come back to debugger now you can see we got num1 which is string now because here's a catch because the movement you get values from the input method it will always return your string right so i have 12 which is string then i have num 2 which is again type of string the moment is a string with string it will do the concat operation right the moment i execute this okay execute once again and can you see we got the value as one two five six okay so which is not even uh what we expected we thought it will add the numbers and we'll get the output but because of this integers sorry the string part it is just doing the concat operation now here comes the debugging part because the moment you start debugging you will understand that it's a string when we are combining two string we're getting one final string so what we can do we can just do now uh typecasting we will convert this string into integer and then we can do the final addition right so let me just show you once again uh so in order to typecast what i will do i can typecast directly here or i can type cast in the next statement it's up to you so let me do one thing let me typecast here it will make little uh task easy so i will say num1 again i will i can use the same variable because of dynamic typing right what i will say convert this num1 into integer okay same thing i will do here i will say num2 and we want to convert num2 also enough in an integer so i will say num2 here so earlier it was string in the next statement i will be converting into integer same thing i will get num2 as a string i will be converting into integer now integer with integer i will get final integer and now it should also give me the final output but again as we know that we cannot add or we cannot concat string with integer so again we need to do a small casting here that we will do okay but let me execute till here at least so right click debug okay let me execute this step over come back to here let's say number is 67 execute once again okay and let's say 90. now you come back here and just go to debugger can you see now number one is already converted into integer so it says num1 now is 67 which is integer but if you just notice one more thing we are or we have not executed this statement right so num2 is still a string right now but the moment i execute the statement you will see num2 will change to integer type so let's execute and you can see the moment i execute the statement num2 got converted into integer now we have integer and integer the moment i say integer plus integer final value also i should get as integer so you got the output as integer 157 but the moment i try to do the concatenation you will see it says you can only concat string to string not integer so either i can directly do it here or i can execute here so what i will do some anyways it's coming as a string integer what i will say convert into a string and then use it so this is how we can convert any value in string so num1 and num2 was coming as integer the moment i pass integer value within str it will convert into string and we will get the final output okay so what i will do i will just put a breakpoint at this statement i don't want to execute all the program so i will just stop this debugger delete all this locks not required just right click debug it is asking please enter your number i will say 90 67 and you can see now let's talk about this sum so sum is right now it has a form of integer right and we cannot convert or we cannot concat string with integer so right now i have not executed this statement but the moment i execute just notice this variable okay some it converted into string now i can concat string with string the moment i execute this and i can see i got the value as this so this is how debugging can help you i know this is very basic example but once you move it same principle will be applicable for complicated programs as well and butter will save a lot of time for you so i would highly recommend just try the same examples which i showed you or maybe you can take some previous example that we discussed in the for loop or in previous programs it will help you to debug your programs easily so maybe quickly i can show you one more example that will make your concepts little better so i will quickly create a list which is l1 and let's say i have some numbers here okay now i want to just run a for loop and i just want to get these values i will say for i can take any variable let's say x in l1 so it will pick one value from the list it will store into x and it will just iterate it so i just want to print these values fine so what if i want to debug and i just want to see how this for loop is working how these values are getting iterated i can add a debug point here at line number 17 and let me just debug this okay it's still like asking us the numbers okay let's enter some numbers okay so you can see now right now this x already we have taken okay so it's already taking some value 10 don't worry the moment we execute this now this x has one value which is 2 so it's actually started taking 2 from this list assigned into x now the moment i step over it is again going to the for loop and from this list it will pick 3 now and it will assign into x you can see value got updated next value should be 4 you can see value is 4 again it will go to list and this time value should be 5 and here we go at last it should take 90 and now we don't have any value in the list okay so the moment execute one more time it will go back and check do we have any more value in the list if no it will come out of this for loop okay so just try this debug breakpoint multiple times with multiple examples and it will make your concept clear in case if you're new to this channel then don't forget to click on subscribe button like this video share with your friends and in case if you find any other issue related to python let me know in the comment section or you can just send me an email to my email which is mukesh ottwani at the red learn hyphen automation dot com and i will see in the next video have a nice day bye
Info
Channel: Mukesh otwani
Views: 2,155
Rating: undefined out of 5
Keywords: debug python code in pycharm, debug python program, debug python code, python tutorial, python for automation tutorial, python for automation testing, python for automation
Id: v_j6iG13va0
Channel Id: undefined
Length: 16min 3sec (963 seconds)
Published: Thu Aug 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.