DOWNLOAD
Click here to download the free PyScripter program that is used in the tutorial videos (and that you can use to practice your programming).
DEFINITIONS
Python – Python is a general-purpose, interpreted high-level programming language whose design philosophy emphasizes code readability. Its syntax is said to be clear and expressive. Python has a large and comprehensive standard library.
Python supports multiple programming paradigms, primarily but not limited to object-oriented, imperative and, to a lesser extent, functional programming styles. It features a fully dynamic type system and automatic memory management, similar to that of Scheme, Ruby, Perl, and Tcl. Like other dynamic languages, Python is often used as a scripting language, but is also used in a wide range of non-scripting contexts.
Python is a general-purpose, interpreted high-level programming language whose design philosophy emphasizes code readability.
Python has been successfully embedded in a number of software products as a scripting language, including Abaqus, 3D animation packages such as Blender, Cinema 4D, MotionBuilder, Softimage, the visual effects compositor Nuke, GIMP, Inkscape, Scribus, Paint Shop Pro and many others.
Computer Science – Computer science is the scientific and practical approach to computation and its applications. A computer scientist specializes in the theory of computation and the design of computational systems.
PYTHON STATEMENTS AND CONTROL FLOW
Python’s statements include (among others):
* The if statement, which conditionally executes a block of code, along with else and elif (a contraction of else-if).
* The for statement, which iterates over an iterable object, capturing each element to a local variable for use by the attached block.
* The while statement, which executes a block of code as long as its condition is true.
* The try statement, which allows exceptions raised in its attached code block to be caught and handled by except clauses; it also ensures that clean-up code in a finally block will always be run regardless of how the block exits.
* The class statement, which executes a block of code and attaches its local namespace to a class, for use in object-oriented programming.
* The def statement, which defines a function or method.
* The with statement (from Python 2.5), which encloses a code block within a context manager (for example, acquiring a lock before the block of code is run, and releasing the lock afterwards).
* The pass statement, which serves as a NOP. It is syntactically needed to create an empty code block.
* The assert statement, used during debugging to check for conditions that ought to apply.
* The yield statement, which returns a value from a generator function. (From Python 2.5, yield is also an operator. This form is used to implement coroutines – see below.)
* The import statement, which is used to import modules whose functions or variables can be used in the current program.
TYPING
Python uses duck typing and has typed objects but untyped variable names. Type constraints are not checked at compile time; rather, operations on an object may fail, signifying that the given object is not of a suitable type. Despite being dynamically typed, Python is strongly typed, forbidding operations that are not well-defined (for example, adding a number to a string) rather than silently attempting to make sense of them.
Python allows programmers to define their own types using classes, which are most often used for object-oriented programming. New instances of classes are constructed by calling the class (for example, SpamClass() or EggsClass()), and the classes themselves are instances of the metaclass type (itself an instance of itself), allowing metaprogramming and reflection.
Prior to version 3.0, Python had two kinds of classes: “old-style” and “new-style”. Old-style classes were eliminated in Python 3.0, making all classes new-style. In versions between 2.2 and 3.0, both kinds of classes could be used. The syntax of both styles is the same, the difference being whether the class object is inherited from, directly or indirectly (all new-style classes inherit from object and are instances of type).
Here is a summary of Python 3’s built-in types:
| TYPE | DESCRIPTION | SYNTAX EXAMPLE |
|---|---|---|
| str | An immutable sequence of Unicode codepoints. | ‘Wikipedia’ “Wikipedia” “””Spanning multiple lines””” |
| bytearray | A mutable sequence of bytes. | bytearray(b’Some ASCII’) bytearray(b”Some ASCII”) |
| bytes | An immutable sequence of bytes. | b’Some ASCII’ b”Some ASCII” |
| list | Mutable, can contain mixed types. | [4.0, ‘string’, True] |
| tuple | Immutable, can contain mixed types. | (4.0, ‘string’, True) |
| set, frozenset | Unordered, contains no duplicates. A frozenset is immutable. | set([4.0, ‘string’, True]) {4.0, ‘string’, True} frozenset([4.0, ‘string’, True]) |
| dict | A mutable group of key and value pairs. | {‘key1’: 1.0, 3: False} |
| int | An immutable integer of unlimited magnitude. | 42 |
| float | An immutable floating point number (system-defined precision). | 3.1415927 |
| complex | An immutable complex number with real and imaginary parts. | 3+2.7j |
| bool | An immutable truth value. | True False |
