Python code exercises from “Introduction to Computing Using Python..” chapter 4

Exercise: Format some string information to look like so: John Doe 123 Main Street AnyCity, AS 09876 first=’John’ last= ‘Doe’ street=’Main Street’ number= 123 city = ‘AnyCity’ state=’AS’ zipcode=’09876′ print(‘{0} {1}\n{2} {3}\n{4}, {5} {6}’.format(first,last,number, street, city, state, zip code)) Exercise: Write a function that will take a list of data Continue Reading

Python With As

In Python you can do the typical way of opening/writing to a file by calling it with: my_var = open(“my file.txt”,”r”) or “w” for write… In such cases you need to close the file when you’re done with a my_var.close() Instead you could also use: with open(“my_file.txt”, “w”) as my_var: Continue Reading

Python Classes

In Python you create a class with: class Like: class MyCalculator(object): Member variables are variables defined within a class.  For example: class MyCalculator(object): statement = “Welcome to the calculator” You can access member variables with dot notation like so: Frist lets create an instance of the class: my_calc = MyCalculator() Continue Reading