Writing First Python Code Using VSCode

Python programming languages is one of the most popular and beginner-friendly computer language today. This post is bigner's guide for those who wants start writing first Python code using VSCode (Visual Studio Code). VSCode is a powerful and flexible editor like it can make your journey smoother.

Writing First Python Code Using VSCode

Getting Started

In this guide, we’ll walk through setting up VSCode and writing your first Python program, build python. If you're working on very large Python projects with advanced refactoring, a full IDE like PyCharm Professional might offer more out of the box. But for most Python developers, VSCode is more than enough, and often preferred for its flexibility. change sentence

Using VSCode for Python programming is a popular choice among both developers beginners and professionals alike, because it offers a great balance between lightweight performance, powerful features, and customizability. Here's why many Python developers choose VSCode:

VSCode Supports For Python

  • Excellent Python Support: Official Python extension by Microsoft
  • Lightweight but Powerful: Starts fast and runs smoothly on most machines.
  • Built-in Debugger: Powerful debugging tools with breakpoints, watch variables, call stack inspection, etc.
  • Integrated Terminal: You can run your scripts, use pip, virtual environments, or Git directly inside the editor.
  • Support for Virtual Environments: Automatically detects and uses Python environments (e.g., venv, conda).
  • Extensions: Supports extensions for everything
  • Cross-Platform: Available on Windows, macOS, and Linux, setup can be synced across devices
  • Customizable & Extensible: JSON-based configuration gives full control. Themes, keyboard shortcuts, and UI can all be tailored
  • Great for Web + Python: VSCode allows you to install extensions for HTML, CSS, and JavaScript when working on web development with frameworks like Flask, Django, or FastAPI.
  • Free and Open Source: VSCode is completely free to use, with a large open-source community and frequent updates.

Before you start, make sure you have the following:
  1. VSCode Installed
  2. Python Installed
  3. Python Extension for VSCode

Writing First Python Code

There are a few essential steps to follow before writing Python code, as outlined below.

Create a Project Folder
Create a new folder through File Explorer or run for your project, e.g., my_first_python_code:
 mkdir my_first_python_code  
 cd my_first_python_code  

Open Folder in VS Code
  • Open VS Code
  • Go to File > Open Folder...
  • Select my_first_python_code

Create a File For Python
  • In the left sidebar (Explorer), click New File
  • Name it: main.py

Writing Python Code
Here's a simple Python script that asks for a user's name and then says hello to them:
 # Ask the user to enter their name  
 name = input("Enter your name: ")  
 # Greet the user  
 print("Hello, " + name + "!")  

Example Output:
 Enter your name: Alex  
 Hello, Alex!  

What the Python Codes Do:
  1. input() is a built-in Python function that allows you to take input from the user.
  2. "Enter your name: " is the prompt message that appears to the user.
  3. Whatever the user types in response is stored in the variable name.
  4. Example- If the user types Kailash, then name will hold the value "Kailash".
  5. print() is another built-in Python function that outputs text to the screen.
  6. "Hello, " + name + "!" combines:
    • The string "Hello, "
    • The value of the variable name
    • The exclamation mark "!"
  7. The + operator is used here to concatenate strings (join them together). So, if name is "Kailash", the output will be
    Hello, Kailash!
    

What the Python Programm do:
  1. Asks the user for their name.
  2. Stores the input in a variable called name.
  3. Prints a personalized greeting using that name.

Run the Python Code
There are several ways to run python code in VSCode editor:
  1. Using the "Run Python File" Button
    • Click "Run Python File in Terminal" in the top right corner
    • Or right-click inside the file and select “Run Python File in Terminal
  2. Using the Terminal
    • Open terminal in VSCode (`Ctrl+`` or View > Terminal)
    • Type:
       python main.py  
      
  3. Using the Debugger
    • Press Ctrl+Shift+P to open the Command Palette
    • Type: Run Python File and select it
  4. Using the Debugger
    • Click the Run and Debug icon on the sidebar
    • Select "Python File" when prompted
  5. Next Steps (Optional)
    • Add a virtual environment: python -m venv venv
    • Install packages: pip install requests or similar
    • Create more files and organize your code

Summary

Congratulations! You've just written and executed your first Python program using VSCode. This is the first step in your journey to learning programming. From here, you can explore topics like variables, loops, functions, and beyond.

Thanks

Kailash Chandra Behera

I am an IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Previous Post Next Post

نموذج الاتصال