# 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:
- In Python we use the 'print' function to print to the console
- In Python function calls need not have () like in Java
- In Python we use the '#' character to comment a single line. The C style comment '//' does not work in Python
- We do not have to end a statement with a ';' or any delimiter in Python
- Python Strings can be delimited by double quotes as well as single quotes
- A double quoted string can contain single quotes
- It is not compulsory to have a main method in Python
- The print function can take multiple arguments separated by ','
- When we give multiple values to the print function, it automatically puts a space in between them
- 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
- 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
- Python supports formatting of Strings with the % character
- 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
- When we use %r for formatting, it surrounds the String between ''
- We can multiply a String with a number, such as "." * 10 - This will concatenate the String 10 times
- Strings can also be concatenated with the + operator
- We can specify triple quoted Strings. These can span multiple lines, and can also contain single and double quotes
- 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 ','
- raw_input("prompt") can also be given a String which will be used as a prompt
- In Python we can unpack a list into multiple variables by assigning a list to multiple variables (see ex13.py)
- If we type CTRL-C on the console when the program is expecting input, it results in a KeyboardInterrupt and the program will exit
- 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