def getAge():
return int(input("how old r u?"))
def under18(currentAge):
if(currentAge < 18):
return True
else:
return False
myAge = getAge()
if under18(myAge):
print("you are under 18")
else:
print("you are 18 or older")
balance = 100
def deposit(amt):
global balance
balance += amt
deposit(15)
print(balance)
recursion
Recursion: solving a problem with a method that calls itself.
a = [999,2,3]
a[0] = 1
print(a[2]) #prints '3'
print(len(a)) #prints '3', because there are 3 elements
for stuff in a:
print("value:", stuff)
#using range to parse list via index:
for index in range(0,len(a)): #index will parse element 0, 1 & 2
print("element: " + str(index) + ", value: " + str(a[index]))
multiDimensionalArray
note - there aren't multidimensional arrays as such in Python, what you have is a list containing other lists:
#jagged:
lists = [[10, 20], [30, 40, 50, 60, 70]]
for eachList in lists:
print(eachList, len(eachList))
'''--------------
nested lists:
use a 'comprehension', which is 'an
expr that specifies a seq of vals'
--------------'''
singleList =["~" for i in range(3)]
#^3 "~" characters in a single list
nestedList =[["~" for x in range(3)] for y in range(3)]
#^3x3 grid of character "~"
nestedList[0][2] = "miss"
nestedList[1][0] = "hit"
for row in nestedList:
print(row)
simpleClass
OOP = programming paradigm. some defns:
class = blueprint in code (e.g. a bicycle class might have a "speed" variable and a speedUp() method)
object = instance of a class (e.g. Ronnie's bike)
abstraction = means hiding what isnt important when working with classes / OOP
encapsulation = techniques to bundle and hide methods and properties within classes (e.g. using public and private keywords). this is one way to enable abstraction
inheritance = when new objects to take on the properties of existing objects (e.g. a circle object inherits the properties of a shape object)
polymorphism = ability for an object to take on many forms (e.g. a method draw() could be used with triangles, stars and boxes)