Вы находитесь на странице: 1из 8

PYTHON

CS206 PROGRAMMING LANGUAGES

HISTORY
Python was created in the early 1990s by Guido van Rossum at Stichting Mathematisch Centrum (CWI) in
the Netherlands as a successor of a language called ABC. Guido is Python's principal author, although it
includes many contributions from others. The last version released from CWI was Python 1.2. In 1995,
Guido continued his work on Python at the Corporation for National Research Initiatives (CNRI) in Reston,
Virginia where he released several versions of the software. Python 1.6 was the last of the versions
released by CNRI. In 2000, Guido and the Python core development team moved to BeOpen.com to form
the BeOpen PythonLabs team. Python 2.0 was the first and only release from BeOpen.com.
Following the release of Python 1.6, and after Guido van Rossum left CNRI to work with commercial
software developers, it became clear that the ability to use Python with software available under the GNU
Public License (GPL) was very desirable. CNRI and the Free Software Foundation (FSF) interacted to
develop enabling wording changes to the Python license. Python 1.6.1 is essentially the same as Python
1.6, with a few minor bug fixes, and with a different license that enables later versions to be
GPLcompatible. Python 2.1 is a derivative work of Python 1.6.1, as well as of Python 2.0.
After Python 2.0 was released by BeOpen.com, Guido van Rossum and the other PythonLabs developers
joined Digital Creations. All intellectual property added from this point on, starting with Python 2.1 and
its alpha and beta releases, is owned by the Python Software Foundation (PSF), a nonprofit modeled
after the Apache Software Foundation. See http://www.python.org/psf/ for more information about the
PSF.

3CS-C; LOPEZ, MELDRICK ALFRED TROY S., SANCHEZ, MARIUS DOMINIC

PYTHON

CS206 PROGRAMMING LANGUAGES

NAMES, BINDINGS, AND SCOPES


Python keywords and names are case-sensitive.
Python keywords are spelled in lowercase letters and are color-coded in orange in an IDLE
window. All Python names are color-coded in black, except when they are introduced as function,
class, or method names, in which case they appear in blue.
A name can begin with a letter or an underscore (_), followed by any number of letters,
underscores, or digits.
The names of modules, variables, functions, and methods are spelled in lowercase letters. With
the exception of modules, when one of these names contains one or more embedded names, the
embedded names are capitalized (camel notation per se).
The names of classes follow the same conventions but begin with a capital letter. When a variable
names a constant, all the letters are uppercase, and an underscore separates any embedded
names.
The following table shows examples of these naming conventions:
Type of Name
Variable
Constant
Function or method
Class

Examples of Python Naming Conventions


Examples
salary, hoursWorked, isAbsent
ABSOLUTE_ZERO, INTEREST_RATE
printResults, cubeRoot, isEmpty
BankAccount, SortedSet

Python allows:
dynamic type binding
static scoping and global scoping
nested subprograms
Data types are strongly and dynamically typed. Mixing incompatible types causes an exception
to be raised, so errors are caught sooner.
In Python, any variable can name a value of any type. Variables are not declared to have a type,
as they are in many other languages; they are simply assigned a value.
Consequently, data type names almost never appear in Python programs. However, all values or
objects have types.
The types of operands in expressions are checked at run time, so type errors do not go
undetected; however, the programmer does not have to worry about mentioning data types
when writing code.
You can use some data type names as type conversion functions. For example, when the user
enters a number at the keyboard, the input function returns a string of digits, not a numeric
value. The program must convert this string to an int or a float before numeric processing.
Like most other languages, Python allows operands of different numeric types in arithmetic
expressions. In those cases, the result type is the same as the most general operand type. For
example, the addition of an int and a float produces a float as the result.

3CS-C; LOPEZ, MELDRICK ALFRED TROY S., SANCHEZ, MARIUS DOMINIC

PYTHON

CS206 PROGRAMMING LANGUAGES

DATA TYPES
Primitive data types:
integer numeric values with no fractional part
-9
50
0x4F 077
floating-point numeric values with a fractional part
25.45 -17.4 0.34 17.045E-03
boolean logical values of true or false
True False
string an ordered sequence of alphanumeric characters
string
another string c
list an ordered sequence of objects in which each element is identified by an integer
subscript
[0, 1, 2, 3]
[abc, 1, 4.5]
[]
tuples similar to a list type but the contents of a tuple cannot be changed once it has
been created
(a, b, c)
(1, 4, 5, 8)
dictionary a collection of items in which each items contains a key and a
corresponding value
{123 : bob, 456 : sally}

3CS-C; LOPEZ, MELDRICK ALFRED TROY S., SANCHEZ, MARIUS DOMINIC

PYTHON

CS206 PROGRAMMING LANGUAGES

