Browse Source

basics

master
Ken Schaefer 6 months ago
parent
commit
e2a5a62034
  1. 61
      basics/basics.py

61
basics/basics.py

@ -1,6 +1,8 @@
import random import random
# Hello World ###############################################################################
# Variables
###############################################################################
print ('Hello World!') print ('Hello World!')
print ('What is your name?') print ('What is your name?')
@ -9,7 +11,9 @@ print ('It is good to meet you ' + myName)
print ('The length of your name is: ') print ('The length of your name is: ')
print (len(myName)); print (len(myName));
###############################################################################
# Conditional # Conditional
###############################################################################
if myName == 'Ken': if myName == 'Ken':
print('Hello') print('Hello')
@ -18,14 +22,16 @@ elif myName == 'Randolph':
else: else:
print('Hi') print('Hi')
###############################################################################
# Looping # Looping
###############################################################################
spam = 0 spam = 0
while spam < 5: while spam < 5:
print('Spam ') print('Spam ')
spam = spam + 1 spam = spam + 1
# Range # Range (aka, the For loop)
total = 0 total = 0
for num in range(101): for num in range(101):
@ -35,7 +41,9 @@ for num in range(101):
for i in range(5): for i in range(5):
print(random.randint(1, 10)) print(random.randint(1, 10))
###############################################################################
# Functions # Functions
###############################################################################
def hello(name): def hello(name):
print('Hello ' + name) print('Hello ' + name)
@ -52,28 +60,73 @@ def spam(divideBy):
except ZeroDivisionError: except ZeroDivisionError:
print('Cant divide by 0') print('Cant divide by 0')
###############################################################################
# Lists # Lists
###############################################################################
animals = ['cat', 'bat', 'rat', 'elephant'] animals = ['cat', 'bat', 'rat', 'elephant']
print(animals[1]) print(animals[1])
print(animals[-1]) print(animals[-1])
# Slices # Slices get sublists
atimals = animals[1:3] atimals = animals[1:3]
print(len(atimals)) print(len(atimals))
# Edit lists
animals.append('moose') animals.append('moose')
animals.remove('rat') animals.remove('rat')
animals.sort() animals.sort()
print(animals) 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 # Dictionaries
###############################################################################
aMonster = {'size':'large', 'color':'red'} aMonster = {'name':'dragon','size':'large', 'color':'red'}
print(aMonster['size']) print(aMonster['size'])
for v in aMonster.values(): for v in aMonster.values():
print(v) 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)
Loading…
Cancel
Save