Introduction

Python is a general purpose interpreted, interactive, object-oriented and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL).

Python 2.0 was released on 16 October 2000 with many major new features, including a cycle-detecting garbage collector and support for Unicode

Python 3.0 was released on 3 December 2008. It was a major revision of the language that is not completely backward-compatible. Many of its major features were backported to Python 2.6.x and 2.7.x version series. Releases of Python 3 include the 2to3 utility, which automates (at least partially) the translation of Python 2 code to Python 3

My code examples will be a mixture of both versions (2 and 3).

The Zen Of Python

Love a Good Read? Open up your Python Interpreter, type in the following code:

import this

The beautiful lines below show up. Go ahead, try it

    The Zen of Python, by Tim Peters

  • Beautiful is better than ugly
  • Explicit is better than implicit
  • Simple is better than complex
  • Complex is better than complicated
  • Flat is better than nested
  • Sparse is better than dense
  • Readability counts
  • Special cases aren't special enough to break the rules
  • Although practicality beats purity
  • Errors should never pass silently
  • Unless explicitly silenced
  • In the face of ambiguity, refuse the temptation to guess
  • There should be one - and preferably only one - obvious way to do it
  • Although that way may not be obvious at first unless you're Dutch
  • Now is better than never
  • Although never is often better than *right* now
  • If the implementation is hard to explain, it's a bad idea
  • If the implementation is easy to explain, it may be a good idea
  • Namespaces are one honking great idea - let's do more of those!

Prerequisites

It is not a must to have any knowledge of programming before you start Python. Python for me is the preferred first programming language. I might be wrong or a bit bias because it was my first programming language, but it's not possible for several websites to be lying when they all say or imply that Python is easy to learn. What you do need is a device that you can run Python on. These days, even mobile phones have a Python app that can be used on them.

You need to download the Python interpreter. I have the Python 3.7 interpreter installed in my windows machine. I did not need to set any path in the environment variables. However, if yours was not set after installation, follow this Guide or just look though this Page returned after a quick Google search and choose a destination.

Python Editions

As mentioned in the introduction, there are various versions of Python. However, these versions can be streamlined to be two:

  1. Python 2
  2. Python 3

One major difference in the syntax of both versions has to do with the print keyword

In Python 2

print "Hello, Benedict"

In Python 3

print("Hello, Benedict")

For other differences between Python 2 and Python 3, check out this Article

Basic Syntax

The Python language has many similarities to Perl, C and Java. However, there are definite differences between the languages

Python is meant to be an easily readable language. Its formatting is visually uncluttered and it uses English keywords where other languages use punctuation. Unlike many other languages, it does not use curly brackets to delimit blocks, and semicolons after statements are optional. I will pick out a few of the syntactic details that stand out and talk briefly on them.

Indentation and Escape sequences

Python uses whitespace indentaton, rather than curly user brackets to delimit blocks An increase in indentation comes after certain statements, e.g nested statements; a decrease in indentation signifies the end of the current block.

            def add_three(x):
              sum = x + 3
              if (x.isnumeric()):
                print(sum)
              else:
                print("Please enter a number")
          

Python emphasises readability and clean code. Escape sequences also help in this regard. Python's interaction with users is also commendable. Using the input keyword, data can be collected from users. For this data to be entered on a new line, Python provides a code for that.

            input('Please enter your name: \n')
          

result:

            Please enter your name:
            *cursor*
          

Quotation and Comments in Python

A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them.

            #A comment 
            print("Showing comment")
          

Python accepts both single (') and double (") quotation marks. However, as with other languages If you are starting an expression with a single quotation mark, kindly finish it with a single quotation mark, else, the interpreter will scream and point you precisely to your error. Another fun fact about the Python interpreter, the error messages are quite informative and detailed.

Variables and Data Types

Variables are reserved memory locations tos store values, like containers. This means that once you create a variable, you reserve some space in memory

In Python, you do not need to create variables with a particular keyword, you simply go ahead and write the variable name and assign a value to it. The equal sign (=) is used to assign values to variables in Python.

            counter = 100        # An integer assignment 
            miles = 1000.0       # A floating-point assignment 
            name = "Benedict"    # A string assignment
          

There are exciting data types in Python, not so different from the normal data types in other languages. Python does have a soft spot for numbers. You will come across floating-point numbers and integers, both of the data type: number. To get the type of an element, simply do type(element). Some of the data types in Python are listed below with examples

            100                 # Integer
            1000.7              # Floating-Point number
            2+3j                # Complex
            'I am a string'     # String
            True                # Boolean
          

Basic Operators

Operators are constructs which can manipulate the value of operands.Operands are the elements involved in an expression or operation.

Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator

There are several types of operators in Python. Below are some of these operators

  1. Arithmetic Operators: e.g Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%)
  2. Comparison Operators: e.g Equal (==), Not equal (!=), Less than (<), Greater than (>)
  3. Assignment Operators: e.g Assign (=), Add and Assign (+=), Subtract and Assign (-=)
  4. Logical Operators: e.g Logical OR (or), Logical AND (and), Logical NOT (not)

Decision Making

Decision making is anticipation of conditions occuring while executing a program and specifying actions to be taken according to these conditions.

Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. An action is then decided according to the outcome.

Python uses if...else statements for decision making. This statement is quite powerful and makes a program dynamic.

            def add_three(x):
              sum = x + 3
              if (x.isnumeric()):    # Check if the value collected is a number
                print(sum)
              else:
                print("Please enter a number")
          

Loops

Generally, statements are executed sequentially: The first statement in a function is executed first, followed by the second and so on. Loops are for situations when you need a block of code to run several number of times.

  • while loop repeats a statement or group of statements while a given condition is TRUE.
  •               count = 0
                  while (count < 4):
                    print ('The count is: ', count)
                    count = count + 1
    
                  print 'Good bye'
    
                  result:
                    the count is:  0
                    the count is:  1
                    the count is:  2
                    the count is:  3
                    Good bye
                
  • for loop executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
  •               letter = 'Python'
                  for x in letter:
                    print ('the current letter: ', x)
    
                  result:
                    the current letter: P
                    the current letter: y
                    the current letter: t
                    the current letter: h
                    the current letter: o
                    the current letter: n
                
  • nested loops allow you to use or more loops inside any any other loop

Lists

Lists in Python is the same as arrays in JavaScript. List is the most versatile data type available in Python which can contain several element of different data types, its values are separated with a comma and all of them placed within sqaure brackets

list1 = [1, 2, 'mathematics', true, 0.5]

Functions

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.

             def add_three(x):
              sum = x + 3
              if (x.isnumeric()):    # Check if the value collected is a number
                print(sum)
              else:
                print("Please enter a number")
          

Reference