Basic Programming Concept for Beginner (Python)

Python programming

Intro

I believe computer programming is one of the most beautiful things in this universe. And we can learn it for free. We can do so many awesome things using this skill. For that, we have to invest time in it. And no doubt, this one blog post doesn’t make someone expert in programming. Here, I am going to cover an ultra-light overview of some basic concepts.

First of all, we have to remember that programming and programming language are not the same. Programming consists of some concepts, and programming language is how we implement those. So if we know the programming concepts, we can implement them in any programming language. The main difference between languages is syntax.

There are mainly two types of general-purpose programming languages. Statically typed and dynamically typed. In statically typed languages, we must have to define the data type. On the other hand, we don’t need to do it in dynamic programming languages. For example, C, C++, Java, C# are examples of statically typed programming languages. And Python, JavaScript, PHP are examples of dynamically typed programming languages. There are some pros and cons, but that is beyond this blog’s topic. Here, We are going to use python, which is a dynamically typed language.

You don’t worry about these too much as you start to learn the basics.

To run python on your computer, make sure your computer has it. If not, you can download it from python.org.

To write code, we generally use IDE or code editor. IDE (Integrated development environment) is more powerful than a code editor. Both of these are for writing code. Generally, IDE comes up with robust features and functionality. But code editors are minimal than an IDE. But it also has many useful features for writing good code. There are so many IDE and code editors. You can choose any of them as per your preference.

If you don’t want to go through the installation process, you can use this online editor to see the output or practice this blog’s example. Just write your code, run, and see the result. And it’s simple.

The extension of a python file is: .py. (filename.py)

Print

Printing something is the most fundamental thing in programming. In python, we can do it in one of the easiest ways. We have to put the data we want to print in the print function. There are many kinds of data types in programming. And string, number, boolean is some of them. In short: A String is a sequence of characters wrapped with single or double-quotes. But number data type is not. Let’s see some examples by printing those data types.

print('Python is Awesome!') 
# Output 
# Python is Awesome

print(1) 
# 1

print(True) 
# True

Comment

In programming languages, it’s is an essential part. It helps us to write documented code. So that when we see our code after a long time, we can understand what this code does. And comments help other programmers to understand or read our codes easily. Suppose we write some code for doing something, and we describe the code what it is for using a comment. Then this will help others to understand our code easily and also help us.

When we execute/run our code in the compiler/interpreter, comments are ignored by the compiler/interpreter. Compiler/Interpreter skip this part. And also, if we comment out our codes, the compiler/interpreter will not run it.

If you don’t understand the core purpose of it, don’t need to worry. You will get it after sometimes. Just remember, if you write something using the comment keyword, it will not affect your actual code.

To write a single-line comment in Python, use the # symbol. And don’t forget to give a space after the # symbol. For a multiline comment, use ”’ ”’ . Let’s see.

# Single line comment

'''
More
than
one 
line
comment
'''

Variable

A Variable is something like a box where we can store our data. We can give a name of a variable so that we can identify them. When we need data of a variable, we can get the data by calling the name.

In python, we don’t need to declare the type of data. Type of data can be string, integer, float, boolean, etc. Python will automatically understand what type of data it is.

To write a variable, first, we have to give a name. And then = (known as assignment operator) sign, and finally the data. We can check the data type of a variable using the type function. It’s a built-in Python function. We can call a Python built-in function from anywhere in a python file.

Let’s see some examples.

name = 'Python'
print(name) # Python
print(type(name)) # <class 'str'>

number = 1
print(number) # 1
print(type(number)) # <class 'int'>

Array

There is no Array in Python. But we can use List, which is equivalent to the Array. So, Array is a type of data structure where we can store multiple data like a variable. It’s mainly a collection of data. But in a variable, we can only store single data.

The procedure of writing an array is simple. Generally, we put all the data inside []. And All of them have to be separated by a comma. Let’s see the following example.

odd_numbers = [1, 3, 5, 7, 9]
print(odd_numbers) # [1, 3, 5, 7, 9]

# We can access these elements using an index numbers. And index starts from 0.
odd_numbers = [1, 3, 5, 7, 9]
index       =  0  1  2  3  4

print(odd_numbers[0]) # 1
print(odd_numbers[4]) # 9

# We can use len() function to know the number of elements of an array.
print(len(odd_numbers)) # 5

In python, you can store more than one type of data in a list. ['Python', 1, 1.0, true]

Control Flow/Conditional

One of the most important parts of programming is control flow. We can make decisions based on certain conditions and do something. It works the same way how we make decisions in real-life based on some condition. For example, if this happens, then we will do this. Otherwise, we will do something else or nothing. In programming, it works this way.

if condition:
    # If the condition is true, then this block of code will execute.  
    # A block can have more than one line of code.

Look at the above example. There is a sample code for writing conditions. To declare a conditional statement, we have to write the ‘if’ keyword. Then condition. It can be any condition that has to be True. If it’s true, then the if block will execute. Otherwise, not. Don’t forget to put a colon after writing the conditions.

Suppose, 1 > 0 can be condition. We all know that it’s true. There are many conditional operators in programming, for example. >, <, == (it checks the equality of two value), etc. Let’s see how to write some conditional statements using these operators.

You must have to indent the code to execute as if block’s code. To start a line with indentation, you can press your tab key. Remember, indentation is essential in Python. You can’t indent code randomly, and you have to maintain the order.

if 1 > 0:
    print('1 is greater than 0')

# Output
# 1 is greater than 0

if 1 < 2:
    print('1 is smaller than 2')
    
# Output
# 1 is smaller than 2
    
if 1 == 1:
    print('1 is equal to 1')
    
# Output
# 1 is equal to 1

if 0 > 0:
    print('0 is greater than 0')

