2473
Comment:
|
164
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
Here are some samples to help get a better idea of Python's syntax: Hello World (the traditional first program) {{{#!python numbers=disable print 'Hello world!' # Python 2 syntax # or print('Hello world!') # Python 3 syntax }}} String formatting {{{#!python numbers=disable name = 'Monty' print('Hello, %s' % name) # string interpolation print('Hello, {}'.format(name)) # string formatting }}} Defining a function {{{#!python numbers=disable def add_one(x): return x + 1 }}} Testing variable equality {{{#!python numbers=disable x = 1 y = 2 print 'x is equal to y: %s' % (x == y) z = 1 print 'x is equal to z: %s' % (x == z) names = ['Donald', 'Jake', 'Phil'] words = ['Random', 'Words', 'Dogs'] if names == words: print 'Names list is equal to words' else: print "Names list isn't equal to words" new_names = ['Donald', 'Jake', 'Phil'] print 'New names list is equal to names: %s' % (new_names == names) }}} Defining a class with two methods {{{#!python numbers=disable class Talker(object): def greet(self, name): print 'Hello, %s!' % name def farewell(self, name): print 'Farewell, %s!' % name }}} Defining a list {{{#!python numbers=disable dynamic_languages = ['Python', 'Ruby', 'Groovy'] dynamic_languages.append('Lisp') }}} Defining a dictionary {{{#!python numbers=disable numbered_words = dict() numbered_words[2] = 'world' numbered_words[1] = 'Hello' numbered_words[3] = '!' }}} Defining a while loop {{{#!python numbers=disable while True: if value == wanted_value: break else: pass }}} Defining multiline strings {{{#!python numbers=disable string = '''This is a string with embedded newlines. Also known as a tripled-quoted string. Whitespace at the beginning of lines is included, so the above line is indented but the others are not. ''' }}} Splitting a long string over several lines of source code {{{#!python numbers=disable string = ('This is a single long, long string' ' written over many lines for convenience' ' using implicit concatenation to join each' ' piece into a single string without extra' ' newlines (unless you add them yourself).') }}} Defining a for loop {{{#!python numbers=disable for x in xrange(1, 4): print ('Hello, new Python user!' 'This is time number %d') % x }}} ---- CategoryDocumentation |
I am Vincent from Forse. I love to play Air horn.<<BR>> <<BR>> Other hobbies are Equestrianism.<<BR>> <<BR>> my page - [[http://seoenergy.de|backlink aufbau]] |
I am Vincent from Forse. I love to play Air horn.
Other hobbies are Equestrianism.
my page - backlink aufbau