Python Basics 2 - Lets Learn Some Python

Python Basics 2 - Lets Learn Some Python

Hello and welcome to second blog of series The Pythonic Way. So now that you have some basic idea of computation and algorithm. We can start actually start coding in Python. But before that it is important that you should have some sort of IDE installed into your computer where you will run your python code which I will presume you do.

So you need two skills to be a programmer :

  • First, you need to know the programming language (Python) -you need to know its vocabulary and the grammar (just like any other human language). You should also know its sentence Structure i.e, valid syntax patterns.

  • Second you need to “tell a story” i.e, constructing a program for purpose

Once you learn one programming language such as Python, you will find it much easier to learn a second programming language such as JavaScript or C++.

Unlike human languages, the Python vocabulary is actually pretty small. We call this “vocabulary” the “reserved words”.

These are words that have very special meaning to Python. When Python sees these words in a Python program, they have one and only one meaning to Python. Later as you write programs you will make up your own words that have meaning to you called variables.

The reserved words in the language where humans talk to Python include the following:

and       del       global      not       with
as        elif      if          or        yield
assert    else      import      pass
break     except    in          raise
class     finally   is          return
continue  for       lambda      try
def       from      nonlocal    while

You will have lots of choices for choosing your names for your variables, but you cannot use any of Python’s reserved words as a name for a variable.

We will learn these reserved words and how they are used in later blogs.

Your first line of code

The nice thing about telling Python to speak is that we can even tell it what to say by giving it a message in quotes:

Open your terminal or command window and you will type python or python3 and the Python interpreter will start executing in interactive mode and appear somewhat as follows:

Python 3.9.7 (default, Sep 16 2021, 08:50:36) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> prompt is the Python interpreter’s way of asking you, “What do you want me to do next?” Python is ready to accept instructions and execute it. So lets write our first line of code

>>> print("Hello World!")
Hello World!

Congratulations! We have even written our first syntactically correct Python sentence. Our sentence starts with the function print followed by a string of text of our choosing enclosed in single quotes.

The strings in the print statements are enclosed in quotes. Single quotes ' ' and double quotes " " do the same thing; most people use single quotes except in cases like this where a single quote (which is also an apostrophe) appears in the string.

Don't understand what is string here its the message Hello world! encloded between quotes. We will learn about strings later as well :-)

For now, lets say goodbye to our Python interpreter, the proper way to say “bye” to Python is to enter quit() at the interactive >>> prompt.

>>> quit()

Interpreter and compiler

Python is a high-level language intended to be understandable for humans to read and write and for computers to read and process. Other high-level languages include Java, C++, Ruby, and many more. The actual hardware inside the Central Processing Unit (CPU) does not understand any of these high-level languages.

The CPU understands a language known as machine language. Machine language is very simple but very tiresome to write because it is represented all in zeros and ones:

01001000 01101001 00100000 01100001 01101110 01100100 00100000 01110111 01100101 01101100 01100011 01101111 01101101 01100101 00100000 01110100 01101111 00100000 01010100 01101000 01100101 00100000 01110000 01111001 01110100 01101000 01101111 01101110 01101001 01100011 00100000 01110111 01100001 01111001 00100000 01100010 01101100 01101111 01100111......

Machine language seems quite simple on the surface, given that there are only zeros and ones, but its syntax is even more complex and far more complicated than Python.

So very few programmers ever write machine language. Instead there are various translators to allow programmers to write in high-level languages like Python or JavaScript and these translators convert the programs to machine language for actual execution by the CPU.

These programming language translators fall into two general categories - (1) interpreters and (2) compilers.

An interpreter is software that translates a source code into a machine language that converts high-level programming language into machine language line-by-line while interpreting and running the program.

The interpreter is a special program that executes a sequence of instructions in order and uses tests to figure out whether it should redo a current calculation, move on to the next instruction, or stop.

A compiler needs to be handed the entire program in a file, and then it runs a process to translate the high-level source code into machine language and then the compiler puts the resulting machine language into a file for later execution. If you have a Windows system, often these executable machine language programs have a suffix of “.exe” or “.dll” which stand for “executable” and “dynamic link library” respectively, in Linux and Macintosh.

Well after this you should be wondering a bit about the Python interpreter itself. What language is it written in? The Python interpreter is written in a high-level language called “C”.

Aspects of Language

Every programming language can be thought of as:

  1. A set of primitives (simple operations we can perform).

  2. A means of combination (ways we can put those primitives together to form expressions).

  3. A means of abstraction (ways to take an expression and treat it like a primitive).

The syntax of a programming language is the set of rules that defines how the code should be written.

  • 8”hello” - this line has incorrect syntax, because there’s no operator between 8

    and “hello”

  • Syntax errors are rather common and easily caught.

Static semantics describes whether or not an expression with correct syntax has meaning.

  • 8 + “hello” - this is correct syntax, but you can’t add an integer and a string

  • Static semantic errors can keep the program from running or cause unpredictable behaviour.

Semantics describes the meaning of an expression with the correct syntax. It may be what was intended, or what was not.

  • 8 + 16 - correct syntax with meaning (8 + 16 = 24)

  • Semantic errors occur when the program gives a different meaning than what the programmer intended. As the old saying goes, Garbage in, garbage out.

What is a program?

A program is a sequence of Python statements that have been crafted to do something, some statements are conditional, sometimes a statement or a group of statements is repeated and sometimes we store a set of statements to be used over and over as needed several places throughout the program.

We will read all about this kind of statements in further blogs 😄 Liked this article. Follow me on twitter 🐦

References

(Dr. Charles R. Severance, Python for Everybody : Exploring Data Using Python 3 , Chapter 1: Introduction)