# It will not print anything because the condition is false.

You can try something like above with these operators: >= (greater than or equal to), <= (less than or equal to), != (not equal).

If your condition is false, you can do something else based on that. For that, you can use else keyword.

Let’s see the below example for better understanding.

# Let's try to write a conditional statement to check even numbers.

# % = Modulus operator. 
# Modulus operator gives the remainder of a division. 
# For example: 11 % 2 = 1

number = 10

if number % 2 == 0:
    print('Even number')
else:
    print('Odd number')
    

# Try to see the output by changing the number from the variable. 

Loop

Assume that we need to print hello world 100 times. It’s tedious if we have to do it 100 times manually using the print statement. Instead of doing this way, we can use a loop. We will write print(‘Hello World’) and tell the loop to do it 100 times for us. A Loop is pretty useful when something needs to do repeatedly. There are mainly two types of loops in programming. That is for and while loop.

while

Let’s see how to print hello world 100 times using a while loop.

number = 100

while number > 0:
    print('Hello World')
    # number = number - 1 == number -= 1
    number -= 1

To start a while loop, you have to use a while keyword. And then the condition and colon. After this line, you can write code for the loop block. (AKA loop body)

Remember, at some point, we have to stop the loop. Otherwise, it will run forever. If you put True as a condition of a while loop, it will never end.

In the above example, our initial number is 100, and the condition is number > 0. So the loop will run till the variable value is greater than 0. So how do we get the value of this variable to 0 so that the loop stops? As we have to run it 100 times, we decrease the value of the variable by 1 in the loop block. After running 100 times, the variable’s value will be 0. And 0 > 0 is a false condition, and then the loop will stop.

If we need to print the number in the loop, it will print 100 to 1. What if we want to print 1 to 100? Let’s see another way.

number = 1

while number <= 100:
    
    print(number)
    
    number += 1

If we want to print all the elements from a list, we can do it using a while loop. Let’s see.

frontend = ['HTML', 'CSS', 'JavaScript']

index = 0 # index starts from 0

# len(frontend) = 3
while index < len(frontend):
    
    print(frontend[index])
    index += 1
for

‘for’ loop is also used to do repetitive work. In python, it’s a bit different from the other language. Before getting into the ‘for’ loop in python, it’s necessary to know about the range() function. It helps us to count the index.

range() is a built-in python function. It gives us the sequence of numbers from 0 to before the number we will provide. This function takes at least one parameter. Suppose if we provide the number 10 ( range(10) ). It will give us 0 to 9. The default starting value of this function is 0. We can set the starting number as the first parameter of the function. If we need a sequence of 5 to 10, we need to write something like: range(5, 11). Generally, from the starting number, it’s increment by 1. But we can set the incrementation step as the third parameter of the function. For example: range(0, 11, 2). If you want to set the step, you have to specify all three parameters. It’s helpful when we need to run a ‘for’ loop. This loop is pretty awesome for iterating through a sequence.

If we need to run a ‘for’ loop ten times, we can create a sequence of numbers from 0 to 9. Then run a loop on that sequence. Let’s see an example.

for i in range(10):
    print(i)
# 0 1 2 3 4 5 6 7 8 9

for i in range(0, 11, 2):
    print(i)
# 0 2 4 6 8 10

As a string is a sequence of characters, we can print each character using this loop.

string = 'Python'

for char in string:
    print(char)

Function

A function is mainly a block of code with a name for doing something specific. We can use that block of code just by calling the function name. A Function is essential for reducing repeating code. Suppose, if we need to use a block of code five times to do something, we can put that block into a function and call the function name five times instead of repeating that block of code five times.

Let’s see how to write a function.

def printHello():
    print('Hello')

printHello() # Hello

In our above example, def is the keyword for defining a function. And then ‘printHello‘ is the function name. Giving a function name is up to you. But it’s good to have a name that is related to the function’s purpose. After the function name, (): is essential. From the following line, the function block will start. But, don’t forget to indent the code to run as a function’s block. After the function’s defining line, each line of code with one indentation will count as a function block.

To call a function, we have to write the name with parenthesis.

However, you don’t need to write a function to print Hello. This one is just for example purposes. Let’s write another one, where it will print all the even numbers from 1 to 20.

def even_numbers():
    
    for i in range(1, 21):
        if i % 2 == 0:
            print(i)

even_numbers() # It will print all even numbers from 1 to 20.
even_numbers() # It will also print all even numbers from 1 to 20.

You can also pass a value when you call a function. To use this value, we can receive it as a function parameter.

In the previous example, it prints all even numbers from 1 to 20. But this time, we want to print even numbers from 1 to n. Where the number n, we will provide when we call the function.

Let’s see how to do it.

def even_numbers(number):
    
    for i in range(1, number+1):
        
        if i % 2 == 0:
            print(i)

even_numbers(10) # It will print all even numbers from 1 to 10.
even_numbers(100) # It will print all even numbers from 1 to 100.

Remember, when receiving a value as a parameter, we can name it anything to use the value inside the function. But it’s good to give a relevant name.

It’s not that we can only pass one value. We are allowed to provide as much as we need. But those values have to be separated by a comma.

Let’s see another example.

def add_two_number(num1, num2):
    
    result = num1 + num2
    print(result)
    
add_two_number(5, 5) # 10
add_two_number(1, 1) # 2

Conclusion

It’s pretty tough to cover everything about basics in a single blog. But this can give you a good overview of a programming language concept. And I will try to update this article over time.

Again, if you want to learn Programming, this one article is not even near enough. You have to practice a lot. Try to understand the basics in-depth. You have to read a lot. Learn from Google, Books, Youtube, Courses, Blog, etc. And most importantly, solve programming problems.

Happy coding.
Happy learning.