Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Sarah Farley High level language Extensible No compilation or linking Type python on the command line to start the interpreter python -c command [arg] is an alternative Exit by ctl- d or quit() >>> Is the prompt for next command … indicates a continuation Comments are indicated with # +, -, *, / act as you would expect them to >>> (50-5*6)/4 • 5 >>> 2+2 • 4 >>> • -3 7/-3 = is used to assign variables >>> width = 20 • >>> height = 5*9 • >>> width * height • 900 Values can be assigned simultaneously >>> x = y = z = 0 When working with mixed operators, integers are turned into floats >>> 3 * 3.75 / 1.5 • 7.5 >>> • 3.5 7.0 / 2 Strings can be declared with ‘ or “. >>> 'spam eggs' • 'spam eggs' >>> 'doesn\'t' "doesn't" >>> "doesn't" • "doesn't" >>> '"Yes," he said.' • '"Yes," he said.' >>> "\"Yes,\" he said." • '"Yes," he said.' >>> '"Isn\'t," she said.' • '"Isn\'t," she said.' To input string across multiple lines use the / character hello = "This is a rather long string containing\n\ several lines of text just as you would do in C.\n\ Note that whitespace at the beginning of the line is\ significant." Or “”” print """ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """ To get a raw string, you add an r in front >>>hello = r"This is a rather long string containing\n\ several lines of text much as you would do in C.“ It prints the string exactly ignoring things like newlines …This is a rather long string containing\n\ several lines of text much as you would do in C. Strings are concatenated with + and repeated with * >>> word = 'Help' + 'A' >>> word • 'HelpA' >>> '<' + word*5 + '>' • '<HelpAHelpAHelpAHelpAHelpA>' Placing two literal strings next to each other automatically concatenates them. >>> 'str' 'ing' • 'string' Strings are indexed, starting at 0 >>> word[4] • 'A' >>> word[0:2] • 'He' >>> word[2:4] • 'lp' Strings can also be sliced. >>> word[:2] • 'He' >>> word[2:] • 'lpA' Giving negative numbers for the indexes starts the string at the right instead of the left List do not need to have the same data type throughout >>> a = ['spam', 'eggs', 100, 1234] >>> a • ['spam', 'eggs', 100, 1234] Like strings, the index starts at 0 and lists can be sliced, concatonated, etc. It is possible to change a list once its been made. >>> # Replace some items: ... a[0:2] = [1, 12] >>> a [1, 12, 123, 1234] >>> # Remove some: ... a[0:2] = [] >>> a [123, 1234] >>> # Insert some: ... a[1:1] = ['bletch', 'xyzzy'] >>> a [123, 'bletch', 'xyzzy', 1234] >>> # Insert (a copy of) itself at the beginning >>> a[:0] = a >>> a [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234] >>> # Clear the list: replace all items with an empty list >>> a[:] = [] >>> a [] >>> x = int(raw_input("Please enter an integer: ")) Please enter an integer: 42 >>> if x < 0: ... x = 0 ... print 'Negative changed to zero' ... elif x == 0: ... print 'Zero' ... elif x == 1: ... print 'Single' ... else: ... print 'More' >>> # Measure some strings: ... a = ['cat', 'window', 'defenestrate'] >>> for x in a: ... print x, len(x) ... cat 3 window 6 defenestrate 12 >>> range(5, 10) • [5, 6, 7, 8, 9] >>> range(0, 10, 3) • [0, 3, 6, 9] >>> range(-10, -100, -30) • [-10, -40, -70] The break statement breaks out of the for or while loop. The continue statement continues onto the next section of the loop. The else clause terminates when the list exhausts or becomes false. The pass funtion is a placeholder. It doesn’t do anything. def() is the default argument for a function. def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): They have the form keyword = value. def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'): print "-- This parrot wouldn't", action, print "if you put", voltage, "volts through it." print "-- Lovely plumage, the", type print "-- It's", state, "!" http://docs.python.org/tutorial/index.ht ml