Python Tutorial

Python Tutorial

Python is a programming language that allows developers to work quickly and integrate systems more efficiently. Because of that, Python has become a popular programming language around the world. In this tutorial, we will share the basics of Python so you can easily get started with it.

What is Python?

Python is a popular programming language created in 1991 by Guido van Rossum. This is a programming language that is easy to read, remember, and learn because of its clear and concise structure. Python is highly applicable, it can be built and executed on a variety of operating systems platforms such as Linux, Macintosh, macOS, Windows, etc.

How to install Python?

Normally, on macOS and Linux operating systems, Python will be pre-installed. If you use the Windows operating system you will need to install Python from outside.

To check if your device has Python installed, just go to the Command Line (For Windows, search for cmd in the Run dialog box (+ R), with Linux open the terminal using the Ctrl combination + Alt + T, for macOS, just search for the Terminal app and run it.

Then enter the following command:

For Python2

python --version

For Python3

python3 --version

If the computer has Python installed, it will generate a message with the available Python version.

In case your computer does not have Python installed, you can visit Python’s website to download and install the version according to the instructions.

Basic Python Syntax


In Python, to define a variable, function, class or object the programmer will use an identifier for it. Identifiers can start with an uppercase or lowercase letter of the alphabet or an underscore (_) followed by 0 or more characters, underscores, or digits (between 0 and 9).

Python is a case-sensitive programming language, so Learnshareit and learnshareit are two different identifiers in Python.

In Python, you can execute the syntax directly on the command line

>>> print ("Hello LearnShareIT!")
# Hello LearnShareIT!

Also, if you have created a Python file on the server, to execute it you need to pass the .py file extension and run it with the command:

C:\Users\Your Name> python


Indentation is also a differentiator and is extremely important when working with Python. Python uses indentation to highlight blocks of code, and it is represented by spaces. The spaces will be indented to the right. The number of spaces in indentation is variable, but all commands inside the block must be indented by the same amount of space. You can see this example:

if 8 > 2:
print("Eight is greater than two!") # This is the exact command line
if 8 > 2:
print("Eight is greater than two!") # This is the wrong command line


Python supports two types of comments: single-line and multi-line comments. In Python, a (#), not inside a string constant, begins a one-line comment. All characters after the # sign and extending to the end of the line are considered a comment and are ignored by the interpreter. For example:

# Declare two integers
x, y = 5, 2 
# Add two integers
sum = x + y 

Python Lists

In Python, to store many different data types, similar to arrays of other programming languages. The data in a list is separated by commas and you can completely make changes in the list such as insertion, deletion. However, editing the list will not create a new list.

To create a list in python will use square brackets. The syntax to create a list is as follows:

<list_name>=[value1, value2, ..., valueN];

To access the elements of a list, you will also use the concept of an index (in python the index will also start at 0).

For example:

my_developer = ['TiffDoan', 'Programmer', 2022];
my_number = [5, 8, 4, 6, 7];

print "my_developer[0]: ", my_developer[0]
print "my_number[1:3]: ", my_number[1:3]


my_developer[0]: TiffDoan
my_number[1:3]: [8, 4]

You can completely perform operations such as delete, insert, update and use built-in functions or methods when processing lists in Python. Learn more in Python Lists.

Python Numbers

The numeric data type in python is the name of the data type used to call digits used in python programs. Just like the literal data type, the numeric data type in python is also Literal – a value that represents itself.

Integer data types in python include positive integers (1,2,3…), negative integers (-1,-2,-3…) and number 0. Integer does not contain commas in itself.

For example:

# Output: <class 'float'>

# Output: (4+5j)
a = 2 + 5j
print(a + 2)

Python supports integers, real numbers, and complex numbers. They are defined in Python’s classes: int, float, and complex.

You can perform delete operations, convert numbers in Python. Follow the detailed article about Python Numbers to understand it better.

Python Strings

String in Python is one of the most common types in Python. Strings in Python are surrounded by single or double quotes. Python treats single and double quote commands as the same. For example, ‘LearnShareIT’ is the same as ‘LearnShareIT’.

my_string = 'Hello!'
# Hello!

In Python string is treated as a byte array and it represents unicode characters.

However, Python has no character data type, a character is simply a string of length equal to 1.

When accessing any element of the string you will use square brackets [ ] in the syntax. The first character has an index of 0.

String is treated as a list of characters so you can access the characters through its index. Indexes start at 0, and if you try to access a character outside the index range, an IndexError will appear.

In case you access a character that is not in the string’s index, python will return an IndexError error. And will return a TypeError if you access the index with floats or other numeric types.

Python Tuples

The Tuple data type in python is an ordered, immutable collection. Duplicate data is allowed.

Tuple uses parentheses, Unlike List which uses square brackets. Objects in a tuple are separated by commas and surrounded by parentheses (). Like the index of a string, the index of a tuple starts at 0.

mytuple = ("one", "two", "three")
print(mytuple) # ("one", "two", "three")

Once you create a tuple, their value will not be changed, or it can be understood that the tuple is immutable. You won’t be able to change it. In case you convert the tuple to a list and then edit the list, then you can convert the changed list into a new tuple.

Python Variables

A Python variable is a dedicated memory location for storing values. In other words, a variable in a python program provides data for the computer to process.

In python there are many different data types, so when declaring a variable you can choose any name you want, as long as it is syntactically correct.

Unlike most other programming languages, variable declarations must be accompanied by data types. In Python, declaring the data type for the variable is not necessary, but Python will know the data type of the value assigned to the variable.

So to check the value data type of an initialized variable, we use the type() function, for example:

myage = 22
type(myage) # check the value data type of the variable myage

Python Arrays

Python also provides us with a module that is Array. Using this module we can use and manipulate arrays in Python. However, this array only allows numeric values inside.

To import the array module into the program, we need to use the statement

import array as arr

To access the elements in the array, we also access the index of the element. The element index starts from the first element the index will be 0, the second element the index is 1, the third element is the index 2….the nth element the index is n – 1.

To change the value of an element in the array, we need to access that element and use the assignment operator “=” to change the new value for the element.

You can learn more about Array in Python in the articles below.

Python For Loop

The For loop in Python is a loop statement used to execute the inner statements sequentially. In Python, the For loop is also looped for a specified number of times. A defined number of iterations means an explicitly specified number of repetitions – this is also a condition for the loop to execute the blocks of statements inside the For loop.

The following example uses a For loop in Python to iterate and display the value of the variable i with the loop condition between 1 and 5.

# Declare a loop between 1 and 5
for ex1 in range(1.5):
     # Show value ex1



Python Enum

Enum stands for enumeration which means enumeration, this feature defines a set of names associated with constants like numbers, strings, etc. Enum is useful for representing data that represents a finite set of states such as day of week, month of year, etc.

We can create an Enum simply by calling the Enum class as shown in the example below:

>> q1= Enum('M1', [('Jan', 1), ('Feb', 2), ('Mar', 3)])
>> print (q1(3))
# M1.Mar

Python Enum is extremely useful in managing data with a finite set of states. Or, maybe you’re interested in creating a game and using a Python enum for various state effects.

Python Dictionary

Dictionary in python is a mapped and mutable data type. Mapping data type (mapping type) means that the objects in it are created by a combination of key and value respectively, and can be meaningfully changed after creating the dictionary in python. You can add, remove or change its value.

Each element of a Dictionary in Python is made up of a key and value pair (key and value) separated by a colon (:).

The elements are separated from each other by a comma , and are placed between the {} pairs to create the Dictionary data type in Python as follows:

     key1 : value1,
     key2 : value2,
     keyN : valueN

When using a dictionary, you don’t need to care about the order of the elements in the list anymore.

With a dictionary in Python, the element can be accessed by specifying its key, so the element in the dictionary can be accessed visually when programming.

Python Dates

Datetime in Python is a specialized module for performing operations with time. In the Datetime module contains many classes for manipulating different objects of time.

  • class datetime is used to manipulate date and time.
  • class date is used to manipulate dates
  • class time is used to manipulate time
  • class timezone used to manipulate timezone

The date class in python is a class used to manipulate dates, with the following constructor:

class, month, day)

In there:

  • year : year ( 1 <= year <= 9999
  • month: month ( 1<= month<= 12)
  • day : day (1<= day <= number of days in that month)

Using date in python specifically like the following example:

import datetime
d =, 10, 25)
# 2022-10-25


In this tutorial, we have given you the best overview of Python. Through this article you can learn the most basic knowledge when starting to get acquainted with a new programming language.

If you are interested in Python, you can learn more about it in our other articles at LearnShareIT.

Other tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *