|
|
|
import random
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Variables
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
print ('Hello World!')
|
|
|
|
print ('What is your name?')
|
|
|
|
myName = input()
|
|
|
|
print ('It is good to meet you ' + myName)
|
|
|
|
print ('The length of your name is: ')
|
|
|
|
print (len(myName));
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Conditional
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
if myName == 'Ken':
|
|
|
|
print('Hello')
|
|
|
|
elif myName == 'Randolph':
|
|
|
|
print('Ftaghn')
|
|
|
|
else:
|
|
|
|
print('Hi')
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Looping
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
spam = 0
|
|
|
|
while spam < 5:
|
|
|
|
print('Spam ')
|
|
|
|
spam = spam + 1
|
|
|
|
|
|
|
|
# Range (aka, the For loop)
|
|
|
|
|
|
|
|
total = 0
|
|
|
|
for num in range(101):
|
|
|
|
total = total + num
|
|
|
|
print(total)
|
|
|
|
|
|
|
|
for i in range(5):
|
|
|
|
print(random.randint(1, 10))
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Functions
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
def hello(name):
|
|
|
|
print('Hello ' + name)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
r = hello('Fred')
|
|
|
|
print(r)
|
|
|
|
|
|
|
|
# Exceptions
|
|
|
|
|
|
|
|
def spam(divideBy):
|
|
|
|
try:
|
|
|
|
return 42 / divideBy
|
|
|
|
except ZeroDivisionError:
|
|
|
|
print('Cant divide by 0')
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Lists
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
animals = ['cat', 'bat', 'rat', 'elephant']
|
|
|
|
print(animals[1])
|
|
|
|
print(animals[-1])
|
|
|
|
|
|
|
|
# Slices get sublists
|
|
|
|
|
|
|
|
atimals = animals[1:3]
|
|
|
|
print(len(atimals))
|
|
|
|
|
|
|
|
# Edit lists
|
|
|
|
|
|
|
|
animals.append('moose')
|
|
|
|
animals.remove('rat')
|
|
|
|
animals.sort()
|
|
|
|
print(animals)
|
|
|
|
|
|
|
|
# Processsing lists
|
|
|
|
|
|
|
|
weapons = ['sword', 'axe', 'bow', 'hammer']
|
|
|
|
for i in range(len(weapons)):
|
|
|
|
print(str(i) + ': ' + weapons[i])
|
|
|
|
|
|
|
|
# Find things in list
|
|
|
|
print('dagger' in weapons) # false
|
|
|
|
|
|
|
|
print(weapons.index('axe')) # 1
|
|
|
|
|
|
|
|
# Tuples are like lists but they are immutable
|
|
|
|
|
|
|
|
classes = ('fighter', 'cleric', 'mage')
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Dictionaries
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
aMonster = {'name':'dragon','size':'large', 'color':'red'}
|
|
|
|
print(aMonster['size'])
|
|
|
|
|
|
|
|
for v in aMonster.values():
|
|
|
|
print(v)
|
|
|
|
|
|
|
|
for k, v in aMonster.items():
|
|
|
|
print('Key: ' + k + ' Value: ' + str(v))
|
|
|
|
|
|
|
|
|
|
|
|
if ('weapon' not in aMonster.keys):
|
|
|
|
aMonster['weapon'] = 'talons'
|
|
|
|
|
|
|
|
# alternative to the above if statement is to use default
|
|
|
|
|
|
|
|
aMonster.setdefault('weapon', 'talon')
|
|
|
|
|
|
|
|
# import pprint to get better formatting of dictionary data
|
|
|
|
import pprint
|
|
|
|
message = 'In a hole in the ground there lived a hobbit.'
|
|
|
|
count = {}
|
|
|
|
|
|
|
|
for character in message:
|
|
|
|
count.setdefault(character, 0)
|
|
|
|
count[character] = count[character] + 1
|
|
|
|
|
|
|
|
pprint.pprint(count)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|