Python for Beginners – Basic Codes to get you Started

Python is the backbone of Artificial Intelligence and learning python is the gateway to unlocking its capabilities. While it may all sound confusing and foreign at first, the key, as to every other difficult problems, is to actually try and get started. So here we have the simple run codes that you have to understand in order to learn python.

Print

To actually get Python to run any code you write on your py script, use print( ), and python will run what is between the brackets. For example, if you wish to do a simple calculation such as 1+2, simply key in as such print(1+2). This also works for multiplication, division etc. I.e print(10/2) which will run 10 divide by 2 or print(1*2) which will run 1 multiple by 2 and so on.

Hash Comments

A full python script can be very long and sometimes (most of the time rather), you might forget what was the code you wrote previously about OR you might be working in teams and have to hand off your script to a colleague to continue on from. So instead of writing a new code to overwrite what was written previously (Codes run from top to bottom, so a later code will replace what was written previously) which will cause the server to take a longer time to load because of a longer script, you can just add in comments. Comments are basically words or phrases that are not run by the script and do not influence the result. Comments will be ignored by the system and it’s mostly for the coder’s eyes and reference only. To add a comment, simply add a hash infront of the phrase. For example #this code is for the multiplication of revenue. Comments can be both in words or numerical format.

Exponentiation **

To do a square of formula, we use the exponentiation code which is 2 asterisk. One asterisk is for multiplication and two asterisk is for exponentiation. The code will use the number on it’s left and run it to the square of the number on the right. For example,

print(2**3) will give you 8, which works out to be 2 x 2 x 2 = 8.

print(4**4) will give you 256 which works out to be 4 x 4 x 4 x 4 = 256

Variable and Strings

In Python, you are able to assign a variable to a code. Think of it as naming a string of codes, so you don’t have to retype the code each time you want to use it again.

For example, you wish to find out how much interest can you get based on your annual pay. You can first name your annual pay as a variable.

annual_pay= 2000*12
ocbc_interest= 1.1
ocbc_result=annual_pay*ocbc_interest
print(ocbc_result)

you can also use Variable on text, also known as strings in Python. However, for anything that is related to strings (text), you would have to add a single or double quotes, For example:

x = “awesome”
print(“The Collective Intelligence is ” + x)

The Collective Intelligence is awesome

Types – Integer, Float, String, Boolean

So aside from text also known as Strings (str) in Python, there are also other types of classification for different types of codes.

For whole numbers, it is known as Integer or int in Python.

For numbers with decimal places, it is called a float.

Boolean are codes that represent logical value such as True or False

Leave a Comment!