print "Hello World" #!/usr/bin/env python print "Hello World!" # Writing to standard out: print "Python is awesome!" # Reading from standard input and output to standard output name = raw_input("What is your name?") print name #String a = "apple" type(a) #print type(a) #Integer b = 3 type(b) #print type(b) #Float c = 3.2 type(c) #print type(c) #Boolean d = True type(d) #print type(d) 14/b 14/c 14./b #Concatenating strings a = "Hello" # String b = " World" # Another string print a + b # Concatenation # Slicing strings a = "World" print a[0] print a[-1] print "World"[0:4] print a[::-1] # Popular string functions a = "Hello World" print "-".join(a) print a.startswith("Wo") print a.endswith("rld") print a.replace("o","0").replace("d","[)").replace("l","1") print a.split() print a.split('o') string = "string" string[-1] = "y" #Here we attempt to assign the last character in the string to "y" for i in range(5): print "Hi \n" for i in range(3): for j in range(3): print i, j print "This statement is within the i-loop, but not the j-loop" # Writing to a file with open("example.txt", "w") as f: f.write("Hello World! \n") f.write("How are you? \n") f.write("I'm fine.") # Reading from a file with open("example.txt", "r") as f: data = f.readlines() for line in data: words = line.split() print words # Count lines and words in a file lines = 0 words = 0 the_file = "example.txt" with open(the_file, 'r') as f: for line in f: lines += 1 words += len(line.split()) print "There are %i lines and %i words in the %s file." % (lines, words, the_file) groceries = [] # Add to list groceries.append("oranges") groceries.append("meat") groceries.append("asparangus") # Access by index print groceries[2] print groceries[0] # Find number of things in list print len(groceries) # Sort the items in the list groceries.sort() print groceries # List Comprehension veggie = [x for x in groceries if x is not "meat"] print veggie # Remove from list groceries.remove("asparangus") print groceries #The list is mutable groceries[0] = 2 print groceries L1 = [x**2 for x in range(10)] L2 = [2**i for i in range(13)] L3 = [x for x in L1 if x % 2 == 0] print L1 print L2 print L3 noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)] print noprimes primes = [x for x in range(2, 50) if x not in noprimes] print primes primes = [x for x in range(2, 50) if x not in [j for i in range(2, 8) for j in range(i*2, 50, i) ]] print primes # Tuple grocery list groceries = ('orange', 'meat', 'asparangus', 2.5, True) print groceries #print groceries[2] #groceries[2] = 'milk' numbers = range(10) evens = [2, 4, 6, 8] evens = set(evens) numbers = set(numbers) # Use difference to find the odds odds = numbers - evens print odds # Note: Set also allows for use of union (|), and intersection (&) # A simple dictionary simple_dic = {'cs591': 'data-mining tools'} # Access by key print simple_dic['cs591'] # A longer dictionary classes = { 'cs591': 'data-mining tools', 'cs565': 'data-mining algorithms' } # Check if item is in dictionary print 'cs530' in classes # Add new item classes['cs530'] = 'algorithms' print classes['cs530'] # Print just the keys print classes.keys() # Print just the values print classes.values() # Print the items in the dictionary print classes.items() # Print dictionary pairs another way for key, value in classes.items(): print key, value # Complex Data structures # Dictionaries inside a dictionary! professors = { "prof1": { "name": "Evimaria Terzi", "department": "Computer Science", "research interests": ["algorithms", "data mining", "machine learning",] }, "prof2": { "name": "Chris Dellarocas", "department": "Management", "interests": ["market analysis", "data mining", "computational education",], } } for prof in professors: print professors[prof]["name"] for i in [1,2,3,4]: print i for k in {'evimaria': 'terzi', 'aris': 'anagnostopoulos'}: print k for l in 'python is magic': print l list({'evimaria': 'terzi', 'aris': 'anagnostopoulos'}) list('python is magic') print '-'.join('evimaria') print '-'.join(['a','b','c']) i = iter('magic') print i print i.next() print i.next() print i.next() print i.next() print i.next() print i.next() a = [x for x in range(10)] print sum(iter(a)) def func(n): for i in range(n): yield i g = func(10) print g print g.next() print g.next() def demonstrate(n): print 'begin execution of the function' for i in range(n): print 'before yield' yield i*i print 'after yield' g = demonstrate(5) print g.next() print g.next() print g.next() print g.next() g = (x for x in range(10)) print g print sum(g) y = [x for x in range(10)] print y def displayperson(name,age): print "My name is "+ name +" and I am "+age+" years old." return displayperson("Bob","40") def f (x): return x**2 print f(8) g = lambda x: x**2 print g(8) f = lambda x, y : x + y print f(2,3) def multiply (n): return lambda x: x*n f = multiply(2) g = multiply(6) print f(10), g(10) multiply(3)(30) def dollar2euro(x): return 0.89*x def euro2dollar(x): return 1.12*x amounts= (100, 200, 300, 400) dollars = map(dollar2euro, amounts) print dollars amounts= (100, 200, 300, 400) euros = map(euro2dollar, amounts) print euros map(lambda x: 0.89*x, amounts) a = [1,2,3,4,5] b = [-1,-2,-3, -4, -5] c = [10, 20 , 30, 40, 50] l1 = map(lambda x,y: x+y, a,b) print l1 l2 = map (lambda x,y,z: x-y+z, a,b,c) print l2 nums = [i for i in range(100)] print nums even = filter(lambda x: x%2==0 and x!=0, nums) print even print reduce(lambda x,y: x+y, [x for x in range(10)]) print reduce (lambda x,y: x if x>y else y, [1, 15, 26, -27]) import random myList = [2, 109, False, 10, "data", 482, "mining"] random.choice(myList) from random import shuffle x = [[i] for i in range(10)] shuffle(x) print x # Getting data from an API import requests width = '200' height = '300' #response = requests.get('http://placekitten.com/' + width + '/' + height) response = requests.get('http://lorempixel.com/400/200/sports/1/') print response with open('image.jpg', 'wb') as f: f.write(response.content) from IPython.display import Image Image(filename="image.jpg") # Code for setting the style of the notebook from IPython.core.display import HTML def css_styling(): styles = open("theme/custom.css", "r").read() return HTML(styles) css_styling()