Pay Attention to the Basics

Learning is a process. I got to understand this while watching my little niece find her way through counting figures, then I said to myself I can easily count figures but there are still somethings I don't know in mathematics. Counting numbers are just the foundation of mathematics. It helps us all have our roots in mathematics. This same thing applies to all spheres of learning.

When learning something new, it is important to understand the basics because it's on these basics you will become an expert. This article will be talking about the basic things you need to get along with learning python, and all these basic things will make you a master.

basics.gif Let's take Python for example, shall we?

shall we.gif

Python is a concise high-level programming language but be it as it may, understanding the basics of this concise language is paramount.

When talking about basics of python, we are definitely gonna be mentioning

  • print statement
  • datatypes
  • literals and identifier
  • list, set, tuple, dictionary
  • Operators in python
  • escape characters
  • python control statement
  • loops in python
  • string and characters
  • comments in python
  • Indentation
  • functions
  • error and expectation handling etc

All these are the basics aspects to understand in python as a beginner. Now, let's take an example to see the importance of understanding each of the above.

''' this is a Fibonacci series code. Fibonacci series is the
recurrence of numbers in a given order. A series whereby the next number is the addition of the last two. This program allows the user to input the number they want to start with and the range of the series, and then it will print numbers in that given series, not more than 100 '''
c = int(input("enter your number" ))
d = int(input("enter your range" ))

class Fibonacci_series():
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def series(self):
        while (True):
            yield (self.b)
            self.a, self.b = self.b, self.a + self.b

f = Fibonacci_series(c,d)


for (x) in f.series():
    if x > 100:
        break
    print(x)

Note: In the snippet above, you will notice that most of the basics in python is been applied. You might be looking at it like, this is a lot. Chill lets break it down, shall we?

  • The first 4 lines are comment lines. Which explains what the code is about and what the user is supposed to do and the result to get. This shows the importance of adding comment line to your code.
''' this is a Fibonacci series code. Fibonacci series is the
recurrence of numbers in a given order. A series whereby the next number is the addition of the last two. This program allows the user to input the number they want to start with and the range of the series, and then it will print numbers in that given series, not more than 100 '''
  • The next two lines are the input statement. This allows the user to input some numbers freely, which are the number the user wants to start with, and the range the user wants the Fibonacci series to run.
c = int(input("enter your number" ))
d = int(input("enter your range" ))
  • The 7th line is; declaring a class function. A class is a code template used for creating objects. Objects have member variables and have behavior associated with them. So in python class is created using the keyword class.
class Fibonacci_series():
  • The 8th to 10th line is the "init" function, the method "init" is a reserved method in python classes. It is called as a constructor in object-oriented programming. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class. So in this case class Fibonacci_series was created.
 def __init__(self, a, b):
        self.a = a
        self.b = b
  • The next 4 lines are another block of code containing another function and a loop statement, a while loop. Which is taking in a condition, that while this is true do this, in this case, while true the program yield. You can read more about yield here
def series(self):
 while (True):
            yield (self.b)
            self.a, self.b = self.b, self.a + self.b
  • This is also a loop statement, a for loop. This deals with the range you want the series to take. You can check the previous posts on understanding loops in python for more clarity on the while loop and for loop and the loop control statement.
for (x) in f.series():
    if x > 100:

- 
       break
  • The last line is the print statement, which is simply saying display result

    print(x)
    
  • Indentation is another basic thing to always put in mind. Indentation refers to the spaces at the beginning of a code line. In other programming languages, the indentation in code is for clear readability only, it's not the same thing with Python, the indentation in Python is very important. Python uses indentation to indicate a block of code. Whenever the indentation is wrong the Python interpreter returns an indentation error. For example: Taking the code we analyzed initially, we will just change a position and see the result.

''' this is a fibonacci series code. fibonacci series is the
recurrence of numbers in a given order.
this program will print numbers in a given series not more than 100 '''
c = int(input("enter your number" ))
d = int(input("enter your range" ))

class fibonacci_series():
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def series(self):

    while (True):
            yield (self.b)
            self.a, self.b = self.b, self.a + self.b

f = fibonacci_series(c,d)


for (x) in f.series():
    if x > 100:
        break
    print(x)

Note that the position of the 'while True' is changed. Now see the result below

C:\Users\TINUKE\PycharmProjects\shecodeafrica\venv\Scripts\python.exe "C:/Users/TINUKE/PycharmProjects/shecodeafrica/fibonacci series.py"
  File "C:/Users/TINUKE/PycharmProjects/shecodeafrica/fibonacci series.py", line 14
    while (True):
    ^
IndentationError: expected an indented block

Process finished with exit code 1

IndentationError shown and also pointing at the exact line where the error found.

I hope you now understand the importance of paying attention to the basics because in the example analyzed it shows that it's the combination of the basics that make up the whole program. You'll agree with me that no matter how pro and efficient you are, it's all built on the knowledge of the basics and how good you'll become in anything coding or designing, depends on how grounded you are in the basics. You will have to pay more attention to the basics. The basics are always very important.

important.gif

I hope you find this article helpful. Feel free to drop your comments.