To get started with Python application development on Windows, you will need to have Python installed on your computer. You can download and install Python from the official website at https://www.python.org/downloads/. Once you have successfully installed Python, open up a command prompt or terminal window (depending on whether you’re using Windows or macOS) and type “python” followed by pressing Enter to confirm that it is working correctly.
Next, let’s create our first simple Python application. Open your preferred text editor or IDE (Integrated Development Environment), such as Visual Studio Code or PyCharm, and create a new file with the extension “.py”. For this example, we will call our file “my_app.py” and save it in a folder of your choice.
Inside the newly created file, add the following code:
# This script prints "Hello World!" to the console
# Use the print() function to output the string "Hello World!"
print("Hello World!")
This is a simple program that prints out the text “Hello World!” to the console when you run it using Python. Save and close the file.
To test our application, open up your command prompt or terminal window again and navigate to the folder where you saved the “my_app.py” file. Run the following command:
# This script is used to run a simple Python program that prints out the text "Hello World!" to the console.
# To run the program, we need to use the "python" command followed by the name of the Python file we want to execute.
# The following line uses the "python" command to run the "my_app.py" file.
python my_app.py
This will execute the Python script that we created earlier and print out the text “Hello World!” to your console window.
Now, let’s create a more complex application that uses input from the user. Open up your preferred text editor or IDE again and add the following code:
# This script prompts the user for their name and prints a personalized greeting.
# The input() function takes in user input and stores it in the variable "name".
name = input("What is your name? ")
# The print() function outputs the string "Hello" followed by the value stored in the variable "name" using string formatting.
print(f"Hello {name}!")
This program prompts the user to enter their name, stores it in a variable called “name”, and then prints out a personalized greeting using string formatting. Save and close the file.
To test this application, run the following command:
# This line prompts the user to enter their name and stores it in a variable called "name"
read -p "Please enter your name: " name
# This line uses string formatting to print out a personalized greeting using the value stored in the "name" variable
echo "Hello, $name! Welcome to my application."
# This line runs the python script "my_app2.py"
python my_app2.py
This will execute our new Python script that we created earlier and prompt you to enter your name. Once you have entered your name, it will print out a personalized greeting using string formatting.