Summary: This is an ultimate python tutorial guide for absolute beginners. (Please note – This article was wrote focusing the people who are new to programming.)

If you are new to programming and want to start it with something beginner friendly, one of the best programming language to start with is python.

It was designed to be simple, easy and readable. You can express your concepts with fewer lines of code using python when compared it with other programming languages.

Learning to program with python will benefit you in many.

In 2020, most people learning to program in python because of data science, machine learning and artificial intelligence.

Python has powerful APIs and library available for AI, Data Science, and Machine learning.

Will learning Python help me with other languages?

Lot of programming languages have similar concepts at their core with different syntax formats. For example, loops in every language has the same concept but different syntaxes.

//for loop in JavaScript

var i; 

for (i = 0; i < 10; i++)  
{ 
    document.write(i); 
} 
//for loop in PHP

<?php
for ($i = 0; $i < 10; $i++) {
  echo $i;
}
?>
//for loop in Python

for i in range(0, 10):
    print(i)

As you can see, once you learn the basic concepts, all you need to do when switching from one language to another is focus on the syntax structure of the new language because you already has a good understanding about the concepts.

To learn these basic concepts, python is the best language that you can try because of it’s user-friendliness.

OK! enough talking let’s dive in.

About Python Programming Language

This is a python tutorial article for beginners and not a history article. Therefore, I’ll mention essentials about the python programming language.

Python is a high-level, general-purpose programming language Created by Guido van Rossum and first released in 1991.

How does python programming language get it’s name?

Long story short, when Guido van Rossum began implementing python he was also reading a comedy series called “Monty Python’s Flying Circus” so he decided to name the language “Python”.

Is python open source?

Yes.

Basic Python Programming

Arithmetic Operations

Addition of two or more numbers can be easily done by placing the “+” sign in between the numbers.

addition in python
addition in python

We can divide a number by another number by placing the “/” sign in between the numbers.

divide in python
divide in python

Instead of “/” sign if we place “%” sign in between two numbers we can get the remainder of those two numbers.

python get remainder after division
python get remainder after division

