0% found this document useful (0 votes)
7 views

How to Code With Python (1)

Uploaded by

blackcapbilal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

How to Code With Python (1)

Uploaded by

blackcapbilal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

How to write code w/ Python

Glossary

Python Essentials 1

Introduction to Python and Computer Programing 1

Module 1 / Section 1 – Introduction to Programming 1

Module 1 / Section 2 – Introduction to Python

Module 1 / Section 3 – Downloading and Installing Python

Module 2 / Section 1 – The “Hello, World!” Program


Section 1 – Introduction to Programming
 How does a computer program work?
- A program makes a computer usable.
- Without a program, a computer, even the most powerful one, is nothing more than an object.
- Similarly, without a player, a piano is nothing more than a wooden box.

- Computers can perform very complex tasks, but this ability is not innate.
- A computers nature is quite different.

>

- It can execute only extremely simple operations.


- For example, a computer cannot understand the value of a complicated mathematical function
by itself, although this isn’t beyond the realms of possibility in the near future.

>

- Contemporary computers can only evaluate the results of very fundamentals operations, like
adding or dividing, but they can do it very fast, and can repeat these actions virtually any numbers of
times.

>

>

- Imagine that you want to know the average speed you’ve reached during a long journey.

- You know the distance, you know the time, you need the speed.

>

- Naturally, the computer will be able to compute this, but the computer is not aware of such things

as distance, speed, or time.

- Therefore, it is necessary to instruct the computer to:

 Accept a number representing distance


 Accept a number representing the travel time
 Divide the former value by the latter and store the result in the memory
 Display the result (representing the average speed) in a readable format

>

- These four simple actions form a program.

- Of course, these examples are not formalized, and they are very far from what the computer can

understand, but they are good enough to be translated into a language the computer can accept.
>

- Language is the keyboard.

 Natural Language vs Programming Language

- A language is a means (and a tool) for expressing and reading thoughts.

- There are many languages all around us.

- Some of them require neither speaking nor writing, such as a body language; it’s possible to

express your deepest feelings very precisely without saying a word.

>

- Another language you can use each day is your mother tongue, which you use to manifest your will
and to ponder reality.

- Computers have their own language too, called machine language, which is very rudimentary.

>

- A computer, even the most technically sophisticated, is devoid of even a trace of intelligence.

- You could say that it is like a well – trained – dog; it responds only to a predetermined set of known
commands.

- The commands it recognizes are very simple.

- We can imagine that the computer responds to orders like “take the number, divide by another
and save the result.”

>

>

- A complete set of known commands is called an instruction list, sometimes abbreviated to IL.

- Different types of computers may vary depending on the size of their ILs, and the instructions could
be completely different in different models.

- Note: Machine Languages are developed by humans.

>

- No computer is currently capable of creating a new language.

- However, that may change soon.

- Just as people use a number of very different languages, machines may have different languages
too.
- The difference, though, is that human languages developed naturally.

>

>

- Moreover, they are still evolving, and new words are created every day as old words disappear.

- These languages are called natural languages.

 What makes a language?


- We can say that each language (machine or natural, it doesn’t matter) consists of the following
elements:
 An Alphabet: A set of symbols used to build words of a certain language (e.g., the
Latin alphabet for English, the Cyrillic alphabet for Russian, Kanji for Japanese, and
so on).
 A Lexis: (aka a dictionary) a set of words the languages offer its users (e.g., the
word “computer” comes from the English language dictionary, while “cmoptive”
doesn’t; the word “chat” is present both in English and French dictionaries, but the
meanings are different).
 A Syntax: A set of rules (formal or informal, written of felt intuitively) used to
determine if a certain string of words forms a valid sentence (e.g., “I am a python”
is a syntactically correct phrase, while “I a python am” isn’t).
 Semantics: A set of rules determining if a certain phrase makes sense (e.g., “I ate a
doughnut” makes sense, but “a doughnut ate me” doesn’t).

 Machine language vs high-level language


