Writing basic programs with Python Programming Language
Introduction
Are you a developer or a beginner who wants to start a career in tech, Python is the programming language for you.
In this tutorial, we will be writing some basic python programs. We will write some basic programs like finding the average of two numbers, a basic input program, and lastly a program to check for even numbers.
Prerequisites
This blog is a tutorial with various examples, You only need basic knowledge of Python, if you don't, don't panic, this tutorial will help you. If you want to follow along, make sure you have this item in place before doing so.
- Virtual studio code — This tutorial will be using Virtual studio code version 1.58.2 c3f126316369cd610563c75b1b1725e0679adfb3 x64, which can be downloaded through the link.
- Python 3.9.6 — This tutorial will be using Python version 3.9.6amd64.exe, which can be downloaded through this link.
Overview of Python
Python is an interpreted high-level general-purpose programming language created by Guido Van Rossum in 1989 which was first released in 1991. Python’s design philosophy emphasizes code readability with its notable use of significant indentation. This makes debugging of errors easy and encourages the rapid development of application prototypes, marking itself as the language to code with.
Python supports cross-platform operating systems which makes building applications with it all the more convenient. Some of the globally known applications such as YouTube, BitTorrent, DropBox, etc. use Python to achieve their functionality. It is applicable in areas such as; web development, game development, scientific and numeric applications, artificial intelligence and machine learning, software development, enterprise-level/business applications, language development, etc.
Installing Python interpreter
If you downloaded the Python interpreter from the link above, follow these steps to get it installed on your machine. The file is an executable file that you requires a few steps to install.
- Go to the C:/Downloads directory, locate the (python-{version}-amd64.exe) file.
- Run the installer (python-{version}-amd64.exe).
- Check ‘install launcher for all users’ and ‘Add python 3.9 to the Path’
- Click on install now
- When the installation is done, open your terminal and type ‘python.
If you see something like this, then your python interpreter is successfully installed. In case there are any errors, you can go back and follow the step again.
Installing virtual studio code
If you downloaded virtual studio code from the link above, follow these steps to get it installed on your machine. The file is an executable file that you requires a few steps to install.
- Go to the C:/Downloads directory, locate the (VSCodeUserSetup-{version}.exe) file.
- Run the installer (VSCodeUserSetup-{version}.exe). It will only take a minute.
- Accept the agreement and click on next.
- Click on “create a desktop icon” so that it can be accessed from the desktop and click on Next.
- After that, click on the install button.
- Finally, after installation completes, click on the finish button, and the visual studio code will get open.
Now that we have python and virtual studio code installed, let's get started.
Python Program to find the average of 3 numbers
If your VScode is opened, you should see a screen like this
Click on New File
, press ctrl + s
or click on the File
tab and click save.
Type average.py
and press save. Always remember the location where this file is saved.
Now let's declare three variables to hold the three numbers from the input.
Copy the code to your vs code.
# declear the required variables and collect inputs form users
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
In the above code, I wrapped my input in a int
keyword, this is because Python interprets any data from the input stream as a string. So since what we are expecting from the user is an integer value. we need to convert the inputs to an integer.
Copy the code to your vs code.
# add the tree numbers and divide by 2
avg = (num1+ num2 + num3) / 2
print(f"The average of {num1}, {num2}, and {num3} is {avg}")
In the above code, we created another variable to hold our final result. The three inputs from the user are summed together in a bracket ()
so that our program adds the three numbers and divides the sum by two. If we don't do that, we won't get an accurate result.
Next is printed out the result with a formated string, to enable me to interpolate the variables and the result.
# incase the program is executed outside vs code
input()
Lastly for this program, we will add an input statement, this will be useful if the program is being executed outside vs code, to avoid the program ending without the user seeing the output of the program.
The program should look like this.
# declear the required variables and collect inputs form users
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
# add the tree numbers and divide by 2
avg = (num1+ num2 + num3) / 2
print(f"The average of {num1}, {num2}, and {num3} is {avg}")# incase the program is executed on the command line
input()
Now let’s run our program. Make sure your code looks like the one above. Click on the Terminal
tab in our vs code and type python average.py
“This could be any name you saved your file with”. Then press enter. If you didn't mess things up, your code should be running.
A program to check for even numbers
First, what is an even number?. An even number is a number that is exactly divisible by 2. Even numbers include 2,4,8,10,20,40, etc.
When an even number is divided by two, the remainder is always 0. We will take advantage of this rule to solve this problem.
In python, the modulus operator %
returns the remainder of an expression. So we will use this operator to text our inputs to know if they are even or not
Follow these steps to create a new file for this program.
- Click on the
File
tab and selectNew File
pressctrl + s
to save. - Name the file
evenNum.py.
- Click on
Save
Copy the code to your vs code
num = int(input("Enter number"))
check = num % 2
if check == 0:
print(f"{num} is an even number")
else:
print(f"{num} is not an even number")
In the above code, we created a variable num
to hold the input from the user and another variable check
to hold our remainder. I believe we know why we had to wrap the input
with the int
keyword.
Then we checked if the remainder equals 0 using the conditional if
statement. Then we formated our output to displace the result of our test.
Now let's run our program. Make sure your code looks like the one above. Click on the Terminal
tab in our vs code and type python evenNum.py
“This could be any name you saved your file with”. Then press enter. If you didn't mess anything up, your code should be running.
Conclusion
We have successfully built some basic applications using Python. You can play along with code. If you loved my tutorial and have any topic you will like me to write on, drop them in the comment section.