Sunday 13 May 2012

Python Crib Sheet 01

Loaded Crib_Sheet01.py listed below

Run Crib_Sheet01.py listed below
# <-- that symbol starts a comment in the code


#  1   ---   Variables and assignment - Creating references

name = 'Betty'
program = name = "Python"
firstname,lastname = 'Fred','Flintstone'
# There is no need to declare variables.

#  2   ---    Control

# >>>note indentation:
# >>>    is important

print "if | elif | else statements code examples"  # Selecting actions
if name == 'Barney':                
    print program, version_of[program]
elif name != 'Wilma':
    print name
else:
    program = 'Bamm-Bamm'
    
print "for code example"     # Sequence iteration

for x in ['this is','a','list']:   
    print x
   
print "range code example"
for y in range(5):                    # 0 .. 4
    print y

print "while statement code example"
z = 3
while z>0:                            # General Loops
    print z
    z = z-1

print "Breaking out of a loop: code example"

a_list = ['Jill', 1, "Bob", 'Brown', 'Jones', 333, 333, 1234.5]
for name in a_list:
    if name == 'Jones':
         break
    print name

# 3   ---  Data   ---   Strings

string = 'abc'
print 'Concatenation works:' + 'abc' + 'def' + string

print '''
For multi-line strings use triple quotes.
For example this is a long sentence which
goes on beyond a single line.'''

print 'My name is %s' % name
print string
#    ------------    alternatively:
name = "Ted"
string = 'My name is ' + name
print string

No comments:

Post a Comment