0% found this document useful (0 votes)
249 views7 pages

Python Essentials

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
249 views7 pages

Python Essentials

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

PCAP: Programming Essentials in Python a syntactically correct phrase, while "I a python am"

isn't).
Part 1 Semantics
- a set of rules determining if a certain phrase makes
Module 1: Introduction to Python and Computer sense (e.g., "I ate a doughnut" makes sense, but "A
Programming doughnut ate me" doesn't).
Instruction List (IL)
I. Programming – absolute basics - the alphabet of a machine language. This is the
simplest and most primary set of symbols we can
How does a computer program work? use to give commands to a computer. It's the
Program computer's mother tongue.
- makes a computer usable. High-level programming languages
- instruct the computer to: - They are at least somewhat similar to natural ones in
 accept a number representing the distance; that they use symbols, words and conventions
 accept a number representing the travel readable to humans. These languages enable
time; humans to express commands to computers that are
 divide the former value by the latter and much more complex than those offered by ILs.
store the result in the memory; Source Code
 display the result (representing the average - a program written in a high-level programming
speed) in a readable format. language,
These four simple actions form a program. Source File
- File containing the source code.
Natural languages vs. programming languages
Language
- a means (and a tool) for expressing and recording Compilation vs. interpretation
thoughts. Computer programming
Computer language - the act of composing the selected programming
- own language of the computers. language's elements in the order that will cause the
- developed by humans. desired effect.
Instruction List (IL) - such a composition has to be correct in many senses:
- a complete set of known commands.  alphabetically - a program needs to be
Natural languages written in a recognizable script, such as
- New words are created every day and old words Roman, Cyrillic, etc.
disappear. These languages are called natural  lexically - each programming language has
languages. its dictionary and you need to master it;
thankfully, it's much simpler and smaller
What makes a language? than the dictionary of any natural language;
Elements:  syntactically - each language has its rules
An alphabet and they must be obeyed;
- a set of symbols used to build words of a certain  semantically - the program has to make
language (e.g., the Latin alphabet for English, the sense.
Cyrillic alphabet for Russian, Kanji for Japanese, and Two different ways of transforming a program from a high-
so on). level programming language into machine language:
A lexis Compilation
- (aka a dictionary) a set of words the language offers - the source program is translated once (however, this
its users (e.g., the word "computer" comes from the act must be repeated each time you modify the
English language dictionary, while "cmoptrue" source code) by getting a file (e.g., an .exe file if the
doesn't; the word "chat" is present both in English code is intended to be run under MS Windows)
and French dictionaries, but their meanings are containing the machine code; now you can distribute
different). the file worldwide; the program that performs this
A syntax translation is called a compiler or translator.
- a set of rules (formal or informal, written or felt Interpretation
intuitively) used to determine if a certain string of - you (or any user of the code) can translate the
words forms a valid sentence (e.g., "I am a python" is source program each time it has to be run; the
program performing this kind of transformation is run your code immediately after any
called an interpreter, as it interprets the code every amendment;
time it is intended to be executed; it also means that  you have to have as many compilers as
you cannot just distribute the source code as-is, hardware platforms you want your code to
because the end-user also needs the interpreter to be run on.
execute it. Interpretation
- Advantages:
What does the interpreter actually do?  you can run the code as soon as you
Computer file complete it - there are no additional phases
- a computer program is actually a piece of text, so the of translation;
source code is usually placed in text files. Note: it  the code is stored using programming
has to be pure text, without any decorations like language, not the machine one - this means
different fonts, colors, embedded images or other that it can be run on computers using
media. Now you have to invoke the interpreter and different machine languages; you don't
let it read your source file. The interpreter reads the compile your code separately for each
source code in a way that is common in Western different architecture.
culture: from top to bottom and from left to right. - Disadvantages:
 don't expect that interpretation will ramp
1. The interpreter checks if all subsequent lines are your code to high speed - your code will
correct (using the four aspects covered earlier). share the computer's power with the
2. If the compiler finds an error, it finishes its work interpreter, so it can't be really fast;
immediately. The only result in this case is an error  both you and the end user have to have the
message. The interpreter will inform you where the interpreter to run your code.
error is located and what caused it. However, these
messages may be misleading, as the interpreter isn't 1. Python is an interpreted language. This means that it
able to follow your exact intentions, and may detect inherits all the described advantages and disadvantages.
errors at some distance from their real causes. Of course, it adds some of its unique features to both
3. If the line looks good, the interpreter tries to execute sets.
it (note: each line is usually executed separately, so 2. If you want to program in Python, you'll need the Python
the trio "read-check-execute" can be repeated many interpreter. You won't be able to run your code without
times - more times than the actual number of lines in it. Fortunately, Python is free. This is one of its most
the source file, as some parts of the code may be important advantages.
executed more than once).
4. It is also possible that a significant part of the code Due to historical reasons, languages designed to be utilized in
may be executed successfully before the interpreter the interpretation manner are often called scripting
finds an error. This is normal behavior in this languages, while the source programs encoded using them
execution model. are called scripts.

Compilation vs. interpretation - advantages and II. Python – a tool, not a reptile
disadvantages
Compilation What is Python?
- Advantages: Python
 the execution of the translated code is - a widely used, interpreted, object-oriented, and
usually faster; high-level programming language with dynamic
 only the user has to have the compiler - the semantics, used for general-purpose programming.
end-user may use the code without it; - name comes from an old BBC television comedy
 the translated code is stored using machine sketch series called Monty Python's Flying Circus.
language - as it is very hard to understand
it, your own inventions and programming Who created Python?
tricks are likely to remain your secret. Guido van Rossum
- Disadvantages: - Python is one person’s work.
 the compilation itself may be a very time- - created Python.
consuming process - you may not be able to - born in 1956 in Haarlem, the Netherlands.
A hobby programming project Python rivals:
In December 1989, I was looking for a "hobby" programming Two direct competitors:
project that would keep me occupied during the week around - Perl: a scripting language originally authored by
Christmas. My office (...) would be closed, but I had a home Larry Wall;
computer, and not much else on my hands. I decided to write - Ruby: a scripting language originally authored by
an interpreter for the new scripting language I had been Yukihiro Matsumoto.
thinking about lately: a descendant of ABC that would appeal 1. The former is more traditional, more conservative than
to Unix/C hackers. I chose Python as a working title for the Python, and resembles some of the good old languages
project, being in a slightly irreverent mood (and a big fan of derived from the classic C programming language.
Monty Python's Flying Circus). – Guido van Rossum 2. In contrast, the latter is more innovative and more full of
fresh ideas than Python. Python itself lies somewhere
Python goals between these two creations.
In 1999, Guido van Rossum defined his goals for Python:
- an easy and intuitive language just as powerful as Where can we see Python in action?
those of the major competitors; 1. Internet services: search engines, cloud storage and
- open source, so anyone can contribute to its tools, social media and so on.
development; 2. Developing tools
- code that is as understandable as plain English; 3. Everyday use applications
- suitable for everyday tasks, allowing for short 4. Scientists
development times 5. Testers
Python isn't a young language. It is mature and trustworthy.
Why not Python?
III. Python – a toll, not a reptile | Why Python? Some niches where Python is absent, or is rarely seen:
- low-level programming (sometimes called "close to
What makes Python special? metal" programming): if you want to implement an
Python is: extremely effective driver or graphical engine, you
- it's easy to learn: the time needed to learn Python is wouldn't use Python;
shorter than for many other languages; this means - applications for mobile devices: although this
that it's possible to start the actual programming territory is still waiting to be conquered by Python, it
faster; will most likely happen someday.
- it's easy to teach: teaching workload is smaller than
that needed by other languages; this means that the V. Python 2 vs. Python 3
teacher can put more emphasis on general
(language-independent) programming techniques, Python 2
not wasting energy on exotic tricks, strange - Python 2 is an older version of the original Python.
exceptions and incomprehensible rules; Its development has since been intentionally stalled,
- it's easy to use for writing new software: it's often although that doesn't mean that there are no
possible to write code faster when using Python; updates to it.
- it's easy to understand: it's also often easier to - The former is more traditional, more conservative
understand someone else's code faster if it is written than Python, and resembles some of the good old
in Python; languages derived from the classic C programming
- it's easy to obtain, install and deploy: Python is free, language.
open and multiplatform; not all languages can boast - These two versions of Python aren't compatible with
that. each other.
Drawbacks:
- it's not a speed demon: Python does not deliver 1. Python 3 is the newer (to be precise, the current) version
exceptional performance; of the language. It's going through its own evolution
- in some cases it may be resistant to some simpler path, creating its own standards and habits.
testing techniques: this may mean that debugging 2. You should use Python 3, and this is the version of
Python's code can be more difficult than with other Python that will be used during this course.
languages; fortunately, making mistakes is always
harder in Python.
IV. Python – a tool, not a reptile | Why Python, why not? Python 3
- all the newer versions of Python 3 are backwards - The source code of PyPy is not run in the
compatible with the previous versions of Python 3. interpretation manner but is instead translated into
the C programming language and then executed
There is more than one Python: CPython and Cython separately.
- is rather a tool for people developing Python than
Python aka CPython for the rest of the users.
1. There are Pythons which are maintained by people - compatible with the Python 3 language.
gathered around the PSF ((Python Software Foundation),
a community that aims to develop, improve, expand, and VII. Begin your Python journey
popularize Python and its environment. The PSF's
president is Guido von Rossum himself, and for this How to get Python and how to get to use it
reason, these Pythons are called: Several ways to get a copy of Python 3 depending on the
- Canonical; operating system used:
- reference Pythons: any other implementation of the 1. Linux users most probably have Python already
language should follow all standards established by installed
the PSF. - this is the most likely scenario, as Python's
infrastructure is intensively used by many Linux OS
CPython components:
- All Pythons coming from the PSF are written in the  package managers
"C" language.  graphical environments
- Python may be easily ported and migrated to all - If you're a Linux user, open the terminal/console,
platforms with the ability to compile and run "C" and type:
language programs (virtually all platforms have this
feature, which opens up many expansion python3
opportunities for Python).
at the shell prompt, press Enter and wait.
Cython
- one of a possible number of solutions to the most If you see something like this:
painful of Python's trait - the lack of efficiency. Large
and complex mathematical calculations may be Python 3.4.5 (default, Jan 12 2017, 02:28:40)
easily coded in Python (much easier than in "C" or [GCC 4.2.1 Compatible Clang 3.7.1
any other traditional language), but the resulting (tags/RELEASE_371/final)] on linux
code's execution may be extremely time-consuming. Type "help", "copyright", "credits" or "license" for
- automatically translate the Python code (clean and more information.
clear, but not too swift) into "C" code (complicated >>>
and talkative, but agile.).
then you don't have to do anything else.
VI. There is more than one python: Jython, PyPy and 2. If Python 3 is absent, then refer to your Linux
RPython documentation in order to find how to use your package
manager to download and install a new package - the
Jython one you need is named python3 or its name begins with
Jython that.
- A Python written in Java 3. All non-Linux users can download a copy at
- the current Jython implementation follows Python 2 https://www.python.org/downloads/.
standards. There is no Jython conforming to Python
3, so far. VIII. Begin your Python journey | Downloading and installing
Python
PyPy and RPython 1. Click the appropriate version you want
PyPy - Select Python 3. The site always offers the latest
- a Python within a Python version of it.
- a Python environment written in Python-like - If you're a Windows user, start the
language named RPython (Restricted Python). It is downloaded .exe file and follow all the steps.
actually a subset of Python. Leave the default settings the installer suggests
for now, with one exception - look at the
checkbox named Add Python 3.x to PATH and
check it.
- If you're a macOS user, a version of Python 2
may already have been preinstalled on your
computer, but since we will be working with
Python 3, you will still need to download and
install the relevant .pkg file from the Python
site.

IX. Begin your Python journey - The editor window is currently untitled, but it's good
practice to start work by naming the source file. Click
Starting your work with Python File (in the new window), then click Save as..., select
Tools: a folder for the new file (the desktop is a good place
1. an editor which will support you in writing the code (it for your first programming attempts) and chose a
should have some special features, not available in name for the new file.
simple tools); this dedicated editor will give you more
than the standard OS equipment;
2. a console in which you can launch your newly written
code and stop it forcibly when it gets out of control;
3. a tool named a debugger, able to launch your code step
by step and allowing you to inspect it at each moment of
execution.

IDLE is an acronym: Integrated Development and Learning


Environment.
Note: don't set any extension for the file name you are going
to use. Python needs its files to have the .py extension, so you
should rely on the dialog window's defaults. Using the
standard .py extension enables the OS to properly open these
files.

How to write and run your very first program


1. Put just one line into your newly opened and named
editor window.

The line looks like this:

print("Hisssssss...")
How to write and run your very first program
1. The first step is to create a new source file and fill it with
- You can use the clipboard to copy the text into the
code.
file.
- Click File in the IDLE’s menu and choose New file.
- Take a closer look at the quotation marks. These are
IDLE opens a new window for you. You can use it to
the simplest form of quotation marks (neutral,
write and amend your code. This is the editor
straight, dumb, etc.) commonly used in source files.
window. Its only purpose is to be a workplace in
Do not try to use typographic quotes (curved, curly,
which your source code is treated. Do not confuse
smart, etc.), used by advanced text processors, as
the editor window with the shell window. They
Python doesn’t accept them.
perform different functions.
- A new window appears – it says that the interpreter
has encountered an EOF (end-of-file) although (in its
opinion) the code should contain some more text.
 The editor window shows clearly where it
happened.

2. If everything goes okay and there are no mistakes in the


code, the console window will show you the effects
caused by running the program.
- In this case, the program hisses.
4. Fix the code now. It should look like this:

print("Hisssssss...")

Run it to see if it "hisses" again.

How to spoil and fix your code


1. Start IDLE.
- Click File, Open, point to the file you saved previously
and let IDLE read it in.
- Try to run it again by pressing F5 when the editor
window is active. 5. Let's spoil the code one more time. Remove one letter
 As you can see, IDLE is able to save your from the word print. Run the code by pressing F5. As you
code and retrieve it when you need it again. can see, Python is not able to recognize the error.
2. IDLE contains one additional and helpful feature. - The error message generated for the previous error
 First, remove the closing parenthesis. is quite different from the first one.
 Then enter the parenthesis again.
 Every time you put the closing parenthesis
in your program, IDLE will show the part of
the text limited with a pair of corresponding
parentheses. This helps you to remember to
place them in pairs.

- This is because the nature of the error is different


and the error is discovered at a different stage of
interpretation.
- The editor window will not provide any useful
information regarding the error, but the console
windows might.
3. Remove the closing parenthesis again. The code becomes - The message (in red) shows (in the subsequent
erroneous. It contains a syntax error now. IDLE should lines):
not let you run it.  the traceback (which is the path that the code
- Try to run the program again. IDLE will remind you to traverses through different parts of the
save the modified file. Follow the instructions. program);
 the location of the error (the name of the file
containing the error, line number and module
name); note: the number may be misleading, as
Python usually shows the place where it first
notices the effects of the error, not necessarily
the error itself;
 the content of the erroneous line; note: IDLE’s
editor window doesn’t show line numbers, but it
displays the current cursor location at the
bottom-right corner; use it to locate the
erroneous line in a long source code;
 the name of the error and a short explanation.

X. Course Interfaces

Sandbox
Sandbox
- allows Python code to be run in an Internet browser.
- It is a tool integrated within the course, which can be
used as a browser-based Python sandbox that
allows you to test code discussed throughout the
course, as well as an interpreter that enables you to
launch, perform and test the lab exercises
specifically designed for this course.
3 Main Parts
1. the Editor window, which lets you type in your code,
2. the Console window, which lets you see the output of
your programs,
3. a tool named the Action Buttons bar, which lets you run
your code, refresh the editor window, download your
program as a .py file, upload a .py file that will be
displayed in the editor, report a bug (in case you spot
anything wrong, do let us know about it!)
and the Settings button, which lets you adjust display
settings and switch between Python/C/C++
environments.

Course Interfaces
1. Study Interface
- one or two windows with text and images/animation
2. Sandbox Interface
- which you can use to try out your own code
- click Sandbox to switch to the Sandbox Interface
3. Practice Interface
- which enables you to study new things and do
coding tasks at the same time.
- consists of a text area on the left and the
Editor/Console windows on the right.
4. Quiz/Test Interface
- which will let you check your knowledge and skills to
see how well you have mastered the study material.

You might also like