734
Comment:
|
1720
Added more examples.
|
Deletions are marked like this. | Additions are marked like this. |
Line 9: | Line 9: |
Line 16: | Line 17: |
Line 22: | Line 24: |
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'] print 'Names list is equal to words: %s' % (names==words) new_names=['Donald','Jake','Phil'] print 'New names list is equal to names: %s' % (new_names==names) }}} |
|
Line 33: | Line 52: |
Line 39: | Line 59: |
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 ' 'multiline string') }}} 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
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']
print 'Names list is equal to words: %s' % (names==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 '
'multiline string')
Defining a for loop
for x in xrange(1,4):
print ('Hello, new Python user!'
'This is time number %d') % (x)