Open In App

Creating Your First Application in Python

Last Updated : 27 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python is one of the simplest and most beginner-friendly programming languages available today. It was designed with the goal of making programming easy and accessible, especially for newcomers. In this article, we will guide you through creating your very first Python application from a simple print statement to interacting with a database. Before we start coding, it’s helpful to be familiar with these foundational Python concepts:

If you understand these basics, you will find the rest of this article much easier to follow.

Step 1: Your First Python Program

Let’s start with a simple application that prints a welcome message by following the steps below:

1. Open any text editor you like (Notepad, VSCode, PyCharm, etc.) and write the following Python code:

print("Welcome to GeeksForGeeks!")

2. Save the file as gfg.py (the .py extension tells the system it’s a Python file).

3. Open your terminal or command prompt and navigate to the folder where gfg.py is saved and run the program by typing:

python gfg.py

4. This will result in Python executing the code in the gfg.py file as shown below:

Congratulations! You have just created and run your first Python program.

Step 2: Making the Application Interactive (Odd or Even Number Checker)

Now, let’s make your program interactive. This time, it will determine whether a given number is odd or even by following the steps below:

1. First, we need to get a number from the user. In Python, use the input() function, and convert the input string to an integer using int():

num = int(input("Enter a number: "))

2. Next, use a conditional statement to check if the number is divisible by 2:

if num % 2 == 0:

print("It's an Even number!")

else:

print("It's an Odd number!")

3. Combine these into one script and save it as gfg.py:

Python
num = int(input("Enter a number: "))

if num % 2 == 0:
    print("It's an Even number!")
else:
    print("It's an Odd number!")

4. Run it again via the command line:

python gfg.py

5. Enter any integer when prompted. The program will tell you if it’s odd or even.

Here we are now, with a successfully built interactive python application.

Step 3: Connecting Your Python Application to a PostgreSQL Database

Most real-world applications need to store and retrieve data. Let’s see how to connect your Python program to a PostgreSQL database and insert data by following the steps below. But first, you’ll need the following:

  • PostgreSQL installed on your machine
  • psycopg2 Python library (used to interact with PostgreSQL)

Let's undestand step by step approch:

Step 3.1: Install psycopg2

Open your terminal or command prompt and run the following command to install the psycopg2 library:

pip install psycopg2

Step 3.2: Set Up the PostgreSQL Database

1. Open the PostgreSQL shell (psql) and connect using your user credentials and create a new database:

CREATE DATABASE test_db;

2. Connect to the newly created database:

\c test_db

3. Create a table to store data (for example, names):

CREATE TABLE test_names (

name VARCHAR(50)

);

Step 3.3: Write Python Code to Insert Data

1. Open a text editor (like Notepad, VSCode, or PyCharm) and write the following Python code:

Python
#!/usr/bin/python
import psycopg2

try:
    # Connect to the database
    conn = psycopg2.connect(
        host="localhost",
        dbname="test_db",
        user="postgres",
        password="your_password"
    )
    cur = conn.cursor()

    # Insert data
    cur.execute("INSERT INTO test_names (name) VALUES (%s)", ("Ramadhir",))
    conn.commit()
    print("Data inserted successfully!")

except Exception as e:
    print(f"Error: {e}")

finally:
    # Close resources
    if cur:
        cur.close()
    if conn:
        conn.close()

Note: Replace "your_password" with your actual PostgreSQL password. Using parameterized queries (%s) helps prevent SQL injection.

2. Save this file as gfg.py.

Step 3.4: Run the Script

1. Open your terminal, navigate to the folder where gfg.py is saved, and run the script:

python gfg.py

2. You should see the message:

Data inserted successfully!

Step 3.5: Verify the Data Insertion

1. Go back to the PostgreSQL shell and run the following SQL query:

SELECT * FROM test_names;

2. You should see the inserted name displayed:

Congratulations! You’ve successfully connected your Python application to a PostgreSQL database and inserted your first data entry.



Next Article
Practice Tags :

Similar Reads