- The IL is, in fact, the alphabet of a machine language.
-This is the simplest and most primary set of symbols we can use to give commands to a computer.
- It’s the computer’s mother tongue.
>
- Unfortunately, this mother tongue is a far cry from a human mother tongue.
- We both (computers and humans) need something else, a common language for computers and
humans, or a bridge between the two different worlds.
>
- We need a language in which humans can write their programs and a language that computers
may use to execute the programs, one that is far more complex than machine learning language
and yet far simpler than natural language.
>
- Such languages are often called high-level programming languages.
- They are at least somewhat similar to natural ones in that way they use symbols, words and
conventions readable to humans.
- These languages enable humans to express commands to computers that are much more complex
than those offered by ILs.
>
>
- A program written in a high-level programming language is called a source code (in contrast to the
machine code executed by computers).
- Similarly, the file containing the source code is called the source file.

 Compilation vs Interpretation
- Computer programming is the act of composing the selected programming language elements in
the order that will cause the desired effect.
- The effect could be different in every specific case – it’s up to the programmer’s imagination,
knowledge, and experience.
>
- Of course, such a composition has to be correct in many series:
 Alphabetically – A program needs to be written in a recognizable script, such as Roman,
Cyrillic, etc.
 Lexically – Each programming language has its dictionary, and you need to master it;
thankfully, its much simpler and smaller than the dictionary of any natural language.
 Syntactically – Each language has its rules, and they must be obeyed.
 Semantically – The program has to make sense.

>

- Unfortunately, a programmer can also make mistakes with each of the above four senses.

- Each of them can cause the program to become completely useless.

>

>

- Let’s assume that you’re successfully written a program.


- How do we persuade the computer to execute it?
- You have to render your program into machine language.
- Luckily, the translation can be done by a computer itself, making the whole process fast and
efficient.
>
- There are two different ways of transforming a program from a high-level programing language
into machine language:
 Compilation – The source program is translated once (however, this act must be repeated
each time you modify the source code) by getting a file (e.g., an .exe file if the code is
intending to be run under MS Windows) containing the machine code. Now you can
distribute the file worldwide; the program that performs this translation is called a
compiler or translator.
 Interpretation – You (or any user of the code) can translate the source program each time
it has to be run. The program performing this kind of transformation is called an
interpreter, as it interprets the code every time it is intended to be executed. It also means
that you cannot just distribute the source code as-is, because the end-user also needs the
interpreter to execute it.

>

- Due to some very fundamental reasons, a particular high-level programming language is


designed to fall into one of these two categories.

- There are very few languages that can be both compiled and interpreted.

- Usually, a programming language is projected with this factor in its constructor’s minds – will
it be compiled or interpreted?

 What does the interpreter do?


- Let’s assume once more that you have written a program.
- Now, it exists as a computer file; a computer program is actually a piece of text, so the source
code is usually placed in text files.
- Note: It has to be pure text, without any decorations like different fonts, colors, embedded
images, or other media.
- Now you have to invoke the interpreter and let it read your source file.
>
>

- The interpreter reads the source code in a way that is common in western culture: from top to

bottom and from left to right.

- There are some exceptions – they will be covered later.

>

- First of all, the interpreter checks if all subsequent lines are correct (using the four aspects

covered earlier).

>

- If the compiler finds an error, it finishes its work immediately.

- The only result in this case is an error message.

>

>

- The interpreter will inform you where the error is located and what caused it.

- However, these messages may be misleading, as the interpreter isn’t able to follow your exact

intentions, and may detect errors at some distance from their real causes.
>

- For example, if you try to use an entity of an unknown name, it will cause an error, but the error

will be discovered in the place where it tries to use the entity, not where the new entity’s name

was introduced.

>

- In other words, the actual reason is usually located a little earlier in the code, for example, in the

place where you had to inform the interpreter that you were going to use the entity of the name.

>

- If the line looks good, the interpreter tries to execute it (note: each line is usually executed

separately, so the trio “read – check – execute” can be repeated many times more than the actual

number of the lines in the source file, as some parts of the code may be executed more than once)

>

- It is also possible that a significant part of the code may be executed successfully before the

interpreter finds an error.

- This is normal behavior in this execution model.

>

- You may ask now: which is better?

- The “compiling” model or the “interpreting” model?


- There is no obvious answer.
- If there had been, one of the models would have ceased to exist a long time ago.
- Both of them have their advantages and disadvantages.
 Compilation vs Interpretation – Advantages and Disadvantages

ADVANTAGES Compilation Interpretation

