Skip to main content

How to Run a Python Script-Task inplant and internship training in Python

How to Run a Python Script

Python is a well known high-level programming language. The Python script is basically a file containing code written in Python. The file containing python script has the extension ‘.py’ or can also have the extension ‘.pyw’ if it is being run on a windows machine. To run a python script, we need a python interpreter that needs to be downloaded and installed.

Here is a simple python script to print ‘Hello World!’:

print('Hello World!')

Here, the ‘print()’ function is to print out any text written within the parenthesis. We can write the text that we want to be printed using either a single quote as shown in the above script or a double quote.

If you are coming from any other language then you will also notice that there is no semicolon at the end of the statement as with Python, you no need to specify the end of the line. And also we don’t need to include or import any files to run a simple python script.

There is more than one way to run a python script but before going toward the different ways to run a python script, we first have to check whether a python interpreter is installed on the system or not. So in windows, open ‘cmd’ (Command Prompt) and type the following command.



python -V

This command will give the version number of the Python interpreter installed or will display an error if otherwise.

Different ways to run Python Script

Here are the ways with which we can run a Python script.

  1. Interactive Mode
  2. Command Line
  3. Text Editor (VS Code)
  4. IDE (PyCharm)
  1. Interactive Mode:
    In Interactive Mode, you can run your script line by line in a sequence.

    To enter in an interactive mode, you will have to open Command Prompt on your windows machine and type ‘python’ and press Enter.

    Example 1:
    Run the following line in the interactive mode:

    print('Hello World !')

    Output:

    Example 2:
    Run the following lines one by one in the interactive mode:

    name = "Aakash"
    print("My name is " + name)

    Output:

    Example 3:
    Run the following line one by one in the interactive mode:

    a = 1
    b = 3
    if a > b:
        print("a is Greater"
    else:
        print("b is Greater")

    Output:

    Note: To exit from this mode, press ‘Ctrl+Z’ and then press ‘Enter’ or type ‘exit()’ and then press Enter.

  2. Command Line
    To run a Python script store in a ‘.py’ file in command line, we have to write ‘python’ keyword before the file name in the command prompt.
    python hello.py
    

    You can write your own file name in place of ‘hello.py’.

    Output:

  3. Text Editor (VS Code)
    To run Python script on a text editor like VS Code (Visual Studio Code) then you will have to do the following:
    • Go in the extension section or press ‘Ctrl+Shift+X’ on windows, then search and install the extension named ‘Python’ and ‘Code Runner’. Restart your vs code after that.
    • Now, create a new file with the name ‘hello.py’ and write the below code in it:
      print('Hello World!')
      
    • Then, right click anywhere in the text area and select the option that says ‘Run Code’ or press ‘Ctrl+Alt+N’ to run the code.

    Output:

  4. IDE (PyCharm)
    To run Python script on a IDE (Integrated Development Environment) like PyCharm, you will have to do the following:
    • Create a new project.
    • Give a name to that project as ‘GfG’ and click on Create.
    • Select the root directory with the project name we specified in the last step. Right click on it, go in New and click on ‘Python file’ option. Then give the name of the file as ‘hello’ (you can specify any name as per your project requirement). This will create a ‘hello.py’ file in the project root directory.
      Note: You don’t have to specify the extension as it will take it automatically.

    • Now write the below Python script to print the message:
      print('Hello World !')
    • To run this python script, Right click and select ‘Run File in Python Console’ option. This will open a console box at the bottom and show the out put there. We can also run using the Green Play Button at the top right corner of the IDE.

    Output:

Comments

Popular posts from this blog

IOT Projects souce code

IOT  Projects souce code What is IoT? The Internet of Things (IoT) is a network where everyday objects like devices, vehicles, and appliances have sensors and internet connectivity. It lets them gather and share data, work together, and perform tasks without human control. This helps boost automation and efficiency across different areas. You can learn IoT to understand its core components and get further knowledge of its functionalities. 20 IoT Projects with Source Code When it comes to IoT, there are many interesting ideas to explore, from making your home smarter to making an autonomous drone. No matter if you’re just starting or have experience, here are 20 Internet of things projects for beginners and advanced professionals. Simple IoT Project Ideas for Beginners For beginner-level, you can start with simple and fun IoT project ideas that involve basic components like sensors, actuators, and microcontrollers. Below are a few of them: 1. Smart Home Automation Smart home automat...

Connecting R to MySql in English #Training Trains

software design institute training

  ONLINE-OFFLINE IN-PLANT/INTERNSHIP With Certificate Training For B.E(ECE,EEE,CSE,IT,AI,ML,DataScience,Cyper Security),MCA, B.Sc,M.E,M.Tech. @ TrainingTrains.Online Classes Available 100 % Job placement Training Full Stack Developer | Placement Training In-plant Training/Internship Training with Project supports the various Engineering and Non-Engineering, Arts Students to develop their Skills on the IT Companies/Corporate Expectations. DURATION OF IN-PLANT TRAINING: 1 week and Above.DURATION OF INTERNSHIP: 1 Month and Above Internship-inplant training For All Departments students, Internship- inplant Training Python | Java | Full Stack Development | UI & UX | C& C++ | Php | Web Designing - HTML, CSS, Java Script, Bootstrap | MEAN Stack | MERN Stack | MEARNStack | Android | Kotlin | Flutter | Database - Oracle, Mongo DB, MySQL, MS SQL Serer | Visual Studio Code | Objective C | Swift | Go Lang | Frame work - Laravel, Django, Vue JS | Machine Learning | React JS | ...