PyCharm Tutorial - 7. Debug python code using PyCharm

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

Key Takeaways

This video tutorial covers the basics of PyCharm, a popular Integrated Development Environment (IDE) for Python, including its elements, debugging concepts, and how to set breakpoints, use watches, and step over functions.

Full Transcript

hello today we are going to get ourself familiar with py charm so if you have watched my previous episode you should have py charm installed on your computer already so just click on it to open it click on create new project and just give a project name to it so since we're writing our first Python program we'll just say first python I have created a c callon code directory you can save this wherever you like create Okay cool so it just created this project called first Python and what we will do is we will add our first python file here so just right click on it new python file let's say you want to write a program to calculate an area of a triangle so just give that name area hit okay and it created area. py file py extension is used for python programs so whenever you're writing any Python program file it will always have py extension so under first Python program now I have this python file called area. py you can can add multiple py files so let's say if you are doing string manipulation and you want to have another strings. py file you can create it and it will add it like like as you see here okay so now you understand the difference or the relation between the project and individual python file project is a container that contains these individual python files okay now let's write our program so if you want to calculate an area of a triangle you need couple of things first thing is your base so what is the base of your triangle so I'm just going to assume let's it's 10 cm so I'll just say 10 and let's say height is five and now to create to calculate area you'll create another variable called area and and if you remember from your high school education the area of a triangle is 1x 2 multiplied by base multiplied by height now one interesting thing that you might have already noticed is when I say at it is giving you this awesome autoc complete with suggestions so it is intelligent enough to know that you have created a variable called height and that's why it is highlighting height so if it is highlighted and if you hit enter it will just type it in for you so you don't have to type in all these different characters so this is like six different characters that you need to type it in but because of this smartness of this IDE you you type h hit enter so your work gets done in basically two keystrokes versus six keystrokes this is extremely use useful in increasing your productivity when you're writing big python programs let's say you're writing let's say 200 lines of Python program this automate complete will help you a lot okay now let's print our area so we'll just say area of our triangle is again I'll use auto complete hit enter okay so now we just wrote Our Python program in order to run it you have to click on run menu hit run and click on area and it just executed it here you see area of triangle is 25 at this stage now I will walk you through different elements of this py charm editor so it has basically basically four different UI elements number one is your Manu so this particular region has all your Manu options and your project name and your python file name second area is called project pan so it is this area where you have your project and list of files you can have multiple projects open as well so this is one project you can you can actually open multiple projects the third area is your code editor this is the most important one this is the place where you will be writing your code the fourth area is your console output so when you run your program the result will go here in this area Okay so these are remember there are like four different areas and you can control the visibility all of these areas by going going under view tools so let's say due to some reason if you're if you go here and click on project if this gets hidden and you're wondering where the thing go and you want to get it back always go into view tool Windows option and click on project and you'll get it back same applies for all this other areas as well okay now let's learn some basic concepts of debugging so what is debugging debug as the word suggest it is a process of finding bug so you are finding a bug and you're fixing it now what is bug bug in any programming language is a problem that you have in your program and things are not working as you expect it to work so you do kind of like thorough analysis of your code and you find out that particular problem IC area and you fix it so that's what is called fixing a bug okay and if you're notice you might have already noticed this icon here there's little bug icon okay so this if you do this it will actually debug so let me first set a break point so breakpoint by setting a break point what you are doing is you're asking a program to stop at particular line and not just execute all at once so when you when you click on run and say run area what you're doing is you're executing all these four lines at once and you immediately see the output let's say my program has some problem and it is not printing expected output so I want to break at particular line and see what's going on so in this area click do left Mouse click and it it will draw this red circle this means you have set a breakpoint here and after that either click on this bug icon to debug it or go to run and click on debug again you have a bug icon here so click here nice so what just happened is our debugging started it started executing this program and it stopped at this very first line which is indicated with this blue background color and you see couple of things here there is a variable pan here there is watches here and I will go over them one by one so you are at base and base variable is still not defined SO watches is used to keep a watch so I want to see what's going on with this base variable when I see hit enter it says it is not defined because you have not executed this line yet so you want to execute it and go to the next line how do you do that you use step over function so either click on this icon it says STS tap over or there is a shortcut called f8 so click here or hit f8 when you do that you come to the second line which means the execution of first line completed and this base which was not defined now it is defined with a value 10 int here means integer in variable pan you will see all the variables of your programs so now you see base here again hit f8 or click on this icon to step over so you see now height is also added here and you can add height if you would like now you might be wondering these two things are same right like kind of same you have base height you have base height but this particular thing is useful because variables will have all the variables so imagine you have 100 variables this list will be long it will be hard to look for the information that you're interested in so in that gue this thing watches becomes pretty useful you can remove all the Clutter and you can add watches on the things that you are interested in looking okay again hit F f8 or step over and it again creates another variable called area and now it has a value 25 so other than these two places it also highlights the values in line in the code editor itself okay here in this area we have console as well as well you are seeing that it has not still printed that statement why because you are at this line you have not passed through that line that's why the console doesn't have the statement so again hit f8 or step into and now you see it it printed that statement because you passed through that line and it ended up in some internal python file that you don't care about so just close it okay so we learned few basic debugging Concepts here one was variable how to watch variables setting explicit watches and how to set a break point Etc okay so this was a little introduction on py Cham different areas and debugging thanks for watching

Original Description

The python video covers how to debug code in Pycharm, what are the elements of PyCharm, basic concepts of debugging, how to set a breakpoint, use of watches and when to use step over function. Topics that are covered in this Python Video: 0:00 Introduction 0:25 Create new project 4:24 Elements of PyCharm editor 4:32 menu(Dashboard) 4:45 Project panel 5:03 Core editor 5:15 Console output 5:56 Debugging 6:47 Set breakpoint 8:14 Watches 10:09 Step over function Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Video without background music: https://youtu.be/0cQj7q8EWxY Pycharm is a free code editor for python. In this tutorial we will go over how to debug python code and use various debugging features of pycharm. Next Video: Python Tutorial - 8. If Statement: https://www.youtube.com/watch?v=UZRD8rmOC7M&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=9 Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 7 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
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

This video tutorial introduces PyCharm, a popular IDE for Python, and covers its elements, debugging concepts, and how to set breakpoints, use watches, and step over functions. By the end of this tutorial, viewers will be able to use PyCharm to debug their Python code and improve their coding productivity.

Key Takeaways
  1. Create a new project in PyCharm
  2. Add a Python file to the project
  3. Write a Python program to calculate the area of a triangle
  4. Set a breakpoint in the code
  5. Use the watches feature to monitor variables
  6. Step over functions using the step over feature
  7. Run the program and observe the output
💡 PyCharm's debugging features, such as breakpoints and watches, can help developers identify and fix errors in their code more efficiently.

Related Reads

Chapters (11)

Introduction
0:25 Create new project
4:24 Elements of PyCharm editor
4:32 menu(Dashboard)
4:45 Project panel
5:03 Core editor
5:15 Console output
5:56 Debugging
6:47 Set breakpoint
8:14 Watches
10:09 Step over function
Up next
How to Get Your Brand Cited by ChatGPT, Claude and Gemini
Arvow
Watch →