- the execution of the translated - you can run the code as soon as you
complete it, there are no additional
code is usually faster
phases of translation
- only the user has to have the compiler,
-the code is stored using programming
the end user may use the code without language, this means that it can be run
it on computers using different machine
languages; you don’t compile your code
- the translated code is stored using separately for each different
machine language – as it is very hard to architecture.
understand it, your own inventions and
programming tricks are likely to remain a
secret.

DISADVATAGES
- the compilation itself may be a very - don’t expect interpretation to ramp up
time – consuming process; you may not your code to high speed; your code will
be able to run your code immediately share the computers power with the
after making an amendment interpreter, so it can’t be really fast

- you have to have so many compilers as - both you and the end user have to have
hardware platforms you want your code the interpreter to run you code.
to be run on.

>

>

- What does this all mean for you?

 Python is an interpreted language. This means that it inherits all the described advantages
and disadvantages. Of course, it adds some of its unique features to both sides.
 If you want to program in Python, you’ll need the Python Interpreter. You won’t be able to
run your code without it. Fortunately, Python is free. This is one of its most important
advantages.

>

- Due to historical reasons, languages designed to be utilized in the interpretation manner are

often called scripting languages, while the source programs encoded using them are called scripts.

- Okay then, lets meet Python.


Section 2 – Introduction to Python
 What is Python?

- Python is a widely – used, interpreted, object – oriented, and high – level programming language
with dynamic semantics, used for general purpose programming.

>

- And while you may know the python as a large snake, the name of the Python programming
language comes from an old BBC television comedy sketch called Monty Python Flying Circus.

>

- At the height of its success, the Monty Python team were performing their sketches to live
audiences across the world, including at the Hollywood Bowl.

>

- Since Monty Python is considered one of the two fundamental nutrients to a programmer (the
other being pizza), Python’s creator named to language in honor of the TV show.

 Who created Python?


- One of the amazing features of Python is the fact that its is actually one person’s work.
- Usually, new programming languages are developed and published by large companies employing
lots of professionals, and due to copyright rules, it is very hard to name any of the people involved
in the project.
- Python is an exception.
>
- There are not many languages whose authors are known by name.
- Python was created by Guido van Rossum, born in 1956 in Haarlem, Netherlands.
- Of course, Guido van Rossum did not develop and evolve all the Python components himself...
>
- The speed with which Python has spread around the world is a result of the continuous work of
thousands (very often anonymous) programmers, testers, users (many of them aren’t IT specialists)
and enthusiasts, but it must be said that the very first idea (the seed from which Python sprouted)
came to the head – Guido’s.
 A history programming project
- The circumstances in which Python was created area bit puzzling.
- According to Guido van Rossum:

[ In December 1989, I was looking for a “hobby” programming around Christmas. My office (…)
would be closed, but I had a home computer, and not much else on my hands. I decided to write an
interpreter for the new scripting language I had been thinking about lately: a descendent of ABC
that would appeal to Unix /C hackers. I choose Python as a working title for the project, being in a
slightly irreverent mood (and a big fan of Monty Python’s Flying Circus).]
>
>
 Python Goals
- In 1999, Guido van Rossum defined his goals for Python:
 An easy and intuitive language just as powerful as those of the major competitors.
 Open source, so anyone can contribute to its development.
 Code that is as understandable as plain English.
 Suitable for everyday tasks, allowing for short development times.

>

- After 20 years later, it is clear that all these intentions have been fulfilled.

- Some sources say that Python is the most popular programming language in the world, while

others claim it’s the second or third.

>

- Either way, it still occupies a high rank in the top ten of the PYPL, Popularity of Programming

Language and the TIOBE Programming Community Index.

>

- Python isn’t a young language anymore.

- It is mature and trustworthy.

- It’s not a one-hit wonder.

- It’s a bright star in the programming firmament, and time spent learning Python is a very good

investment.

 What makes Python so special?


 Why Python?
- How does it happen that programmers, young and old, experienced and novice, want to use it?
- How did it happen that large companies adopted Python and implemented their flagship products
using it?
>
- There are many reasons; we’ve listed some of them already, but let’s enumerate them again in a
more practical manner:
 It’s easy to learn – The time needed to learn Python is shorter than for many other
