#!/usr/bin/env python
# Functions allow us to include code in our programs that will only run when
# explicitly called and can be reused.
# Simple function example.
def allStars(n):
for i in range(0,n):
print "*"
# Main Program
run = True
while run:
x = raw_input("How many stars should I print? ")
if str.isdigit(x):
allStars(int(x))
x = raw_input("Do you want another go? Press Y or N")
if str.upper(x) == "N":
run = False
else:
print "%s is not a number." % x
Tuesday, 28 July 2015
Flow Control 2: while, while-else
#!/usr/bin/env python
# Simple while loop example.
i = 0
while i != 10:
i = i + 1
print i
# while-else
while i != 10:
i = i +1
print i
else:
print "We're all done here!"
# Simple while loop example.
i = 0
while i != 10:
i = i + 1
print i
# while-else
while i != 10:
i = i +1
print i
else:
print "We're all done here!"
Flow Control 1: for, for-else
#!/usr/bin/env python
# Simple for loop example.
for i in range(0,11):
print "The value of i is %d." % i
# for-else example.
# This is taken directly from the Python documentation. The else statement
# looks as though it's in the wrong place. It's not. Python for loops can have
# else statements.
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'
# Simple for loop example.
for i in range(0,11):
print "The value of i is %d." % i
# for-else example.
# This is taken directly from the Python documentation. The else statement
# looks as though it's in the wrong place. It's not. Python for loops can have
# else statements.
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'
Strings 2
#!/usr/bin/env python
# Python has an unhealthy string obsession. There are many ways of converting
# and formatting strings.
A = "hello world"
# More formatting.
# https://docs.python.org/2.7/library/string.html#string-formatting
print "The first character of A is %s." % A[0] # Old method.
print "The first character of A is {!s}.".format(A[0]) # New method, Python 3.
# Some useful built in string methods.
# https://docs.python.org/2.7/library/stdtypes.html#string-methods
# Check and convert a string to all caps.
print(str.upper(A))
if str.isupper(A):
print(A)
else:
print(str.upper(A) + " : Converted with str.upper().")
print(str.swapcase(A) + " : Converted with str.swapcase().")
print(A + " : Original remains intact. Strings are immutable!")
# Check and convert a string to all lower case.
B = "HELLO WORLD"
print(str.lower(B))
if str.islower(B):
print(B)
else:
print(str.upper(B) + " : Converted with str.lower().")
print(str.swapcase(B) + " : Converted with str.swapcase().")
print(B + " : Original remains intact. Strings are immutable!")
# Be careful with str.swapcase()
print(str.swapcase("str.swapcase Literally swAps eACH CHARACTER'S case."))
# Capitalise the first letter only.
print(str.capitalize(A))
print(str.capitalize(B))
# Find a substring within a string.
stringToFind = "lo"
i = A.find(stringToFind)
print(A[i:i + len(stringToFind)] + " found at position %d") % i
# The last example uses a technique called slicing with the print statment.
# This is used to print a sub string and can infact be used on most list
# style objects with indices. The axample below is a bit simpler.
print(A[0:5])
# The first value in the square brackets is the index position to begin at.
# The second value is the number of positions we want to include in the slice.
# It is NOT an index value.
# So if we imagine our string "Hello World" is a row of boxes. What we're
# telling Python to do is start at index 0 and print only the next 5 boxes.
# The index value for the "o" in "Hello" will of course be 4 and not 5. This is
# because the indices in lists and strings begin at 0 and not 1.
# Python has an unhealthy string obsession. There are many ways of converting
# and formatting strings.
A = "hello world"
# More formatting.
# https://docs.python.org/2.7/library/string.html#string-formatting
print "The first character of A is %s." % A[0] # Old method.
print "The first character of A is {!s}.".format(A[0]) # New method, Python 3.
# Some useful built in string methods.
# https://docs.python.org/2.7/library/stdtypes.html#string-methods
# Check and convert a string to all caps.
print(str.upper(A))
if str.isupper(A):
print(A)
else:
print(str.upper(A) + " : Converted with str.upper().")
print(str.swapcase(A) + " : Converted with str.swapcase().")
print(A + " : Original remains intact. Strings are immutable!")
# Check and convert a string to all lower case.
B = "HELLO WORLD"
print(str.lower(B))
if str.islower(B):
print(B)
else:
print(str.upper(B) + " : Converted with str.lower().")
print(str.swapcase(B) + " : Converted with str.swapcase().")
print(B + " : Original remains intact. Strings are immutable!")
# Be careful with str.swapcase()
print(str.swapcase("str.swapcase Literally swAps eACH CHARACTER'S case."))
# Capitalise the first letter only.
print(str.capitalize(A))
print(str.capitalize(B))
# Find a substring within a string.
stringToFind = "lo"
i = A.find(stringToFind)
print(A[i:i + len(stringToFind)] + " found at position %d") % i
# The last example uses a technique called slicing with the print statment.
# This is used to print a sub string and can infact be used on most list
# style objects with indices. The axample below is a bit simpler.
print(A[0:5])
# The first value in the square brackets is the index position to begin at.
# The second value is the number of positions we want to include in the slice.
# It is NOT an index value.
# So if we imagine our string "Hello World" is a row of boxes. What we're
# telling Python to do is start at index 0 and print only the next 5 boxes.
# The index value for the "o" in "Hello" will of course be 4 and not 5. This is
# because the indices in lists and strings begin at 0 and not 1.
Location:
Scotland, UK
Strings 1
#!/usr/bin/env python
# The first thing to note about strings is that they are basically lists of
# characters. This means all elements of a string can be accessed directly
# using their indices.
# Python strings effectively have two indices. The first starts at 0 for the
# first character on wards. The second starts at -1 from the last character.
A = "Hello World"
print "The first character of A is \"%s\"." % A[0]
print "The last charachter of A is \"%s\"." % A[-1]
# The code above looks very simple. And it is. However a number of thing are
# happening that would normally require a lot more code in Java or C.
# 1. A is declaired and it's value set as the string "Hello World".
# 2. Python is told to print the first and last characters.
# 3. ASCII escape characters are used to enclose the first and last characters
# in double quotes. Getting to grips with these will save a lot of code.
# 4. We perform a substitution instead of concatinating multiple strings.
# Concatinated version.
print ('The first character of A is ' + '"' + A[0] + '".')
print ('The first character of A is ' + '"' + A[-1] + '".')
# Slightly more complex examples.
B = ["first","second","third","fourth","last","second last"]
print ("The %s character of A is \"%s\".") % (B[0],A[0])
print ("The %s character of A is \"%s\".") % (B[1],A[1])
print ("The %s character of A is \"%s\".") % (B[4],A[-1])
print ("The %s character of A is \"%s\".") % (B[5],A[-2])
print ('The ' + B[0] + ' character of A is ' + '"' + A[0] + '".')
print ('The ' + B[1] + ' character of A is ' + '"' + A[1] + '".')
print ('The ' + B[4] + ' character of A is ' + '"' + A[-1] + '".')
print ('The ' + B[5] + ' character of A is ' + '"' + A[-2] + '".')
# Integer substition example.
print ("The %s character of A is \"%s\". A is %d characters long.") % (B[4],
A[-2],len(A))
print ('The ' + B[4] + ' character of A is ' + '"' + A[-1] + '". A is '
+ str(len(A)) + ' characters long.')
# Note that to substitute in an integer %d is used instead of %s. Conversion of
# the integer happens automatically. However in the concatination version the
# conversion must be done manually. Which isn't a problem in this example. But
# it would make larger programs harder to read and maintain.
# More information on string formatting can be found here,
# https://docs.python.org/2/library/stdtypes.html#string-formatting-operations
# The first thing to note about strings is that they are basically lists of
# characters. This means all elements of a string can be accessed directly
# using their indices.
# Python strings effectively have two indices. The first starts at 0 for the
# first character on wards. The second starts at -1 from the last character.
A = "Hello World"
print "The first character of A is \"%s\"." % A[0]
print "The last charachter of A is \"%s\"." % A[-1]
# The code above looks very simple. And it is. However a number of thing are
# happening that would normally require a lot more code in Java or C.
# 1. A is declaired and it's value set as the string "Hello World".
# 2. Python is told to print the first and last characters.
# 3. ASCII escape characters are used to enclose the first and last characters
# in double quotes. Getting to grips with these will save a lot of code.
# 4. We perform a substitution instead of concatinating multiple strings.
# Concatinated version.
print ('The first character of A is ' + '"' + A[0] + '".')
print ('The first character of A is ' + '"' + A[-1] + '".')
# Slightly more complex examples.
B = ["first","second","third","fourth","last","second last"]
print ("The %s character of A is \"%s\".") % (B[0],A[0])
print ("The %s character of A is \"%s\".") % (B[1],A[1])
print ("The %s character of A is \"%s\".") % (B[4],A[-1])
print ("The %s character of A is \"%s\".") % (B[5],A[-2])
print ('The ' + B[0] + ' character of A is ' + '"' + A[0] + '".')
print ('The ' + B[1] + ' character of A is ' + '"' + A[1] + '".')
print ('The ' + B[4] + ' character of A is ' + '"' + A[-1] + '".')
print ('The ' + B[5] + ' character of A is ' + '"' + A[-2] + '".')
# Integer substition example.
print ("The %s character of A is \"%s\". A is %d characters long.") % (B[4],
A[-2],len(A))
print ('The ' + B[4] + ' character of A is ' + '"' + A[-1] + '". A is '
+ str(len(A)) + ' characters long.')
# Note that to substitute in an integer %d is used instead of %s. Conversion of
# the integer happens automatically. However in the concatination version the
# conversion must be done manually. Which isn't a problem in this example. But
# it would make larger programs harder to read and maintain.
# More information on string formatting can be found here,
# https://docs.python.org/2/library/stdtypes.html#string-formatting-operations
Monday, 27 July 2015
Making Decisions: if-elif-else
#!/usr/bin/env python
# Programs normally execute one line after the other from top to bottom.
# But sometimes we need to make a decision while the program is running.
# Some of these decisions will send the program down one path or the other.
# To do this in Python we use the if-elif-else statements.
# The first thing to note is Python uses indentation to define code blocks
# rather than the {} notation found in C or Java. This means in Python
# indentation of code blocks is manditory. We also don't actually
# type "then" as in "if condition is true then do somemthing". Instead we use
# a colon ":". Because why not?
# Basic if statement.
A = 0 # Integer.
if A == 0: print A
# A more complex example with indentation defining a code block.
A = [1,2,3,4,5] # List of integers.
if A[0] != 0:
print ("A at index 0 is equal to %d.") % A[0]
print ("A at index 0 is equal to %d.") % A[1]
print ("A at index 0 is equal to %d.") % A[2]
print ("A at index 0 is equal to %d.") % A[3]
print ("A at index 0 is equal to %d.") % A[4]
# Both the examples above will only print the value of A if the condition is
# met. But often we need alternative actions to be taken when the condition
# has not been met. For this we add the else statement.
A = 1
if A == 0:
print A
else:
print ("A is not 0.")
# By adding additional if statements we can test for multiple possibilities.
# We do this in Python by using elif.
A = 2
if A == 0:
print A
elif A == 1:
print A
elif A == 2:
print A
elif A == 3:
print A
else:
print ("The value of A does not match the test.")
# Programs normally execute one line after the other from top to bottom.
# But sometimes we need to make a decision while the program is running.
# Some of these decisions will send the program down one path or the other.
# To do this in Python we use the if-elif-else statements.
# The first thing to note is Python uses indentation to define code blocks
# rather than the {} notation found in C or Java. This means in Python
# indentation of code blocks is manditory. We also don't actually
# type "then" as in "if condition is true then do somemthing". Instead we use
# a colon ":". Because why not?
# Basic if statement.
A = 0 # Integer.
if A == 0: print A
# A more complex example with indentation defining a code block.
A = [1,2,3,4,5] # List of integers.
if A[0] != 0:
print ("A at index 0 is equal to %d.") % A[0]
print ("A at index 0 is equal to %d.") % A[1]
print ("A at index 0 is equal to %d.") % A[2]
print ("A at index 0 is equal to %d.") % A[3]
print ("A at index 0 is equal to %d.") % A[4]
# Both the examples above will only print the value of A if the condition is
# met. But often we need alternative actions to be taken when the condition
# has not been met. For this we add the else statement.
A = 1
if A == 0:
print A
else:
print ("A is not 0.")
# By adding additional if statements we can test for multiple possibilities.
# We do this in Python by using elif.
A = 2
if A == 0:
print A
elif A == 1:
print A
elif A == 2:
print A
elif A == 3:
print A
else:
print ("The value of A does not match the test.")
Location:
Scotland, UK
Variables and Data Types
#!/usr/bin/env python
# Python Variables and Data Types
# The first thing to know about a variable is that it is just a place in the
# computer's memory where data is held. Think of them as boxes.
# Variables have names to make them easier to work with. In most programming
# languages variable names can be made up of numbers and letters. And they
# normally must start with a letter or underscore character "_". Python in this
# respect is like any other programming language. A variable name is called an
# "identifier". Identifiers are case sensitive. Python keywords cannot be used
# as identifiers.
# Python variables are loosely typed. The interpreter assumes the variable type
# based on the content of the variable. Variables can be integers, long integers
# octal literals (base 8), hexadecimal literals (base 16), floating-point
# numbers and complex numbers.
# Python also supports some complex data structures. These are strings, lists,
# tupels, dictionaries and sets. A list is like an array in Java or C. Except a
# list in Python can consist of mixed types. Tupels, dictionaries and sets are
# similar to lists. But they have different uses, attributes and methods of
# access.
# Examples of numbers.
a = 1 # Integer.
b = 42000000000000000000L # Long integer.
c = 010 # Octal literal.
d = 0xA1F # Hexadecimal literal.
e = 0XA1F # Hexadecimal literal.
f = 45.33 # Floating-point number.
g = 9.5541e-10 # Floating-point number.
h = 4 + 4j # Complex number.
print a
print b
print c
print d
print e
print f
print g
print h
# Examples of strings.
i = "Hello World" # String with double quotes "
j = 'Hello World' # String with single quotes '
k = '''Hello World can extend over multiple lines
and contain 'single quotes' and "double quotes"
which is crazy.''' # String with triple quotes '''
l = '\"Hello World\"' # String with single quotes, printing double quotes.
m = "\'Hello World\'" # String with double quotes, printing single quotes.
print i
print j
print k
print l
print m
# Example of a list.
n = ["H","e","l","l","0"] # A simple list.
o = [["W","o","r","l","d"],2,3.45,0xA1F] # A list composed of multiple types.
print n
print o
print n[0]
print n[1]
print n[2]
print n[3]
print n[4]
print o[0][0]
print o[0][1]
print o[0][2]
print o[0][3]
print o[0][4]
print o[1]
print o[2]
print o[3]
# Example of a tuple.
p = ("Tuples","are","immutable","lists")
print p
print p[0]
print p[1]
print p[2]
print p[3]
# Example of a dictionary.
q = {"Name" : "Bob","Age" : 20}
print q
print q["Name"]
print q["Age"]
# Example of a set.
r = set(("Hello World",i))
s = {"Hello World",i}
print r
print s
# Why doesn't "Hello World" print twice for each set as we would expect?
# Sets contain non-repeating data only. Since "Hello World" and "i" are the same,
# one is discarded.
# Python Variables and Data Types
# The first thing to know about a variable is that it is just a place in the
# computer's memory where data is held. Think of them as boxes.
# Variables have names to make them easier to work with. In most programming
# languages variable names can be made up of numbers and letters. And they
# normally must start with a letter or underscore character "_". Python in this
# respect is like any other programming language. A variable name is called an
# "identifier". Identifiers are case sensitive. Python keywords cannot be used
# as identifiers.
# Python variables are loosely typed. The interpreter assumes the variable type
# based on the content of the variable. Variables can be integers, long integers
# octal literals (base 8), hexadecimal literals (base 16), floating-point
# numbers and complex numbers.
# Python also supports some complex data structures. These are strings, lists,
# tupels, dictionaries and sets. A list is like an array in Java or C. Except a
# list in Python can consist of mixed types. Tupels, dictionaries and sets are
# similar to lists. But they have different uses, attributes and methods of
# access.
# Examples of numbers.
a = 1 # Integer.
b = 42000000000000000000L # Long integer.
c = 010 # Octal literal.
d = 0xA1F # Hexadecimal literal.
e = 0XA1F # Hexadecimal literal.
f = 45.33 # Floating-point number.
g = 9.5541e-10 # Floating-point number.
h = 4 + 4j # Complex number.
print a
print b
print c
print d
print e
print f
print g
print h
# Examples of strings.
i = "Hello World" # String with double quotes "
j = 'Hello World' # String with single quotes '
k = '''Hello World can extend over multiple lines
and contain 'single quotes' and "double quotes"
which is crazy.''' # String with triple quotes '''
l = '\"Hello World\"' # String with single quotes, printing double quotes.
m = "\'Hello World\'" # String with double quotes, printing single quotes.
print i
print j
print k
print l
print m
# Example of a list.
n = ["H","e","l","l","0"] # A simple list.
o = [["W","o","r","l","d"],2,3.45,0xA1F] # A list composed of multiple types.
print n
print o
print n[0]
print n[1]
print n[2]
print n[3]
print n[4]
print o[0][0]
print o[0][1]
print o[0][2]
print o[0][3]
print o[0][4]
print o[1]
print o[2]
print o[3]
# Example of a tuple.
p = ("Tuples","are","immutable","lists")
print p
print p[0]
print p[1]
print p[2]
print p[3]
# Example of a dictionary.
q = {"Name" : "Bob","Age" : 20}
print q
print q["Name"]
print q["Age"]
# Example of a set.
r = set(("Hello World",i))
s = {"Hello World",i}
print r
print s
# Why doesn't "Hello World" print twice for each set as we would expect?
# Sets contain non-repeating data only. Since "Hello World" and "i" are the same,
# one is discarded.
Labels:
dictionary,
floating-point,
hexadecimal,
integer,
list,
lists,
long integer,
octal,
Python,
sets,
string,
tuple
Location:
Scotland, UK
Subscribe to:
Posts (Atom)