文档介绍:Python language
Intro
Resources
/
http://web./
/
http://proquest./
Search for “python”
Python
The interpreter
Starting python, interpreting statements, import
Variables, types
Integers, floats, booleans, strings, lists, tuples, dictionaries
Conditionals
Iteration
Functions
Input and output
Classes (object-oriented programming)
Constructors
The Interpreter
Starting python
Interactive mode
In Unix, typically type “python”
python -i <filename>
Non-interactive mode
python <filename>
Interactive mode: statements directly interpreted
.,
a = 10 ## assign variable ‘a’ the value 10
Typing <var-name> and <return> will give you value of variable in interactive mode
import statement lets you read in other files
Don’t supply the .py extension
.,
import lab1 ## imports
import sys ## a standard library; so we can use the write function
Variables, types
Python is an interpreted, loosely typed language
You can dynamically change the types of variables
Variables are implicitly declared when you assign to them for the first time
Example
chris% python
Python (#1, 11/12/02, 23:31:59)
[GCC Apple cpp-p ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a=10
>>> a = "abc"
>>> a = [1, 2, 3, 4]
>>>
Variable names are case sensitive
A = 10
a = 10 ## two variables with value 10
Built-in Primitive types
Integers
Strings; “abc”, ‘abc’, ‘’(empty string)
immutable
Long integers (unlimited size); a = 1000L
booleans (Python )
floating plex numbers; 3+4j
String Operations
+ (concatenate)
* (repeat)
[i] (index)
Indices start at 0; negative indices start at right; -1 rightmost
[i:j] (slice)
Return a substring starting at position i, and going until position j-1
Either i or j can be unspecified, which means the start or end of the string respectively
len(aString) (length)
for aChar in aString (iteration)
aChar in string (membership)
Other operations
String module
import string
Example - 1
Write a Python program to reverse