Differences between revisions 6 and 21 (spanning 15 versions)
Revision 6 as of 2013-04-12 22:49:55
Size: 1803
Comment: On multiline strings: I consider this a pretty wrong and misleading example. I expectet a string with multiple lines, not multiple lines that create a single-line string, which has no line-break!
Revision 21 as of 2020-03-20 17:37:52
Size: 2659
Comment: Undo editing conflict
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
print 'Hello world!' print('Hello world!')
Line 8: Line 8:

Line 14: Line 12:
print 'Hello, %s' % name print('Hello, %s' % name) # string interpolation
print('Hello, {}'.format(name)) # string formatting
Line 16: Line 15:

Line 24: Line 21:

Line 29: Line 24:
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:
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:
Line 39: Line 34:
    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)
    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)
Line 43: Line 38:

Line 54: Line 47:

Line 62: Line 53:

Line 67: Line 56:
numbered_words=dict()
numbered_words[2]='world'
numbered_words[1]='Hello'
numbered_words[3]='!'
numbered_words = dict()
numbered_words[2] = 'world'
numbered_words[1] = 'Hello'
numbered_words[3] = '!'
Line 72: Line 61:

Line 78: Line 65:
    if value==wanted_value:     if value == wanted_value:
Line 83: Line 70:

Line 88: Line 73:
string=('This is a '
        'multiline string')
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
}}}
List comprehension

{{{#!python numbers=disable
l = [x**2 for x in range(4)]
print(l)
# [0, 1, 4, 9]
}}}
Set comprehension with condition

{{{#!python numbers=disable
squares = {x**2 for x in [0,2,4] if x < 4}
print(squares)
# {0, 4}
Line 93: Line 111:
Defining a for loop

{{{#!python numbers=disable
for x in xrange(1,4):
    print ('Hello, new Python user!'
           'This is time number %d') % (x)
}}}

Here are some samples to help get a better idea of Python's syntax:

Hello World (the traditional first program)

print('Hello world!')

String formatting

name = 'Monty'
print('Hello, %s' % name)  # string interpolation
print('Hello, {}'.format(name))  # string formatting

Defining a function

def add_one(x):
    return x + 1

Testing variable equality

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

class Talker(object):
    def greet(self, name):
        print 'Hello, %s!' % name
    def farewell(self, name):
        print 'Farewell, %s!' % name

Defining a list

dynamic_languages = ['Python', 'Ruby', 'Groovy']
dynamic_languages.append('Lisp')

Defining a dictionary

numbered_words = dict()
numbered_words[2] = 'world'
numbered_words[1] = 'Hello'
numbered_words[3] = '!'

Defining a while loop

while True:
    if value == wanted_value:
        break
    else:
        pass

Defining multiline strings

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

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

for x in xrange(1, 4):
    print ('Hello, new Python user!'
           'This is time number %d') % x

List comprehension

l = [x**2 for x in range(4)]
print(l)
# [0, 1, 4, 9]

Set comprehension with condition

squares = {x**2 for x in [0,2,4] if x < 4}
print(squares)
# {0, 4}


CategoryDocumentation

BeginnersGuide/Programmers/SimpleExamples (last edited 2020-03-20 17:37:52 by MarcAndreLemburg)

Unable to edit the page? See the FrontPage for instructions.