21st February 2013 - LPTHW - Learnings

1. EX16 :  'w' is used with open command, which expilictly tells that file is being opened for writing purpose.

2. EX 17:   exists is a command which returns TRUE if a file exists basis on its name in a string as an arguement. It returns FALSE if not.

3. EX17 : To write a new text file on windows we use the type  command.

4. EX17 : File can be open and read on the same line of code.

5. EX18: *args asterisk args is just like argv, only that it is used for functions. It needs to be put inside brackets to work.

6. EX18: After the function has been defined and named, arguments passed in the bracket, the line ended with : colon, the indeting starts after that. In Python the indentation is 4 spaces.

7. E18: Function Checklist as given by Zed Shaw in Learning Python The Hard Way

Write out a function checklist for later exercises. Write these on an index card and keep it by you while you complete the rest of these exercises or until you feel you do not need it:
  1. Did you start your function definition with def?
  2. Does your function name have only characters and _ (underscore) characters?
  3. Did you put an open parenthesis ( right after the function name?
  4. Did you put your arguments after the parenthesis ( separated by commas?
  5. Did you make each argument unique (meaning no duplicated names).
  6. Did you put a close parenthesis and a colon ): after the arguments?
  7. Did you indent all lines of code you want in the function 4 spaces? No more, no less.
  8. Did you "end" your function by going back to writing with no indent (dedenting we call it)?
And when you run ("use" or "call") a function, check these things:
  1. Did you call/use/run this function by typing its name?
  2. Did you put ( character after the name to run it?
  3. Did you put the values you want into the parenthesis separated by commas?
  4. Did you end the function call with a ) character?
8. EX19: It is prudent to use not more than 5 arguments inside a function.

9. EX20: f  is just a variable, but this time it denotes file.

10. EX20: Whenever f.seek(0) is done , we move to the start of the file.

11. EX20:  f.readline() means we are reading a line from the file.

12. EX20: The readline() function returns the \n that's in the file at the end of that line. To not let the print  command also type its own \n in case f.readline() is there in a line having print command, we type "," at the end of the print statement.

13. The seek() function deals in bytes, thats why seek(0) means starting at the script again.

14. Inside readline() is code that scans each byte of the file until it finds a \n character, then stops reading the file to return what it found so far. The file f is responsible for maintaining the current position in the file after each readline() call, so that it will keep reading each line.

 

Comments

Popular posts from this blog

Coursera - CS 101 - Completed