# For Loop
for a in range(1,10,2):
print `a` + ": hello"
# For Loop / List
from random import randint
randomList = []
for x in range(10):
randomList.append(randint(1,100))
if randomList[x] % 2 != 0:
print randomList[x]
print randomList
# List / Sum
def totalUp():
listOfNum = [4,3,6,2]
total = sum(listOfNum)
return total
print totalUp()
# Functions
def areaOfRect(w,h):
area = w * h
return area
print areaOfRect(5,10)
# Function / List
def printList(x):
a = x[0]
b = x[-1]
c = "First element is " + `a` + ", while Last element is " + `b`
return c
print printList([154, 198, 123, 136, 239, 257, 207, 262, 178, 228])
# Function / List II
def sumList(x):
a = x[0]
b = x[-1]
c = "The sum of " + `a` + " and " + `b` + " is " + `(a + b)`
return c
print sumList([193, 220, 113, 248, 132, 289, 188, 124, 163, 255])
# List / Maximum
def findMax(x):
maxNum = x[0]
for a in range(len(x)):
if x[a] > maxNum:
maxNum = x[a]
return maxNum
print findMax([171, 135, 160, 295, 105, 253, 169, 224, 298, 139])
# List / Maximum by 4
def findMaxBy4(x):
maxNum = x[0]
for a in range(len(x)):
if x[a] % 4 == 0:
if x[a] > maxNum :
maxNum = x[a]
return maxNum
print findMaxBy4([233, 135, 109, 117, 300, 268, 185, 259, 298, 171])
# List / Maximum / Minimum
def diff(x):
maxNum = x[0]
for a in range(len(x)):
if x[a] > maxNum:
maxNum = x[a]
minNum = x[0]
for a in range(len(x)):
if x[a] < minNum:
minNum = x[a]
difference = maxNum - minNum
return `maxNum` + " - " + `minNum` + " = " + `difference`
print diff([110, 116, 209, 163, 287, 222, 293, 268, 275, 213])
No comments:
Post a Comment