If we add two “/” signs (“//”) in between two numbers it will round of the resulting value to the nearest whole number.


Rounding of a number to nearest whole number in python
Rounding of a number to nearest whole number in python

If you want to calculate the power values of a number you can do it by adding two “*” (“**”) in between the two numbers.

Calculating the power values of a number in python
Calculating the power values of a number in python

Hello World In Python

Not like other programming languages, printing a statement in python is very straight forward. You can use the print function to print a message onto the screen.

 Hello World In Python
Hello World In Python

Comments In Python

Python comments are the lines of text included inside a python script. These lines will not be interpreted by Python.

Normally we use comments to describe the functionality of the code we wrote.

You can add a single line comment in python by adding a “#” sign in front of your desire comment.

# this is a single line comment in python

You can add multiple lines of comments in python by using triple quotes.

""" this 
is a multi
line comment in 
python"""
 Python Comments
Python Comments

You’ve completed the most easiest arithmetic operations section in this python tutorial for beginners article. Now we’re going to learn about the data structure in python programming language. So let’s get started.

Data Structures In Python

Data structures are ways of organizing data in a computer so that you can reuse them effectively. Every programming language has data structures. In python main data structures are Lists, Tuples, Dictionaries and Sets.

Lists In Python

A list is a container which we used to store multiple data. In python lists are written using square brackets “[]”. Data inside a list is ordered and changeable. Also we can have duplicate items inside a list.

players = [71, 72, 73, 74]
print(players)
 Python Lists
Python Lists

We can access the items inside the list by referring to the index number. First item inside the list is indexed as 0 second item inside the list is indexed as 1 and so on… Always remember that list indexes are start with 0 not 1.

 Accessing items in a list in python
Accessing items in a list in python

We can also assign new values to the items inside the list.

assign new values to the items inside the list in python
assign new values to the items inside the list in python

We can also add new items into the list.

 add new items into the list in python
add new items into the list in python

But the above method is not permanent. If we once again check players list it results the old list.

 add items into the list in python
add items into the list in python

To make this permanent we can use .append() method.

.append() method in python
.append() method in python

Sets In Python

Set is also another python collection data type. Set’s can’t have duplicates. If there are any duplicates only one is printed. These are unordered and unindexed. In python sets are written using curly brackets. “{}”.

names =  {'learn', 'tech', 'python', 'tutorials'}
print(names)
Python Sets
Python Sets

You can not access access items inside a set by referring to it’s index number because sets are unordered the items has no index. But you can can loop through set by using the “for” loop.

Accessing a list using for loop in python
Accessing a list using for loop in python

Dictionaries In python

Dictionary is also a python collection data type. These are unordered, changeable and indexed. In python dictionaries are written using curly brackets. “{}”. Dictionaries have keys and values.

classmates = {'Tony':'Boy', 'Emma':'Girl', 'Lucy':'Girl'}
print(classmates)
print(classmates['Emma'])
 Python Dictionaries
Python Dictionaries

We can also loop through dictionaries to print out the key and value pairs.

Accessing a dictionary using for loop in python
Accessing a dictionary using for loop in python

Tuples In Python

Tuples are another type of in-built data structure in python. These are immutable data structures which mean you can not add, edit or delete values in a tuple once it generated.

Other than that all other features are quite similar to lists.

In python tuples are written using parentheses “()” and each element separate is separate by a comma.

Tuples in python
Tuples in python

If you want to change the values in a tuple, you can do it by changing the tuple into a list using the list() method.

Changing values in python tuple
Changing values in python tuple

Also Read: How Does Hardware And Software Work Together

In this python tutorial for beginners article now we are going to talk about the conditionals and loops in python programming language. So let’s get started.

Conditionals and Loops In Python

These conditional are used when we want to make decisions within our code. So in summery if, elif and else statements are used for decision making.

if statement

a = 10
b = 20
if a > b:
   print("a is greater")

As you can see in the above code in python “if statement” is written by using the “if ” keyword.

In the example we are using two variables “a” and “b” which are equal to 10 and 20 respectively. Third line is the place where you can find the beginning of if statement. It says that if a is greater than b display “a is greater” in the terminal. Since 10 is not greater than 20 we won’t be able to see a output in the terminal.

By the way if you have only one condition in the if statement you can put it on the same line as shown below.

if a > b: print("a is greater than b")

elif Statement

a = 10
b = 20
if a > b:
   print("a is greater")
elif a < b:
   print("a is smaller")
elif a == b:
   print("a is equal to b")
else:
   print("Done")

elif statement helps you to check multiple expressions and execute a block of code. For example in the above code at first it will check if a is greater than b if it’s true it will display “a is greater” in the terminal. Obviously 10 is not greater than 20 therefore code will jump to the next line. In there it will check if a is smaller than b. Since it is true it will print out the statement “a is smaller” and exit the program without going through the rest of the code lines.

else Statement

a = 10
b = 20
if a > b:
   print("a is greater")
else:
   print("Done")

if the conditional expression in the if statement resolves to false then this else statement comes into play. For example in the above code a is not greater than b therefore the code within else statement gets execute. Therefore in the terminal the only thing we can see is the word “Done”.

Loops In Python

for Loop in Python

This loop is used for iterating over a sequence.

foods = ["Soya", "Rice", "meet"]
for f in foods:
    print(f)
for loop in python
for loop in python

In the above example “f” act as the variable for 1st f=rice 2nd f=Soya and 3rd f=meet. This how you can iterate over a sequence using for loop.

while Loop in Python

while loop is used to execute a set of instructions as long as a condition is true. Let’s look at the below statement.

s = 1
while s < 6:
  print(s)
  s += 1
while loop in python
while loop in python

So in the above code we have a variable named as “s” and it is equal to 1. Next line it says that while “s” smaller than 6 which means while 1 is smaller than 6 print “s” and add 1 to “s”. As soon as the “s” become equal to 6 the loop will stop.

continue and break Statements in Python

i = 0
while i < 6:
  i += 1 
  if i == 3:
    continue
  print(i)

Continue is use if the statement is true skip the code it and jump to beginning. In the above code there is variable named “i” and it is equal to zero. next we have a while loop which says as long as “i” smaller than 6 “i” equal + 1 (i = i+1). During the 3rd iteration the condition in the 4th line become true. Then it will jump to the next line of the code which says ”continue” therefore during this iteration it will not print the number “3” on the terminal. It will continue to iterate by printing 4, 5 and 6.

continue statement in python
continue statement in python

If we remove the 5th line from the above code which contains the “continue” statement the output will be only number 3.

without continue statement in python
without continue statement in python
i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1

Using the break statement we can stop the stop the loop even if the condition is true.

break statement in python
break statement in python

So far in this “python tutorial for beginners” article we’ve talked about arithmetic operations, data structures (list, tuple, dictionary and set), conditionals, loops, break and continue statement.

Now we are going to talk about some basic ideas which are little bit complex. So let’s get started.

Read and Write Files In Python

fw = open(‘Sample.txt’, ‘w’)
fw.write(‘Writing to a file’)
fw.write(‘Hello! there’)
fw.close()

Above code block shows you how to write to a file using python. In the first line of the code we’ve created a text file with the name “sample”. In the first line of the code ‘w’ is a parameter which we need to add to the open() function. ‘w’ parameter will overwrite the existing content. If we add ‘a’ instead of ‘w’ it will append to the end of the file. write() function will write the content to the created file and finally close() function will close the opened file after finish writing to it.

fr = open(‘sample.txt’, ‘r’)
text = fr.read()
print(text)
fr.close()

Above code block shows you how to read from a file in python. In the first line of the code we’ve opened the file that we need to read. ‘r’ parameter will specify the open function that we need to read from the open file. In second line we’ve stored the text inside the sample.txt file in the text variable. Finally, we print the stored text on to the terminal.  

Python Functions

In simple terms functions are collection of codes that we can use again and again within a program. These block of codes are only get executed when they are called. In python there are many in-built functions such as print(), range(), chr(), compile() etc… You can also create your own functions with your code.

def name():
    print(“learntechtutorials”)

Above code is a simple function in python. If you execute a python script with the above code block you will never get any output in the terminal because even though there is a function in the script, it is not called. As I mentioned in the above functions get executed only if they get called.

We can call the above function like below.

def name():
    print(“learntechtutorials”)
name()

Now if we run the script once again you will get an output in terminal saying “learntechtutorials”.

Functions in python
Functions in python

Below is another function with more stuff inside.

def money(btc):
    amount = btc + 273
    print(amount)
money(2) 

Classes and Objects In Python

Class is an easy way to group similar variables, functions together. In common practice classes are started with capital letters.

class Enemy:
    life = 3

    def attack(self):
        print("Ouch")
        self.life -= 1

    def checklife(self):
        if self.life <= 0:
            print("I'm Dead")
        else:
            print(str(self.life) + "life left")

enemy1 = Enemy()

enemy1.attack()
enemy1.attack()

So when using classes in python there are few things that you need to remember. When you call variables inside a class you always need to add “self.” In front of the variable. Always we need to create an object in order to access data in classes. In the above example enemy1 is the object that we used to call the enemy class. Last two lines of the code shows you how we use functions inside a class.

Each object is independent from each other. Which means; if we make a change to one object which uses the same class it does not affect for the second object which also use the same class.

So that’s it guys. This article “Python Tutorial For Beginners”, was wrote to give you a quick overview and about the basic concepts in python programming. There are so many other thing that you need to learn even under the topics that we discussed in this article.

After this try to advance your self with advanced topics in python. There are plenty of resources available for free in the internet that you can referrer during your journey to become a python pro. Keep learning. Thank you for checking out this python tutorial for beginners. I hope you learn something new.

Write A Comment