In my last video, we discussed why Python is so popular and how widely it used for various applications such as web development, data analysis, machine learning, automation, robotics and more.
But in order to start programming with Python, you first need to download and install it on your computer. In this video I will demonstrate how to install Python on your computer. So let's begin.
- Go to the Python website: The first step is to go to the Python website at www.python.org/downloads. This website has the latest version of Python available for download.
- Choose your Python version: On the Python download page, you will see different versions of Python available for download. Choose the version that is compatible with your computer's operating system. For example, if you are using a Windows machine, choose the Windows version.
- Download the Python installer: Once you have selected the version of Python you want to download, click on the "Download" button to start the download process. The installer will be downloaded to your computer.
- Run the Python installer: Once the installer has finished downloading, run it by double-clicking on the downloaded file. This will start the installation process.
- Follow the installation wizard: The Python installer will guide you through the installation process.
- Follow the instructions on the screen to complete the installation. You will be asked to select the destination folder for the installation, and you can leave the default settings as they are.
- Don't forget to mark "set path variable"
- Verify the installation: Once the installation is complete, you can verify that Python is installed on your computer by opening a command prompt or terminal and typing "python" without the quotes. If Python is installed correctly, you should see the Python version number and the Python prompt.
Congratulations, you have successfully downloaded and installed Python on your computer! Now you can start coding in Python and explore its vast libraries and tools for various applications.
Writing your first program
Now, let's write a simple program in python. To do that, let's open notepad and type .. in that. Save this file.. lets say in tmp directory in D drive. You can save it anywhere.
Now go to command prompt and run this program as: python and then file name. This will give the following output. Kool.
Congratulations, you have written your first Python program!
But it looks a little cumbersome. No syntax highlighting, etc. Moreover, it'd be more uncomfortable when we would be working with multiple files in the future. Another difficulty would be debugging. and many more. This is where IDEs make our lifes easier by manyfolds.
Setting up your Python IDE
An IDE (Integrated Development Environment) is a software application that provides comprehensive facilities for programming, debugging, and testing code. Setting up an IDE for Python is an important step for developers, as it can help streamline their workflow and make coding in Python more efficient. In this section, we will go through the steps of setting up your Python IDE.
Choose your IDE: There are several popular IDEs for Python, including PyCharm, Visual Studio Code, and Spyder. Choose the IDE that best fits your needs and preferences.with ease. My preferred choice is Visual Studio Code
Setting up Visual studio code as python IDE
Visual Studio Code is a popular open-source code editor that can be used as a powerful Python IDE. Setting up Visual Studio Code as your Python IDE is a straightforward process, and it provides an excellent development environment with features such as IntelliSense, debugging, and Git integration. In this article, we will go through the steps to set up Visual Studio Code as your Python IDE.
Install Visual Studio Code
The first step is to download and install Visual Studio Code from the official website at https://code.visualstudio.com/download. Choose the version that is compatible with your operating system.
Install the Python extension
Once Visual Studio Code is installed, you need to install the Python extension. To do this, open Visual Studio Code and click on the Extensions icon on the left sidebar. Search for "Python" and click on the first result.
By clicking on this extension, you can also read more about it in the right.
Now, click on the "Install" button to install the extension.
Set up the Python interpreter:
After installing the Python extension, you need to set up the Python interpreter. To do this, go to the bottom left corner of Visual Studio Code and click on the "Python" button. Then click on "Select Interpreter" and choose the Python interpreter you want to use.
Create a new Python project
To create a new project, we'll first create a directory. Let's name this directory python-tuts. Open this folder in VS Code.
We now want to write some Python code. To do that, create a new file, file by pressing + button here, and enter the name. Let's name it as first.py. it will be created as soon as you press enter.
Note that we named the extension as py, denoting a Python file.
Okay, let's just write some code.
Type, print ("hello world") on the terminal.
Before, running this program, just notice that visual Studio Code also has a terminal that you can use to execute your script directly and various other things.
In order to demonstrate how you can reopen the terminal, I'll just close it for the time being.
Now click on the view from here, and then click the terminal to launch the terminal.
You can also open additional terminals by clicking the plus sign.
Going back to our code..let's run our very simple one liner program. Right-click on the same file and choose to run Python in Terminal.
As you can see, this executes your Python script inside of your terminal and prints the phrase Hello World there.
We just ran our script by simply right-clicking the file and selecting this option. Let's now try running it manually. You can do this through command line by simply writing python and the name of your script, which in our case is a first.py
Other notable features of VSCode
Visual Studio Code provides many more features to make your coding experience more efficient, including IntelliSense, code formatting, and debugging tools. Let's have a glimpse of these features too.
VSCode intelligence
To demonstrate this feature, let's define a variable my_name equal to Deep, for instance. We'll discuss about variables in our next video. For right now, just keep this in mind that it's a way to remember a value.
Let's see how your Python visual studio code's intelligence feature functions. This variable, my_name, has been declared, and if you write a . here, you will see a number of functions that you can use to modify your string variable, such as capitalizing it or using any of the other functions that are available.
Let's say we want to use this lower function. If you try to complete it with these parenthesis, it will also show you what this function will do.
Thus, it will return a copy of the string that has been lowercased. This is how you can use the intelligence. There are many more ways that we'll see in our coming videos.
Debug
This is life of any software development. It helps you to understand how your program works during execution or runtime. It's also helpful to find any bugs.
So let's see how VSCode can helps us to debug our python code.
To do this, we simply need to set a breakpoint, which you can do by clicking on the left-hand bar here. Once the breakpoint is set, you will see a red-dot, which indicates that program will stop here during execution.
Next, you can simply go to the debug options, where there is currently no configuration.
You can see the first option here, which says python file. When you click on the Spanner option, then this launch.json file will be created inside your project. You can just click on this python file.
As you can see, a folder called .vscode has been created, and inside it is where your launch.json file is located. However, for the time being, let's not focus solely on this file because we need to debug our code.
Again go to the debug option, and now you will be able to see python here in the drop down after clicking "Open Launch Dot JSON File" and "This Green Button Here." I'm going to simply click on this Green Button to begin debugging the Python script.
The debugger has now been started, and after a short while. You can see that it has stopped at the breakpoint we've set for our print function.
There is only one local variable that we defined, which was this my_name variable, and there are a ton of built-in variables that are available inside your standard Python, as you can see on the left-hand side of this page. This means that they are also displayed here.
If you want to view any variable, you can also write its name, such as my_name, and then press Enter. I've just added this expression, and it now displays the message Deep.
Additionally, there is a call stack here, where you can also see the breakpoint.
Here you can see various buttons: the first button, which is for continuing, the second for moving over, the third for moving into, the fourth for moving out, and the last, which is for stopping your debugging.
As we don't have much in our program, so just click the continue button, which will continue the script after the breakpoint and display this output once more when you run your debugger.
So, this is how you can debug your code. You can also restart your debugging by simply pressing the restart button. So let's put an end to our debugging for the time being and return to our script. More on debugging we'll cover in coming videos.
To conclude, setting up Visual Studio Code as your Python IDE provides you with a powerful development environment and setting up is a straightforward process. By following the steps above, you can set it up easily and start coding in Python without any hassle.
How to install and use a Python virtual environment
Setting up a virtual environment for Python is a best practice that enables you to create an isolated and self-contained environment for your Python projects. This is particularly useful when you have multiple Python projects with different dependencies, and you want to avoid conflicts between them.
In this article, we will go through the importance of virtual environment for Python. Later, we'll discuss the steps required to setup virtual environment.
Importance of a virtual environment
- Isolation: Setting up a virtual environment for Python allows you to create an isolated environment for your project, which means that the dependencies for one project will not interfere with those of another project.
- Dependency management: When working on multiple Python projects, it can be difficult to manage dependencies. Setting up a virtual environment for each project makes it easier to manage dependencies and ensures that each project has the necessary dependencies.
- Version control: Virtual environments allow you to specify the version of Python and the packages that you want to use for your project. This makes it easier to share your code with others and ensure that it runs properly on their machines.
- Reproducibility: By using a virtual environment, you can ensure that your code runs the same way on different machines. This is particularly important when working on collaborative projects or when sharing code with others.
- Security: Setting up a virtual environment can help improve the security of your project by isolating it from other projects and reducing the risk of conflicts or security vulnerabilities caused by external dependencies.
Steps to setup virtual environment
- Install virtualenv: The first step is to install virtualenv, which is a tool that allows you to create virtual environments for Python. You can install it using pip by running the command
pip install virtualenvin the command line. - Create a new virtual environment: Once virtualenv is installed, you can create a new virtual environment for your project. To do this, navigate to the directory where you want to create the virtual environment and run the command
virtualenv env. This will create a new virtual environment with the nameenv. - Activate the virtual environment: After creating the virtual environment, you need to activate it. To do this, run the command
source env/bin/activateon Mac/Linux orenv\\Scripts\\activateon Windows. This will activate the virtual environment and you will see the name of the environment in your command prompt. - Install project dependencies: With the virtual environment activated, you can now install the necessary dependencies for your project using pip. To do this, simply run the command
pip install [package name]for each package you need. - Deactivate the virtual environment: Once you are done working with your project, you can deactivate the virtual environment by running the command
deactivatein the command prompt. - Remove the virtual environment: If you no longer need the virtual environment, you can remove it by deleting the directory where it is located.
In conclusion, setting up a virtual environment for Python is a best practice that can help you manage dependencies and avoid conflicts between projects. By following the steps above, you can set up a virtual environment for your project and install the necessary dependencies with ease.
Summary
So, this is how you can begin using Python in your development environment. From here, you can continue learning Python by exploring different concepts and writing more complex programs.
I sincerely hope you have enjoyed watching this video, and I'll see you in the next one