languages; this means that it’s possible to start the actual programming faster.
 It’s easy to teach – The teaching workload is smaller than that needed by other languages;
this means that the teacher can put more emphasis on general (language – independent)
programming techniques, not wasting energy on exotic tricks, strange exceptions and
incomprehensible rules.
 It’s easy to use for writing new software – It’s often possible to write code faster when
using Python.
 It’s easy to understand – It’s also often easier to understand someone else’s code faster if it
is written in Python.
 It’s easy to obtain, install and deploy – Python is free, open, and multiplatform; not all
languages can boast that.

 Python Rivals?
- Python has two different competitors, with comparable properties and predispositions.
 Pearl – A scripting language originally authored by Larry Wall
 Ruby -A Scripting language originally authored by Yukihiro Matsumoto

>

- The former is more traditional and more conservative than Python, and resembles some of the

old languages derived from the classic C programming language.

>

- In contrast, the latter is more innovative and fuller of fresh ideas than Python.

- Python itself lies somewhere between two creations.

>

- The internet is full of forums with infinite discussions on the superiority of these three over the

others, should you wish to learn more about each of them.

 Where can we see Python in action?


- We see it every day and almost everywhere.
- It’s used extensively to implement complex internet services like search engines, cloud storage
and tools, social media and so on.
- Whenever you use any of these services, you are actually very close to Python, although you
wouldn’t know it.
>
- Many developing tools are implemented in Python.
- More and more every day – use applications are being written in Python.
- Lots of scientists have abandoned expensive proprietary tools and switched to Python.
- Lots of IT project testers have started using Python to carry out repeatable test procedures.
- The list is long.

 Why not Python?


- Despite Python’s growing popularity, there are still some niches where Python is absent, or is
rarely seen:
 low-level programming: (someone called “close to metal” programming): if you want to
implement an extremely effective driver or graphical engine, you wouldn’t use Python.
 applications for mobile devices: Although this territory is still waiting to be conquered by
Python, it will most likely happen someday.

 There is more than one Python


 Python 2 vs Python 3
- There are two main kinds of Python, called Python2 and Python 3.
>
- Python 2 is an older version of the original Python.
- Its development has since been intentionally stalled, although that doesn’t mean that there are
no updates to it.
- On the contrary, the updates are issued on a regular basis, but they are not intended to modify
the languages in any significant way.
- They rather fix any freshly discovered bugs and security holes.
- Python 2’s development path has reached a dead end already, but Python 2 itself is still very much
alive.
- Python 3 is the newer (or to be more precise, the current) version of the language.
- It’s going through its own evolutionary path, creating its own standards and habits.
>
- These two versions of Python aren’t compatible with each other.
- Python 2 scripts won’t run in a Python 3 environment and vice versa, so if you want the old
Python 2 code to be run by a Python 3 interpreter, the only possible solution is to rewrite it, not
from scratch, of course, as large parts of the code may remain untouched, but you do have to
revise all the code to find all possible incompatibilities.
- Unfortunately, this process cannot be fully automatized.

>
- It’s too hard, too time-consuming, too expensive, and too risky to migrate an old Python 2
application to a new platform, and it’s even possible that rewriting the code will introduce new
bugs into it.
- It’s easier, and more sensible, to leave these systems alone and to improve the existing
interpreter, instead of trying to work inside the already functioning source code.
>
>
- Python 3 isn’t just a better version of Python 2; it is a completely different language, although its
very similar to its predecessor.
- When you look at them from a distance, they appear to be the same, but when you look closely,
though, you notice a lot of differences.
>
- If you’re modifying an old existing Python solution, then its highly likely that it was coded in
Python 2.
- This is the reason Python 2 is still in use.
- There are too many existing Python 2 applications to discard it all together.
>
- Note: If you’re going to start a new Python project, you should use Python 3, and this is the
version on Python that will be used further ahead.
>
- It is important to remember that there may be smaller or bigger differences between subsequent
Python 3 releases (e.g., Python 3.6 introduced ordered dictionary keys by default under the
CPython implementation); the good news, is that all the newer versions of Python 3 are backward
compatible with the previous versions of Python 3.
>
- All the code samples you will find during this course have been tested against Python 3.4, Python
3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9.

 Python Implementations
-

You might also like