Friday, September 23, 2011

Exercise 22 - What do you know so far ?

In this exercise I am going to read through all the stuff I have done so far in the course, and then I will make a table of all the symbols, keywords, and concepts encountered in the course thus far.

# Used to comment a single like
+ Addition symbol
- Subtraction symbol
/ Division symbol
* Multiplication symbol
% Modulus symbol
< Less than symbol
> Greater than symbol
<= Less than equal to symbol
>= Greater than equal to symbol
= Assignment symbol
\ Escape character

* Can also precede an argument name given to a function. In this case it means that this variable will accept all the input given to the function as a list


def A keyword used to define functions
import Used to import a module or a symbol from a module

argv Is a symbol from the 'sys' module which contains all the command line arguments passed to a Python script, starting with the name of the script itself

print Prints the specified values to the standard console
raw_input Accepts raw input from a user from the standard console
open(filename) Opens a file and returns a handle to it. By default this function will open the file in read mode, but we can specify a mode and open it for writing, etc
read() A function which can be called on a file handle to read the contents of a file
close() A function which can be called on a file handle to close the file and free up resources on the OS
truncate() A function which can be called on a file handle to remove existing contents of a file to either the current position of the file pointer, or if we provide a size argument then it will be truncated to that size
write() A function which can be called on a file pointer to write to the file
exists() A function which takes a file pointer and determines if that file exists
len() A function which takes data and tells us the size of that data. It accepts a sequence or a mapping


Stuff I learned:
  1. In Python we use the 'print' function to print to the console
  2. In Python function calls need not have () like in Java
  3. In Python we use the '#' character to comment a single line. The C style comment '//' does not work in Python
  4. We do not have to end a statement with a ';' or any delimiter in Python
  5. Python Strings can be delimited by double quotes as well as single quotes
  6. A double quoted string can contain single quotes
  7. It is not compulsory to have a main method in Python
  8. The print function can take multiple arguments separated by ','
  9. When we give multiple values to the print function, it automatically puts a space in between them
  10. Since Python is dynamically typed, we do not need to specify the type of variables, nor do we need any special syntax like 'var' to define variable names
  11. When doing math operations in Python, if all the operands are integers, then the result will also be an Integer. If the operation is a division operation, then the result will be truncated to yield an Integer. However, if even one of the operands is a float, then all operands will be promoted to floating point numbers and the result will also be a float
  12. Python supports formatting of Strings with the % character
  13. There are many formatting characters in Python. %r is one of them. It will print the String representation of any object, by calling the repr() method of that object
  14. When we use %r for formatting, it surrounds the String between ''
  15. We can multiply a String with a number, such as "." * 10 - This will concatenate the String 10 times
  16. Strings can also be concatenated with the + operator
  17. We can specify triple quoted Strings. These can span multiple lines, and can also contain single and double quotes
  18. When we print something with the print statement, it automatically appends a newline character to the String when it is printed. We can override this functionality by ending the print with a ','
  19. raw_input("prompt") can also be given a String which will be used as a prompt
  20. In Python we can unpack a list into multiple variables by assigning a list to multiple variables (see ex13.py)
  21. If we type CTRL-C on the console when the program is expecting input, it results in a KeyboardInterrupt and the program will exit
  22. In Python functions do not have to be delimited with {}. Rather there contents are indented, and the function ends when the indentation comes back to the level before the function


No comments:

Post a Comment