Learning Python – the basics

Views: 11
0 0
Read Time:3 Minute, 37 Second

With this series of articles we are going to explore how this programming language works and introduce the most important concepts for learning Python. This series of articles will go in the same direction of the PHP ones (https://brewedbrilliance.net/category/coding/php/learning-php/) so we will have the basics, the variables, if/else statements, arrays, loops and functions.

In this article

What is Python and why to learn

(From Wikipedia) Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured, object-oriented and functional programming.

Some of the reasons for learning Python are:

  • is widely used in the industry,
  • it is easy to absorb and has a relatively simple syntax
  • can be used for small or complex task,
  • server-side programs or web-application,
  • microservices
  • GUI applications
  • fully portable (supported by Windows, Linux, Mac, Android)
  • artificial intelligence and machine learning
  • very good for prototyping

Install Python

For installing python you can follow this guide that will allow you to switch between different versions easily https://brewedbrilliance.net/coding/switch-python-version-using-pyenv

or

Install Python on Ubuntu

sudo apt-get install python

Install Python on a Mac

Install homebrew first

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
# insert the homebrew directory at the top of your PATH
$ export PATH="/usr/local/opt/python/libexec/bin:$PATH"
$ brew install python

Install Python on Windows

we don’t support windows on this blog 🙂

https://www.python.org/downloads/windows/

Basic Python program

A basic python program (or script, debatable this point) can consist in a file named my-first-python-program.py with this content

print("Hello")

That can be executed from the command line by using this command:

python my-first-python-program.py

What is happening when we type “python my-frist-python-program.py” is that the script my-first-python-program.py is processed by another program called the Python interpreter. This interpreter (written in C) opens and reads the script, compiles or translate it into bytecodes and then executes them in a sort of simulated computer called “virtual machine”.

General structure of a Python program

All the python programs and scripts will always follow this general structure

Import statements (usually at the top of the files) are used for importing libraries and packages that can be internal or external to the project

# import statements
# import packages and libraries external or internal
import os
# or
from datetime import datetime 
# or
from some_other_package import (
    library_or_package1,
    library_or_package2,
    ...
    library_or_packageN
)
# or
from another_library import package1, package2, package3

Functions or classes definitions

# functions definition
def some_interesting_function(interesting_argument):
    interesting = interesting_argument
    do_something(interesting)

def some_other_interesting_function(some_stuff):
    stuff = some_stuff
    more_interesting(stuff)

# or classes
class MyPythonClass:
    def my_class_func(self):
        print("I'm my_class_func")

Program execution

if __name__ == "__main__":
    # call functions and do stuff
    ....

It is important to note that the above code is very important to understand. We could have started our program just by calling the functions and do all the stuff, however by using the comparison block of __name__ with __main__ we are ensuring that the program is being executed ONLY if our program is being called and not imported. We will came back to this concept in the next lessons.

First python program

As first complete example we will write a program that is writing the system time every 5 seconds using what we learned so far (import, function, main) and after 3 write will exit. Don’t get scared we will go through all the basic concepts in the next lessons

import time
from datetime import datetime

def print_time():
    print("\n"*5)
    print("{}".format(datetime.now()))

counter = 0
while 1:
    counter += 1
    print_time()
    if counter == 3:
        exit()
    time.sleep(5)

With that that’s the end of the first lesson, I hope you liked it and if so please share and let us grow

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
Previous post Switch python version using pyenv
Next post NGINX as kubernetes ingress controller
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x