Python Basics

Why go for python?

6 months ago I was one of those people who use to hate python due to the indentations ( not a valid point I know but I used to hate due to that ) and to mention Now I am of the opinion that due to the indention readability of code increases making the development process easier. In my college days, I used to work in ruby mostly But due to my job requirements for various modules which needed python I had to learn that.

For Beginners or a person from a nontech background, I think learning Python would be a good choice. Python Syntax is basically like talking and writing plain English. For example, consider this syntax which clearly shows its resemblance to the English language.

Python Syntax:

print("Hello folks")

Java Syntax:

System.out.println("Hello folks");

Understanding Basic’s of Python

There are 2 versions of the python widely used.

I’ll be covering Python3 because most of the people have ported or have started porting there systems to Python3 environment.


Comments make it easy to write code as it helps in understanding why a particular piece of code is written. Another awesome thing about the comment is that it helps to improve the readability of the code. Well, when you give # ( Single line comment )or “”” “”” ( Multi-line comment ) python understand that’s its a comment and not to execute that line of code.

2. Print Statement

Believe me other than debugging tool from my Vscode anything which has helped me in solving the problem is a print statement. I think a print statement is one of the underrated syntaxes of the programming and you will find that it will be the most helpful when debugging the problems.

print("Hello, Readers I hope you are enjoying this article")

3. Declaring Variables

If you have done programming previously you may know variables but for those who are beginners or want a short recap you may read this or skip to the next content. As the name implies, a variable is something which can change. A variable is a way of referring to a memory location used by a computer program.

Well in most of the programming languages you need to assign the type to a variable but in python, you don’t need to. For example in C language for declaring an integer, the following syntax is used int num = 5; while in python num = 5 .

num = 5 #int
decimal_num = 5.0 #float
greeting = "Hello" #String
is_true = True #booleans

Integers as the name suggest they are nothing but a numerical value that can be positive, negative or zero without a decimal point.

Float is the same as an integer but with one slight difference, a numerical value with a decimal place.

Strings are nothing but a formation of characters or integers. In python, strings can be represented using double or single Quotes.

Booleans are constants with either True or False.

4. Conditional Statements

Well as the name suggests conditional statements are used to evaluate if a condition is true or false. Many times when you are developing something you need to check a certain condition and depending on that execution needs to done or you need to skip the program execution for that condition or terminate. In such scenarios conditional statements are useful. If, elifandelseare the conditional statements used in python.

number = 5 # Declaring a Variable.
if number == 5:
    print("Number is equal to the 5.") # print statement 1
elif number < 5:
    print("Number is less than 5.") # print statement 2
    print("Number is greater than 5.") # print statement 3

Above Snippet Shows declaration of a variable which is mapped to an integer when a value is assigned to it. If a number is equal to 5 then it will execute the print statement 1 or else if a number is less than 5 then it will execute the print statement 2 or if a number is greater than 5 it will execute print statement 3.

5. Iterators or Loops

Sometimes while developing you have a condition which needs to repeat more than once (it’s nature may change while iterating through the loop). For such scenarios, python provides for and while loop.

For loop: Ithelps in iterating over a sequence. A sequence can be a list, a tuple, a dictionary, a set, or a string ( Don’t worry we will cover list, tuple, and dictionary in next topic )

for i in range(1,10):
    print("Number is: ",i)

While loop: It will keep on executing till the condition is true.

num = 1
while num < 10:
    print("Number is: ",num)
    num += 1

6. Return Statement

Return statement causes the function to exit and return value back to the caller. It is basically declared when the function has completed the execution and is ready to return the value to the caller. A function can return the output of execution back to the caller.

def hello_return():
    data = "Hello World"
    print("Done with execution time to return")
    return data

7. Classes and Objects

Python is an Object-oriented programming language and for those who are new to the programming, there’s procedure-oriented programming which focuses on functions while object-oriented programming language focuses on objects. An object is simply a collection of data i.e variable and methods are used for the execution of the data. A class is a blueprint of that object. For creating a class class keyword is used in python. A simple example of the class is:

class Human:
    def male(self):
        print("I am a male")
    def female(self):
        print("I am a female")
human = Human()

As class and objects are broad topics I’ll be covering them in a separate article where I’ll be covering the init method, self parameter, the object method How to modify or delete properties of the object and delete an object.

8. Functions and Methods

Functions are a sequence of statements that are independent of the object. In python, a function is defined using def. They are a block of code and help us in the reusability of the repetitive logic. Functions can be inbuilt and user-defined both. They can have any number of parameters or nothing can be passed to a function. It may or may not return any data.

def add(num1, num2): 
    total = num1 + num2
    print("Total: ", total)
    return total
add(5, 7) # Calling a Function.

Methods look same like function only difference is that methods are dependent on the object. A function can be invoked by there name while methods need to be invoked by using their class reference i.e they are defined inside the class.

class HelloWorld: 
    def logger_method(self):
        print("I am the logger")
HelloWorld_Object = HelloWorld() # Object of class HelloWorld.
HelloWorld_Object.logger_method() # Calling a Method.

9. Basic Calculator Program:

Here we have created 4 Functions for addition, subtraction, delete and multiplication. The logic for the calculation is written in the function and a conditional statement is used to parse only the option the user wants. You may seem some different declaration in the program which we have not covered above.

Don’t worry they are just simple syntax for achieving our goal. input(“Select the operations…”) is used for taking the user input. Generally, it gives the output in the string you can check a type of a variable by print(type(user_input)) and our conditions are expecting integer output so we change them into an integer by using type casting. ( int() )

def add(num1, num2):
  total = num1 + num2
  return total

def substract(num1, num2):
  total = num1 - num2
  return total

def multiplication(num1, num2):
  total = num1 * num2
  return total

def division(num1, num2):
  total = num1 / num2
  return total

print("Our Calculator\n"  
        "1. Add\n"  
        "2. Subtract\n" 
        "3. Multiply\n" 
        "4. Divide\n") 

user_input = int(input("Select operations form 1, 2, 3, 4 : "))
number_1 = int(input("Enter the First number: "))
number_2 = int(input("Enter the Second number: "))

if user_input == 1:
  print("Addition is: ", add(number_1, number_2))
elif user_input == 2:
  print("Substraction is: ", substract(number_1, number_2))
elif user_input == 3:
  print("Multiplication is: ", multiplication(number_1, number_2))
elif user_input == 4:
  print("Division is: ", division(number_1, number_2))
  print("Wrong output")

Small Recap

I hope I have made basics of python clear in this article. Some broad topics like classes, objects, data structures and many more will be covered separately. What we learned so far:

  1. Why go for python? ( I hope, I convinced you to learn Python )
  2. What are the comments and why are they so important?
  3. Print statement one of the important tool for debugging.
  4. Declaring Variables.
  5. Conditional Statements.
  6. Iterators or loops.
  7. What is a return Statement?
  8. A brief idea about classes and objects.
  9. Functions, methods, and difference between both of them.
  10. Basic Calculator Program.

If you liked my work give it a heart or if you think I missed something mention it in the comment section. You can also add a suggestion for covering various future topics in python and frameworks related to the python.

Keep coding let’s be a Pythoner. ( Python + programmer 😜 )

My Twitter and Github Handle.