"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 )
Strings are objects and have methods associated with them. Methods are just a number of ways to do something with objects. For example, for a string, you might want to split it into substrings, find where one substring starts, or replace substrings with other substrings... Have a look at string documentation
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
More on lists here
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...
A Python list is an ordered arrangement of elements. Note that the elements can have different types.
Lists are defined either using the list keyword or using []
my_list = [ 1, 2, "three", "four", "5" ] # A list. In this case, an mix of ints and strs
type ( my_list ) # Check the type
In Python, lists start at 0, and we refer to the elements using [i], where i is the element number
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 )
Use .join() to join the elements of my_list above into a single string again.
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)
Dictionaries are similar to lists, but in this case, the different elements are unordered and are accessed by keys, rather than by position.
my_dict = { "key1": "something", "key2": "other", "key3":150. }
print my_dict
print my_dict[ "key1" ]
The keys can be strings, integers, tuples... We can also nest dictionaries inside dictionaries:
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
File access requires to
Let's start defining some data...
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()
You can now use less (or more) to view the contents of the file temporary_file.txt. It should contain the following:
1.000000 1.000000
2.000000 4.000000
3.000000 9.000000
4.000000 16.000000
5.000000 25.000000
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