Open In App

Creating Your First Application in Python

Python is one of the simplest programming language out there. In fact, it was developed for the sole purpose of simplifying the process of learning a programming language and exposed beginners to the concepts of Programming. In this article, we will be building a Python Application. Not to worry it won’t be anything fancy or complex. But before we start make yourself familiar with these Python Concepts:

Once you get familiar with the above concepts the content of the rest of this article will be easy to grasp. Now, let’s move on to building the application. For the sole reason of simplicity, we will be building an application that greets the user with a “Welcome to GeeksForGeeks!” message when executed.



To do so follow the below steps:




# code
print("Welcome to GeeksForGeeks!")



python gfg.py

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

Congratulations!! You have successfully built your first Python Application that greets the user with the “Welcome to GeeksForGeeks!” message when executed.

Now let’s step it up a bit. What if you desire to make your Python application a bit more interactive. Suppose you want the Python application to find out whether a given number is odd or even? Let’s just do that by following the below steps:

num = int(input())

Here we have a variable  named num that is equal to the number received by the input() function and is an integer data type. 

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

In the above code, we Divided the value in the num variable with 2 using the Modulus (%) operator, and depending upon what the operator returns will decide if the given number is odd or even. If it returns a quotient of 0 that it’s an even number else it is odd.




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

 
 

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

Now let’s move a bit further. As every application more or less needs a stable database for it’s functioning, let’s explore the process of connecting your application with a database. For the purpose of demonstration, we will build an application that store some sort of information provided by the user in a PostgreSQL database. To install PostgreSQL in your Windows, Mac, or Linux visit the respective links.

Let’s build an application that takes information from the user (say, names) and stores in the database. To do so follow the below steps: 

pip install psycopg2
CREATE DATABASE test_db;
db_conn = psycopg2.connect("dbname=test_db user=postgres password=postgres")
CREATE TABLE department_employee(
    test_names CHAR(50)
);




#!/usr/bin/python
import psycopg2 
 
# Establish the connection to PostgreSQL
db_conn = psycopg2.connect("host=localhost dbname=test_db user=postgres password=5555")
 
#create a cursor object from connection module
cursor_object = db_conn.cursor()
 
# Add data into the test_names table of test_db
cursor_object.execute("INSERT INTO test_names (name) VALUES ('Ramadhir')")
 
# Save the changes to database
db_conn.commit()

python gfg.py
SELECT * FROM test_names;

This will lead to the following output:

Walla!!! We have successfully added data to a PostgreSQL database at this point.

Conclusion:

At this point, we have managed to create applications that make the use of variables, loops, functions, conditional statements, user input, and a database. You can explore plenty of Python modules that age available on GeeksforGeeks to expand your application and design it according to your requirements.

To explore Python concepts visit Python Tutorial section of GeeksForGeeks.


Article Tags :