#Python3 Primer
#Most of these commands should also work in Python2.
#Preferred text editor: Sublime Text 2
#Can also use Wing101 IDE if you want a more gui based approach
#See CS116 instructions for installing Python if you like
#or use SageMathCloud for all your Python and LaTeX needs.

a = 5
print(a)

if (a==5):
    print("If statement true")
else:
    print("If statement false")

s = "this is a string"
print(s)
print("this is a string slice", s[1:])
print("this is another string slice", s[:5])
print("this is yet another string slice", s[::2])

#This is how we define functions
def count_and_one(s):
    return s.count('a')+1

print(count_and_one("banana"))

print("Random string commands")
s = "banana"
print(s.count('a'))
print(s.replace('a','*'))
print(s, "Notice how replace didn't change s. To do that do:")
s = s.replace('a','*')
print(s)
print(s.find('n'), "Notice here that objects in Python are 0-indexed (they start at 0)")
print("this is a string".find('str'))
print(s.find('not found'))

print("Two final useful string commands: chr and ord. These convert to and from ASCII encodings")
print(chr(65))
print(ord('A'))
print(ord('a'))
print(chr(3 + ord('A')))

print("Lists and dictionaries are next. Python comes with lots of useful built-in commands.")

L = [1,2,"a"]
N = [16,2,4,8,1]
print(len(L))
print(sum(N))
N.sort()
print(N, "Notice that this sort command changed (mutated) N unlike with strings.")
N[2] = 50
print(N)
N.append(10)
print(N)
N.remove(50)
print(N)
N.pop(1)
print(N, "Don't forget that lists start at 0 as well!")
print("List slicing works like string slicing")
print("Reverse a list (or a string)")
print(L[::-1])


print("Dictionaries are like lists except the keys don't have to be integers")
d = {"a":1, "b":2, "c":3}
print(d["a"])
print(d.values())
print(d.keys())
d.pop('a')
print("post popping 'a'",d)

print("To see what methods you can call on a variable, do dir(var_name)")
print("Directory of dictionary",dir(d))
print("Directory of list",dir(L))
print("Directory of string",dir(s))



print("Loops")
for i in range(5):
    print(i)
for i in range(1,5):
    print(i)
for i in range(1,5,2):
    print(i)
for i in range(5,2,-1):
    print(i)

j=0
while j < 5:
    print(j)
    j = j +1

print("Some cool list comprehension:")
print(sum([i*i for i in range(10)]))





