"hello world" 'hello again' """A string that spans multiple lines""" # Note how python identifies `\n` as the linebreak character hw = "hello world" print hw help(str) new_string = "hello" + "world" # Note no spaces within the "s! print new_string new_string = "hello " + "world" # Note the space print new_string print "This is the first %s %s wrote" % ( "code", "Jose" ) what_wrote = "code" who_wrote = "Jose" what_year = 2012 print "This is the first %s %s wrote in %d" % ( what_wrote, who_wrote, what_year ) hw = "hello world" hw.count ( "l" ) # Count the number of times "l" apears in hw hw.find ( "l" ) # Find the first location of "l" in hw. Note that python starts counting at 0, not 1! hw.find ( "lo" ) # Ditto hw.isdigit () # Is this string a digit? hw.replace ( "world", "everyone" ) # Replace one substring with another hw.split () # Split the string into words line = raw_input ( "Enter something: " ) line = line.strip() while True: if line == "" or line == "q": break try: print line except: pass line = raw_input ( "Enter something: " ) line = line.strip() var_1 = True var_2 = False type( var_1 ) var_1 and var_2 var_1 or var_2 not var_1 or var_2 a_string = "1 2 3" # This is just a string type ( a_string ) # Test that it is indeed a list type ( a_string.split() ) # Split a string to create a list of elements... my_list = [ 1, 2, "three", "four", "5" ] # A list. In this case, an mix of ints and strs type ( my_list ) # Check the type print my_list [0] # Print the first element print my_list [3] # The fourth element print my_list [ 0:2] # Select from first to third element (non-inclusive) print my_list [0:4:2]# Select from 0 to 4, skipping every other element print my_list [ -1 ] # -1 is the last element print my_list [ :-3] # Select from element 0 (ommitted) to third from the end print my_list [-3:] # Select from 3rd element from the end to end (ommitted) help(list) # or see [this](http://docs.python.org/tutorial/datastructures.html#more-on-lists) my_list = [ "twinkle", "little" ] my_list.insert ( 0, my_list[0].capitalize() ) my_list.append ( "star" ) print my_list my_list = [ "twinkle" ] *2 + "little star".split() print my_list print [ ( s== "star" and "bat" ) or s for s in my_list] extra=" How I wonder what you are" my_list.extend ( extra.split() ) print my_list my_list.remove ( "you") print my_list my_list.insert ( -1, "ur" ) my_list.pop ( -1 ) print my_list len ( my_list ) print "1" + "1" print 1 + 1 i = 1 print i i += 1 print i i = i + 1 print i i *= 2 print i i = i*2 print i i = i/2 print i i = i + 1 print i/2 ###!!! print i/2, i % 2 print "This is an integer: %d" % ( i ) print "This is the same integer: %03d" % ( i ) print 3.0/2 print "%21.10f" % ( 1.0e10 + 1e-6) my_dict = { "key1": "something", "key2": "other", "key3":150. } print my_dict print my_dict[ "key1" ] some_other_dict = { "older_dict": my_dict, "some_key": 42 } print some_other_dict my_dict[ "key3" ] = my_dict[ "key3" ]*3 print some_other_dict my_dict[ "key2" ] = None print some_other_dict modis_bands = ['630-690','780-900','450-520','530-610','1230-1250','1550-1750','2090-2350'] band_numbers = range ( 1, len( modis_bands ) + 1 ) modis = dict ( zip ( band_numbers, modis_bands ) ) for i, m in modis.iteritems(): # Also itervalues, iterkeys, etc... print i, m the_data = [[1.,2.,3.,4.,5.],[1.,4.,9.,16.,25.]] print the_data print len ( the_data ) print len ( the_data[0] ), len ( the_data[1] ) output_file = open ( "temporary_file.txt", 'w' ) for i in range ( len( the_data[0] ) ): output_file.write ( "%f %f\n" % ( the_data[0][i], the_data[1][i] ) ) output_file.close() input_file = open ( "temporary_file.txt", 'r' ) input_data = input_file.readlines() print input_data data = [] values = [] for i in xrange ( len ( input_data ) ): the_line = input_data[i].strip().split() data.append ( float ( the_line[0] ) ) values.append ( float ( the_line[1] ) ) new_data = [ data, values ] print "new_data: ", new_data print "the_data: ", the_data print "Are they equal?", new_data == the_data