Differences between revisions 9 and 10
Revision 9 as of 2013-04-12 23:55:20
Size: 2423
Editor: stevenjd
Comment: Add version 3 Hello World example
Revision 10 as of 2013-04-13 00:00:55
Size: 2457
Editor: stevenjd
Comment: Make examples comply with PEP 8
Deletions are marked like this. Additions are marked like this.
Line 28: Line 28:
print 'x is equal to y: %s' % (x==y)
print 'x is equal to z: %s' % (x==z)
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 38: Line 38:
    print 'Names list isn\'t equal to words'
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 60: Line 60:
numbered_words = dict()
numbered_words[2] = 'world'
numbered_words[1] = 'Hello'
numbered_words[3] = '!'
Line 69: Line 69:
    if value==wanted_value:     if value == wanted_value:
Line 95: Line 95:
for x in xrange(1,4): for x in xrange(1, 4):
Line 97: Line 97:
           'This is time number %d') % (x)            '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!'  # Python 2 syntax

# or

print('Hello world!')  # Python 3 syntax

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'
    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']

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:

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.

Defining long strings over multiple lines

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


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

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