Python: Basics

""""Hello World""" .isalpha	# Check if alpha characters def  :
 * Single-line comment
 * 1) Hello World
 * Multiple-line comment
 * Functions
 * Create a function

def  (arg):

def  (arg1, arg2):

def  (*args): # Multiple/unknown # of arguments, arbitrary number of arguments

sum(args) # You add arguments up

from import Sample: import math 		#  Import all, use math.sqrt math import sqrt 	# Import only sqrt, no need to qualify from math import *	# Just import metadata??
 * Import defined functions

import this 		# Easter Egg - The Zen of Python Boolean = True if (1 == 2) else False
 * If-then-else on one line

if   : elif  : else:

 = []  # Empty list .append
 * List

my_list.index("dog") # Return the first index that contains the string “dog” my_list.insert(4,"cat") # Add item “cat” at index 4 and bump the rest forward
 * List samples

my_list = [1,9,3,8,5,7] for number in my_list: print number * 2
 * For loop the list

my_list.sort

Remove item from the list n = [1, 3, 5] n.pop(1) print n n.remove(1) print n del(n[1]) print n
 * n.pop(index) will remove the item at index from the list and return it to you:
 * 1) Returns 3 (the item at index 1)
 * 1) prints [1, 5]
 * n.remove(item) will remove the actual item if it finds it:
 * 1) Removes 1 from the list,
 * 2) NOT the item at index 1
 * 1) prints [3, 5]
 * del(n[1] is like .pop in that it will remove the item at the given index, but it won't return it:
 * 1) Doesn't return anything
 * 1) prints [1, 5]

d = {'key1' : 1, 'key2' : 2, 'key3' : 3}
 * Dictionary

menu = {} # Empty dictionary menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
 * Dictionary sample

def plane_ride_cost(city): data = {"Charlotte": 183, "Tampa": 220, "Pittsburgh": 222, "Los Angeles": 475} return data[city] Remove item in dictionary del dict_name[key_name] my_list.remove(value)
 * del deletes a key and its value based on the key you tell it to delete
 * .remove removes a key and its value based on the value you tell it to delete.

inventory = {'gold' : 500, 'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key 'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']}
 * More dictionary samples

inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']
 * 1) Adding a key 'burlap bag' and assigning a list to it

inventory['pouch'].sort
 * 1) Sorting the list found under the key 'pouch'
 * 1) Here the dictionary access expression takes the place of a list name

inventory['pocket'] = ['seashell', 'strange berry', 'lint'] inventory['backpack'].sort

inventory['backpack'].remove('dagger')

inventory['gold'] = inventory['gold']+50

d => {"foo" : "bar"}
 * Dictionaries are unordered

for key in d:    print d[key]  # prints "bar"

for letter in "Codecademy": print letter
 * Print vertically

C o d e c a d e m y def average(alist): return sum(alist)/len(alist)

score = round(score)


 * Random module

from random import randrange

random_number = randrange(1, 10)
 * 1) Generate a


 * Enumerate

choices = ['pizza', 'pasta', 'salad', 'nachos']

print 'Your choices are:' for index, item in enumerate(choices): print index+1, item


 * Zip
 * It's also common to need to iterate over two lists at once. This is where the built-in zip function comes in handy.
 * Zip will create pairs of elements when passed two lists, and will stop at the end of the shorter list.
 * Zip can handle three or more lists as well!

list_a = [3, 9, 17, 15, 19] list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]

for a, b in zip(list_a, list_b): # Add your code here! print a, b