Python Basics

In this blog post you will hopefully learn the basics about Python to then help you create some of your own projects.

Python Basics

You can find all the source code for everything in this post on my GitHub account here.

What is Python?

Python is a high-level programming language that is really simple an easy to learn, Python supports every operating system and is widely used in lots of programs. Python can create some powerful programs to do almost anything!

What is Python used for?

Python is often used to automate a series of tasks meaning it can automate a specific series of tasks consistently and fast, python is used in web based applications and is also used for taking care of common computing tasks.

Python Syntax

Pythons syntax is by far one of the easiest programming language syntaxes to learn, its very basic and easy to understand.
Here is a basic script in python:
Code: print("Hello, World")
Output: Hello World

Example Output in Terminal

Setting up Python

Setting up Python is very simple depending on your operating system follow these links to get setup.

Basic Python Command

import – Used to import packages to your projects library.
print("") – Prints messages / text to the terminal.
input("") – Allows user to type, used to call other functions / store data.

Variable example: name = ... ("")

Creating a Basic Program in Python

This basic program will print text to the terminal, this allows you to say anything to the user when running your program.

Setting up the environment:

  • Install Python
  • Install a Text editor: Visual Studio Code / Atom

Create a new file and name it whatever you like with .py at the end (example.py) and add the following code into the project.

*NOTE:
import sys
import os
os.system('cls' if os.name == 'nt' else 'clear')
Clears your terminal and is an optional feature you can add to make your programs look nicer.

Running your Program

Open up a Terminal in your projects directory and run the following command python3 example.py or on Windows 10 you can double click the file and it should open it, if you run into any issues this could be a problem with your installation of python.
It should output like this:

Storing Data / Asking a Question

To store data or values we use variables, these allow us to do anything with different data types.

In the basic program you just created we used a variable we called it “name” with an input function asking the user “What is your name?”. Variables can not just store text they can also store numbers which we will go over later.

Now I want you to create add two more variables to your last program asking the user “How old are you?” and “What is your friends name”.

It should output something like this:

If your lost and cannot work this out have a look at the source code over here

Python Math Operations

Python can be used as a calculator, here is the syntax that math operations are used in python.

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division (with decimal point)
  • // Division (without decimal point)
  • ** The power of

Using Python as a Calculator

When working with numbers we need to use “int” this will allow python to understand how to use the number given.

Example:
print(int(5 + 5))
Now you can use this to create a simple calculator!

Create a new python project and call it addition.py then add the following code:

It should output something like this:

To use other operations replace the + with anything else from above, if your stuck have a look over here.

Create a Menu Navigation System for Programs

Creating menu navigations can open up lots of possibility's, you can include multiple tasks into one program. In this section I will teach you how to make a simple menu navigation system for all math operations we just learnt about.

First of all lets learn about the new statements we are going to be using.

  • if - Is used for decision making
  • while not- Execute the loop

Setting up the Main Menu:
First we will create a loop and set the main menu, you can see this in the code below. We then add an if statement which displays our first menu with whatever we place inside, inside this menu we also create a variable which will be used for our menu selection

Setting up the Selections:
Now we will use the if statement again to call a function, if the user types one of the numbers listed above it will set a new menu.

Changing Menu:
Let's now create a menu for the addition section, so we stated if opt == "1": it will take us to the addition menu. All we have to do now is add another if statement on a new line outside of our menu 0 loop just like this.

Adding the code:
Finally lets add our code from out addition.py project, you can do it like I have in the image below so you are able to add numbers again or go back to the main menu.


Your task now is to add all the other math operations we have called in the main menu, again if you get stuck the source code can be found here

What's Next?

You now have hopefully learnt some basic operations in Python, this is not the end there is endless possibility's with Python. Hopefully this helped you and if you ran into any problems or would like to learn more advance projects feel free to message me on Discord LachlanDev#8014. You can also see some of my open source python projects over here to try learn some more advance stuff.

Thanks for taking your time and hopefully learning the basics of python!