You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
2.9 KiB

8 months ago
import random
6 months ago
###############################################################################
# Variables
###############################################################################
8 months ago
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));
6 months ago
###############################################################################
8 months ago
# Conditional
6 months ago
###############################################################################
8 months ago
if myName == 'Ken':
print('Hello')
elif myName == 'Randolph':
print('Ftaghn')
else:
print('Hi')
6 months ago
###############################################################################
8 months ago
# Looping
6 months ago
###############################################################################
8 months ago
spam = 0
while spam < 5:
print('Spam ')
spam = spam + 1
6 months ago
# Range (aka, the For loop)
8 months ago
total = 0
for num in range(101):
total = total + num
print(total)
for i in range(5):
print(random.randint(1, 10))
6 months ago
###############################################################################
8 months ago
# Functions
6 months ago
###############################################################################
8 months ago
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')
6 months ago
###############################################################################
8 months ago
# Lists
6 months ago
###############################################################################
8 months ago
animals = ['cat', 'bat', 'rat', 'elephant']
print(animals[1])
print(animals[-1])
6 months ago
# Slices get sublists
8 months ago
atimals = animals[1:3]
print(len(atimals))
6 months ago
# Edit lists
8 months ago
animals.append('moose')
animals.remove('rat')
animals.sort()
print(animals)
6 months ago
# 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')
###############################################################################
8 months ago
# Dictionaries
6 months ago
###############################################################################
8 months ago
6 months ago
aMonster = {'name':'dragon','size':'large', 'color':'red'}
8 months ago
print(aMonster['size'])
for v in aMonster.values():
print(v)
6 months ago
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)
8 months ago