Python is one of the simplest programming languages out there. In fact, it was developed for the sole purpose of simplifying the process of learning a programming language and exposed beginners to Programming concepts. In this article, we’ll build a Python application. Don’t worry, it won’t be anything fancy or complex. But before you begin, familiarize yourself with these Python concepts:
- Indentation
- Variables
- Operators
- Data Types
- Control Flow (Loops)
- Functions
Once you are familiar with the above concepts, the content of the rest of this article will be easy to understand . Now, let’s move on to building the application. For the sole reason of simplicity, we’ll build an app that greets the user with a “Welcome to GeeksForGeeks!” message when executed.
To do this, follow the steps below:
- Step 1: Open an editor of your choice to write the python code. Here we will simply use Notepad, but it is completely up to you what you prefer.
- Step 2: Now type the following code in the editor:
- Step 3: Now that we’ve completed the code, save it as gfg.py (‘gfg’ is just a name we gave python)
) where the file has been saved. Here we will use Command Prompt.
- Step 5: Now make a call to the python interpreter from cmd to execute the gfg application as follows:
python gfg.py
This will cause Python to execute the code in the gfg.py file as shown below:
Congratulations!! You have successfully created your first Python application that greets the user with the message “Welcome to GeeksForGeeks!” message when executed.
Now let’s step it up a bit. What if you want to make your Python application a bit more interactive? Suppose you want the Python application to find out if a given number is even or odd. Let’s do it by following the steps below:
- Step 1: We will need a variable to store the number that we will give it to test. So let’s declare a variable (say, num) as follows:
num = int(input())
Here we have a variable called num which is equal to the number received by the input() function and is an integer data type.
- Step 2: Since the variable num receives the number, we’ll use a conditional statement to check if the number in the num variable is divisible by 2 or not. To do this, use the following code snippet:
if num%2 == 0: print(“It’s an even number!”) else: print(“It’s an odd number!”)
In the above code, we divide the value in the num variable by 2 using the Modulo (%) operator, and depending on what the operator returns, it will decide whether the given number is even or odd. . If it returns a quotient of 0, it is an even number; otherwise it is odd.
- Step 3: Now add the above code snippets and save them in the gfg.py file as shown below:
- Step 4: Now run the file similar to the way we ran it in cmd and the behavior is as expected:
Here we are now, with a successfully built interactive python application.
Now let’s go a little further. Since every application more or less needs a stable database to function, let’s explore the process of connecting your application to a database. For demonstration purposes, we’ll build an application that stores some user-provided information in a PostgreSQL database. To install PostgreSQL on your Windows, Mac, or Linux, visit the respective links.
Let’s create an application that takes user information (for example, names) and stores it in the database. To do so, follow the steps below:
- Step 1: Because the psycopg2 module provides an API for Python to interact with the installation from the database the same using the following command:
pip install psycopg2
- Step 2: Now open the psql shell fill in your credentials and create a database (say, test_db) using the following statement:
CREATE DATABASE test_db;
- Step 3: To establish a connection to the database use the following code:
db_conn = psycopg2.connect(“dbname=test_db user=postgres password=postgres”)
- Step 4: That’s all set up, but we’ll also need a table (eg test_name) using the following statement :
CREATE TABLE department_employee( test_names CHAR(50) );
- Step 5: Now we have configured the database and the table, let’s complete the script gfg.py to connect to the database and execute the INSERT statement to insert the data into the table:
- Step 6: Now run the above gfg.py to make the changes to the database as follows:
python gfg.py
- Step 7: Now verify the changes using the following command in the psql shell:
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 use of variables, loops, functions, statements conditionals, user input, and a database. You can explore many Python modules that are available on GeeksforGeeks to expand your application and design it according to your requirements.
To explore Python concepts, visit the GeeksForGeeks Python Tutorial section.
.