
Python Caleb Lawson CSC 415: Programming Languages Dr. Lyle November 4, 2014 Python – Lawson History Development For Guido van Rossum, the weeks around Christmas were slow. Being the tinkerer he was, Guido van Rossum, could not keep himself from coding. During the Christmas holidays of 1989, he began developing the Python scripting language. Of course, Guido van Rossum did not build his language without a purpose in mind. Guido van Rossum worked at the National Research Institute for Mathematics and Computer Science in the Netherlands, known as CWI. There, he was on the development team for the Amoeba distributed operating system. System administration was difficult, with C programs and Bourne shell scripts being the only options. Amoeba had a custom system call interface which was not easily accessible with Bourne scripts. (“General Python FAQ”) Guido van Rossum's prior experiences developing the ABC computer language proved valuable. ABC was designed to be understood by intelligent computer users who lacked programming experience—and a replacement for BASIC. ABC did not take off, because of the prevalence of BASIC and the lack of efficient distribution mechanisms. Guido van Rossum realized that a scripting language with the syntax of ABC and access to Amoeba system calls was exactly what the Amoeba project needed—a language to “bridge the gap between shell and C.” Realizing that designing a case-specific language would prove to be less useful, Guido van Rossum made sure that Python would be extensible (Venners, “ABC's Influence on Python”). By February of 1991, he decided to post his work to USENET (“General Python FAQ”). Arguably accelerating its growth. By March 6th, 2001, the Python Software Foundation, a non- 2 Python – Lawson profit organization devoted to the Python programming language, was formed. The features of Python grew along with its multiple releases. Features of Python 1.0 (released in 1994) include the addition of functional programming tools, courtesy of a Lisp programmer (Van Rossum, “The Fate of Reduce() in Python 3000”). Additional features include name-mangling (a form of data hiding) and support for complex numbers. Python 2.0 added a garbage collection system, list comprehensions, support for nested scopes, and the unification of the types hierarchy (Kuchling and Zadka). Python 3.0, the most recent release, removed old syntax, converted print into a function, and unified string types (Van Rossum, “What's New In Python 3.0”). Design Philosophy Python, unlike ABC, was initially designed with the seasoned developer in mind. The initial goal of the project was to create a second language for programmers familiar with C and C++, where writing a C program was not effective. Many of the design principles came from Guido van Rossum's experience in developing ABC, with his own improvements baked in (Venners, “Python's Original Design Goals”). Eventually, as Python became a community-based project in the late 90's, the notion of Pythonic Zen came about. This was distilled into design aphorisms, called “The Zen of Python,” by none other than long-time Pythoneer Tim Peters. Python is designed to be beautiful, explicit, practical, simple, and readable (Peters). 3 Python – Lawson Overview of Language Design, Syntax, and Semantics Names, Bindings, and Scopes Python is a case-sensitive language; language reserved words are in lower case. Like Java, variable names can contain alphanumeric characters and underscores but cannot begin with a digit. Variables are essentially references to data objects that exist in memory (Sebesta, 260). They are typeless and are implicitly declared when they are the target of an assignment statement. An object in memory can be referred to by several different variables (Hetland, 14). Variables can be used to refer to functions (Hetland, 17). Python is dynamically typed, meaning that the object to which a variable is referring can be changed by the programmer at any point. That variable can refer to an object of any type, since the variable name is not bound to a type. Python is also a strongly typed language, meaning that a programmer cannot mix incompatible data types in operations. Python allows for global and local variables. Local variables exist inside of Python functions and methods. An interesting thing to note about Python is that a local variable with the same name as a preexisting global variable will not affect the value of the global variable (Hetland, 131). Defining a function creates a local scope. In Python, you can define functions within functions—called “nesting.” When this is done, the inner function can reference the variables of the outer function though static scoping (Hetland, 133) (Sebasta, 230). Data Types Python supports many data types right out of the box. Integers, floats, complex numbers, strings, lists, dictionaries, and tuples are built-in data types—all of which are memory objects. Since Python is both dynamically and strongly typed, Python implicitly converts only compatible 4 Python – Lawson types. Numeric types are implicitly cast, so mathematical operations can be performed without explicitly casting. Numeric types and string types are not compatible, and therefore, need to be cast or parsed before mathematical or stringwise operations can occur. (“Why Is Python a Dynamic Language and Also a Strongly Typed Language”) Integers are supported in Python. In versions prior to three, when an integer is divided by a floating point value, the results in floor division. When regular integers grow too large, Python implicitly converts the large value into a long integer. Long integers can have 'unlimited' length, but are not directly supported by Python. One can explicitly make a long integer like so: x = 12345678987650432L (Sebesta, 246). When a period is used in a number literal, a floating point number is created (ex. x= 3.14). This also happens when integers are divided by floating point numbers. Complex numbers are also supported, they are declared thusly: x = (8 + 9j) (Sebesta, 248). Container types in Python include strings, lists, maps, and tuples. Python supports strings as a primitive type, and behave as is traditional: an array of characters. Python encodes strings in unicode characters. Characters are not a separate data type in Python, but the same affect can be achieved through creating a string of length 1. Python has built-in string operations for substrings, concatenation, indexing, search and replace, and membership. Strings are immutable, meaning that strings are not directly modified—new strings are returned as a result of string operations. Python's arrays are called lists. Unlike arrays in most languages, Python lists are dynamic and can hold objects of any type. Consider this list: list = [ 1, 2.0, (6 + 2j), 'healthy 5 Python – Lawson tomatoes', 42 ]. Python lists are mutable; elements can be inserted, appended, modified, deleted, and sorted. Lists can also be concatenated (Sebesta, 266). The default lower-bound index for collection types accessed by index is 0. Sublists are referred to as slices and can be defined like this: a_list [100:200] or a_list [0:100:5] (Sebesta, 268). In the second example, every fifth element is taken. Dictionaries are another mutable collection type, they are unordered and are accessed in key-value pairs, e.g. telNum = { 'Janeway' : 5022056432, 'Papa Johns' : 2709088097 } (Sebesta, 272). Tuples are yet another container type, and they are immutable. Tuples contain an n-length number of values of mixed data types, seperated by commas. A tuple is declared thusly: tuple = 'Hank Hill', 54, 286.2. Tuples can be nested: tuple = tuple2, 4, tuple3 (Sebesta, 280). Sets are another container type, they are unordered and contain no duplicate values. Sets support set theory through union, intersection, difference and symmetric difference operations. Expressions and Assignment Structures In Python, standard mathematical operators +, -, *, /, %, and ** are in the usual order of precedence. The // operator in Python represents floor division. Compound assignment operators += and -= exist in Python (Sebesta, 337). Mixed-mode assignment does not exist in Python, because data types are associated with objects and not variable names. Multiple-target assignment is legal in Python (e.g. chris, dana, paul = “super special winners”) (Sebesta, 339). Multiple assignment statements can also be reduced into one by seperating the values with commas (e.g. or chris, paul = 'atkins', 'seaworthy'). All logical operators are short-circuit evaluated (Sebesta, 336). Comparison operators maintain a roughly mathematical meaning, a > 6 Python – Lawson b > c == a > b and b > c. However, when functions are used with comparison operators (e.g. a > function() > c), the function is only evaluated once which may yield unexpected results. Boolean operators treat non-boolean data object as True if they are non-empty and False if they are (i.e “”, 0, 0.0, [], {}). Python operators may be overloaded, for instance: def __subtract__(self, second): return self – (self * second). Statement-Level Control Structures Python contains the standard set of control statements that are familiar in other languages, like Java and C++. Python supports if, else if, and else statements. Python also supports for and while loops. The body of a control statement is indented to show that code belongs inside the control structure, instead of the curly braces seen in C-based languages (Sebesta, 351). The header of an if statement looks like this: if condition: print 'if executed'. To test additional conditions, an elif (else if) clause can be added: elif condition: print 'more stuff'. Additionally, if an else clause can be added: else: 'the else clause has been executed'. A complete if block can consist of only one initial if statement, and zero to many elif statements, and zero or one else statement. (“More Control Flow Tools”) While loops are one of the looping control structures included in Python. They are used to repeat a set of instructions while a condition is true.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages19 Page
-
File Size-