As from the previous post, I am doing the google’s python class code and if any one comes up different solutions to the googles version or mine, please post. So here is the string2.py solutions from me.

Verbing, needs to check for string over 3 and then only compare the last 3 characters for ‘ing’

```# D. verbing # Given a string, if its length is at least 3, # add 'ing' to its end. # Unless it already ends in 'ing', in which case # add 'ly' instead. # If the string length is less than 3, leave it unchanged. # Return the resulting string. def verbing(s): # +++your code here+++ if len(s) < 3: return s elif (s[-3:] =='ing'): return s + 'ly' else: return s + 'ing'```

```# E. not_bad # Given a string, find the first appearance of the # substring 'not' and 'bad'. If the 'bad' follows # the 'not', replace the whole 'not'...'bad' substring # with 'good'. # Return the resulting string. # So 'This dinner is not that bad!' yields: # This dinner is good! def not_bad(s): # +++your code here+++ notV = s.find('not') badV = s.find('bad') if (badV > notV): return s[:notV] + 'good' + s[(badV+3):] return s```

Font back, here we needed the modulus function to find out the reminder of a division.

```# F. front_back # Consider dividing a string into two halves. # If the length is even, the front and back halves are the same length. # If the length is odd, we'll say that the extra char goes in the front half. # e.g. 'abcde', the front half is 'abc', the back half 'de'. # Given 2 strings, a and b, return a string of the form # a-front + b-front + a-back + b-back def front_back(a, b): # +++your code here+++ aV = len(a)/2+(len(a)%2) bV = len(b)/2+(len(b)%2) return a[:aV]+b[:bV]+a[aV:]+b[bV:]```

After been talking with some friends, they said about putting some of the google code from there classes online encase other people come up with any other versions. So from the first of the classes that they was talking about was Python , so here is my code for these, may be other people may be doing and come up with different code ?

For first string1.py test there is 4 functions that you have code up and in order from the source code.

Donut, as from the below you just need do a compare on the parameter in.

```# A. donuts # Given an int count of a number of donuts, return a string # of the form 'Number of donuts: <count>', where <count> is the number # passed in. However, if the count is 10 or more, then use the word 'many' # instead of the actual count. # So donuts(5) returns 'Number of donuts: 5' # and donuts(23) returns 'Number of donuts: many' def donuts(count): # +++your code here+++ if count >= 10: return 'Number of donuts: many' return 'Number of donuts: ' + str(count)```

Both end, it is string manipulation and returning part of the string if longer enough.

```# B. both_ends # Given a string s, return a string made of the first 2 # and the last 2 chars of the original string, # so 'spring' yields 'spng'. However, if the string length # is less than 2, return instead the empty string. def both_ends(s): # +++your code here+++ if len(s) < 2: return '' return s[:2] + s[-2:]```

Fix start, replace the characters within the string with a * that the same as the first character, so just need to concatenate the first character with the rest of the characters with any replaced 🙂

```# C. fix_start # Given a string s, return a string # where all occurences of its first char have # been changed to '*', except do not change # the first char itself. # e.g. 'babble' yields 'ba**le' # Assume that the string is length 1 or more. # Hint: s.replace(stra, strb) returns a version of string s # where all instances of stra have been replaced by strb. def fix_start(s): # +++your code here+++ return s + s[1:].replace(s,'*')```

Mix up, change the first 2 characters from the two parameters around.

```# D. MixUp # Given strings a and b, return a single string with a and b separated # by a space '<a> <b>', except swap the first 2 chars of each string. # e.g. # 'mix', pod' -> 'pox mid' # 'dog', 'dinner' -> 'dig donner' # Assume a and b are length 2 or more. def mix_up(a, b): # +++your code here+++ return b[:2]+a[2:]+' '+a[:2]+b[2:]```

## Classes

Since Python is a object oriented programming language then you can use things like classes to define a object, a class is a bunch of methods that act on the data that is stored within the class itself. You can store data within the class, but unlike c++/java/c# etc, there is no private/public/protected, everything is public accessible.

In the example below, the class constructor is called (__init__ method) and the first parameter is the class object itself, which is why it is there and for many reasons why it is called self as a variable name.

```  # self means the object itself.   class mysimpleclass : __myvar = 0 def __init__(self,value = 0) : self.__myvar = value   def printValue(self) : print self.__myvar   simple = mysimpleclass(5) simple.printValue()```

the printValue method just does that, it will print the value that is sorted within the class object and here is the output

`5`

if you did not put in the self in the example above for printing out the class object variable __myvar as below

```class mysimpleclass .... .... def printValue() : print __myvar```

the error would be

`NameError: global name '_mysimpleclass__myvar' is not defined`

because the interrupter is looking for a global variable and not a variable attached to that object.

To carry on my normal process of notes on this website for different languages here is the how to add two numbers with a return value from a function.

To define a function you use the “def” syntax within the programming language and then the next bit of text before the parameters for the function itself ( in this case the parameters are the (number1, number2) ), is the function name and how to call the function.

The function below takes two parameters and returns back there result of the mathematical addition, the last part is how to call the function.

```def addTwo(number1, number2) : return number1 + number2   print addTwo(1,2)```

and the output would be

`3`

Here is how to open and read/write to a file within Python. Most languages supply a basic file opening and read/writing methods with the closing method, and Python is no different which makes going from one language to another very easy (you just need to learn the classes that are capable to doing the interesting stuff 🙂 ).

To write to a file, you have to open it first within a write mode (‘w’) and then write to that file *handler*, in the code below the variable ‘f’ is the file *handler*

```f = open('write.txt','w'); f.write('hi there\n'); f.write('this is genux\n'); f.write('and my site is codingfriends.com\n'); f.write('pyhon does OO\n'); f.close(); print 'File has been written\n';```

which will write to the “write.txt” the following text

```hi there this is genux and my site is codingfriends.com pyhon does OO```

and then to read in the file you use the read mode ‘r’, you are able to use the Iterator style of coding with using a ‘for in :’ or if you wish to read one line at a time you can use the ‘readline()’ method, and to seek within a file you use the method ‘seek()’.

```f = open('write.txt','r'); for readInLine in f: print readInLine;   print 'seek to the start of the file'; f.seek(0); anotherLine = f.readline(); print anotherLine;   f.close();```

and here is the output

```hi there   this is genux   and my site is codingfriends.com   pyhon does OO   seek to the start of the file hi there```

## Hello world

Python is one of those languages that are used by allot of different companies but probably does not get the credit that how much it is used. Google uses it, allot of the Ubuntu backend is python. Python is able to do object orientated programming, but does not include the full essence of objects like other languages like C++/Java/C# where you have a private/public/protected but use a “__” before the variable as a “hidden” variable within the class.

So going to do some posts of how cool Python can be, but to start with the classic “hello world” program, like most languages (C++/PHP/C#/Java) most of the functions/methods are actually english words that should mean something to the writer/reader of the program, so to display a message to the screen you can use the “print” command and here is the example program

`print "hello world"`

and this will output, if you save out that program as helloworld.py

`hello world`

to call the saved program via the command line you need to call the interpreter (the python program).

`python helloworld.py`