Security and Privacy in a Networked World/Programming in Python: erinevus redaktsioonide vahel

Allikas: KakuWiki
Mine navigeerimisribaleMine otsikasti
Resümee puudub
60. rida: 60. rida:


(Note: this is one of the three main variants of a cycle in programming)
(Note: this is one of the three main variants of a cycle in programming)
== Basics of Python ==

Redaktsioon: 16. märts 2014, kell 18:03

NOTE: This topic makes use of Wikipedia articles to provide the basic understanding of the matters. These articles deal with technology and are not much disputed, having reached the common knowledge stage. They also provide some good links for further study under the reference sections.


Python?

Python is a programming language developed by Guido van Rossum in 1989. It is a full-fledged, generic programming language with many different uses, in networking security it is often used as a tool to write short scripts for managing (and sometimes also, attacking) computer networks. Other reasons for choosing Python as the example language for this course include:

  • Python is often deemed as a good choice to start learning programming with, as its syntax is relatively simple.
  • Python directs its users towards good programming style by using indenting to mark structural blocks in programs (most other well-known languages - e.g. Java, C or Pascal - allow free placement of program statements and denote blocks with keywords or symbols).
  • As fully free and open-source software, it is included by default in most modern Unix-based operating systems (including OS X as well as almost all Linux distributions and BSD Unixes).
  • Python is also used to write some system software, e.g. the graphic installers used by Ubuntu, Red Hat Enterprise Linux and Fedora.
  • It is used for writing scripts in LibreOffice (similar to the role of Visual Basic for Applications used in MS Office), GIMP graphics package and a number of graphics/animation packages like 3D Max, Maya, Lightwave and others.

Note that Python is currently available in two parallel flavours. Python 2 was released in 2000 and its current stable version is 2.7. However, in 2008 the creator of Python decided to fix some inconsistencies in the language, resulting in the new Python 3 being incompatible with Python 2. Most systems that include Python have opted for Python 2 so far - therefore, this version (2.7) is also used at our course.

READ MORE at Wikipedia: https://en.wikipedia.org/wiki/Python_%28programming_language%29

...

Basics of programming

Note: this section gives a very rudimentary overview of programming, as plenty of online materials (on Python and other languages) is available.

Languages

Programming is typically done in so-called high-level languages - they are command systems that could be described as something in between of math and human languages. First widely used languages date back to the 60s (FORTRAN, COBOL), current popular choices include C (with its derivatives like C++, C# and others), Java, Python, Ruby, BASIC (while the language has long history, its current versions like Microsoft's Visual Basic are modern languages) and others.

High-level languages are designed to be understood by people rather than computers. To be understood by the computer (or rather, the operating system), they must be translated into machine code. Based on the method of translation, programming languages can be roughly divided into two categories (today, several intemediate and hybrid methods also exist):

  • interpreted languages - the program is read and executed by special software (the language interpreter) row by row (more exactly, statement by statement).
  • compiled languages - use another kind of software (the compiler) that translates the whole program (provided it is error free), resulting in a machine-language version of the program. The compiled program is a separate entity that can be run, copied etc (the .exe files in MS Windows are a well-known example of compiled programs). However, in order to make changes, the original high-level version (called the source code) is typically needed - after making changes, the source program must be re-compiled.

READ MORE: https://en.wikipedia.org/wiki/High-level_programming_language

Main elements of a program

At the most general level, a program consists of three kinds of "building blocks":

  • sequence
  • choice
  • cycle

We may illustrate it with a simple recipe for oat porridge:

"Take a cup of oat flakes and a glass of milk. Pour milk to a pot and heat it. Put oats into the pot and stir. If the mix starts to boil, cut down on heat and let it simmer for 10 minutes. Keep stirring until the porridge is ready. Add some salt and sugar - if you plan to eat the porridge with hot butter, keep it more salty, otherwise make it sweet."

Here, most instructions are given as a sequence - "do this, then that". However there are two choices - the first is at the condition "the porridge boils" - in a programming language, it can be written like

IF porridge boils THEN cut down on heat

Another choice is at the end - we may write it like this:

IF I plan to eat the porridge with hot butter THEN
   Add more salt than sugar
ELSE
   Add more sugar than salt

Finally, there is also a cycle - a part of program that needs to be repeated until an ending condition appears. We may write

DO
  let the porridge simmer
UNTIL 10 minutes have passed

(Note: this is one of the three main variants of a cycle in programming)


Basics of Python