SYNTAX: EXPRESSION AND ASSIGNMENT STATEMENTS


A Python variable is introduced with an assignment statement. The syntax of a simple assignment
statement is:
<identifier> = <expression>
Several variables can be introduced in the same assignment statement, as follows:
minValue, maxValue = 1, 100
To swap the values of the variables a and b, you write
a, b = b, a
Assignment statements must appear on a single line of code, unless the line is broken after a
comma, parenthesis, curly brace, or square bracket. When these options are unavailable, another
means of breaking a line within a statement is to end it with the escape symbol \. You typically
place this symbol before or after an operator in an expression. Here are some admittedly
unrealistic examples:
minValue = min(100,
200)
product = max(100, 200) \
* 30
When you press Enter after a comma or the escape symbol, IDLE automatically indents the next
line of code.

stmt: simple_stmt | compound_stmt


simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
('=' (yield_expr|testlist_star_expr))*)
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
|
'<<=' | '>>=' | '**=' | '//=')

3CS-C; LOPEZ, MELDRICK ALFRED TROY S., SANCHEZ, MARIUS DOMINIC

PYTHON

CS206 PROGRAMMING LANGUAGES

test: or_test ['if' or_test 'else' test] | lambdef


test_nocond: or_test | lambdef_nocond
lambdef: 'lambda' [varargslist] ':' test
lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
or_test: and_test ('or' and_test)*
and_test: not_test ('and' not_test)*
not_test: 'not' not_test | comparison
comparison: expr (comp_op expr)*
comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
star_expr: '*' expr
expr: xor_expr ('|' xor_expr)*
xor_expr: and_expr ('^' and_expr)*
and_expr: shift_expr ('&' shift_expr)*
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
arith_expr: term (('+'|'-') term)*
term: factor (('*'|'@'|'/'|'%'|'//') factor)*
factor: ('+'|'-'|'~') factor | power
power: atom_expr ['**' factor]
atom_expr: [AWAIT] atom trailer*
atom: ('(' [yield_expr|testlist_comp] ')' |
'[' [testlist_comp] ']' |
'{' [dictorsetmaker] '}' |
NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))*
[','] )
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: subscript (',' subscript)* [',']
subscript: test | [test] ':' [test] [sliceop]
sliceop: ':' [test]
exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
testlist: test (',' test)* [',']
dictorsetmaker: ( ((test ':' test | '**' expr)
(comp_for | (',' (test ':' test | '**' expr))* [',']))
|
((test | star_expr)
(comp_for | (',' (test | star_expr))* [','])) )

3CS-C; LOPEZ, MELDRICK ALFRED TROY S., SANCHEZ, MARIUS DOMINIC

PYTHON

CS206 PROGRAMMING LANGUAGES

SYNTAX: CONTROL STRUCTURES


Python includes the usual array of control statements for sequencing, conditional execution, and
iteration. A sequence of statements is a set of statements written one after the other. Each
statement in a sequence must begin in the same column.
Conditional Statements
The structure of Pythons conditional statements is similar to that of other languages. The
keywords if, elif, and else are significant, as is the colon character and indentation.

compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt |


funcdef | classdef | decorated | async_stmt
async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
while_stmt: 'while' test ':' suite ['else' ':' suite]
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
yield_stmt
break_stmt: 'break'
continue_stmt: 'continue'
return_stmt: 'return' [testlist]

3CS-C; LOPEZ, MELDRICK ALFRED TROY S., SANCHEZ, MARIUS DOMINIC

PYTHON

CS206 PROGRAMMING LANGUAGES

SYNTAX: SUBPROGRAMS

decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE


decorators: decorator+
decorated: decorators (classdef | funcdef | async_funcdef)
async_funcdef: ASYNC funcdef
funcdef: 'def' NAME parameters ['->' test] ':' suite
parameters: '(' [typedargslist] ')'
typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [','
['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
tfpdef]]
| '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
tfpdef)
tfpdef: NAME [':' test]
varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [','
['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
vfpdef]]
| '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
vfpdef)
vfpdef: NAME

3CS-C; LOPEZ, MELDRICK ALFRED TROY S., SANCHEZ, MARIUS DOMINIC

| '**'
| '**'

| '**'
| '**'

PYTHON

CS206 PROGRAMMING LANGUAGES

References:

Lambert, K. (2014). Fundamentals of Python: Data structures. Boston, MA: Cengage Learning.
Necaise, R. (2011). Data structures and algorithms using Python. Danvers, MA: John Wiley & Sons.
https://docs.python.org/3/index.html
https://docs.python.org/3/reference/grammar.html

3CS-C; LOPEZ, MELDRICK ALFRED TROY S., SANCHEZ, MARIUS DOMINIC

Вам также может понравиться