-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython-notes.txt
93 lines (58 loc) · 4.8 KB
/
python-notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Python Characters:
< -> less-than
> -> greater-than
<= -> less-than-equal
>= -> greater-than-equal
Math operations follow the PEMDAS order, so do parenthesis, exponents, multiplication, division, addition and then subtraction.
% is referred to modulo. So if you have 30 % 9, 9 will go into 30 3 times and have 3 left over, so the answer is 3. Modulo always refers to the remainder. If you have 14 % 30, the answer is 14, because 30 can't go into 14, not even once.
Variables: Variables are simply names given to chunks of code, so that programmers can more easily remember those chunks of code and what they do.
Format string: A string is any piece of text that appears between double quotes. Essentially think of it as a bit of code your program might give to a human.
Tuple: Used when you need to pass multiple strings in one statement, e.g. -> "Who are you? %s Who are you? %s" % (Ryan, Dave)
%s - Call in string text.
%d - Call in integers.
%r - Call in anything. Use for debugging, since it calls in "raw data." This will give you the "raw programmer's" version of the variable, also known as the "representation."
Having a line of more than 80 characters is considered bad style in Python.
When you're using try / except blocks, the try ... except Exception, E is great for debugging, because it will catch any errors. Not recommended for production code, though.
Try and Except blocks are handy when you need your code to gracefully fail if a network condition isn't being met. This way your page doesn't return a 500 error if one part of the page is broken.
You don't need to put quotes around True or False because then Python will think they are simply a string, and things won't work right.
You can use %s to print out non-ASCII characters.
If you want to start a string on a new line in the output, then put a \n in front of the word where the new line should begin. This is similar to what you did in PERL, when you wanted the format of the e-mail to break between lines.
The backslash is a handy means of escaping, and putting difficult-to-type characters into a string. You can put a backslash into a string by using two backslashes!
Escape sequences -
\\ - escapes backslash.
\' - escapes single quote, use if the string is wrapped in single quotes.
\" - escapes double quote, use if the string is wrapped in double quotes.
\a - ASCII Bell (BEL).
\b - ASCII Backspace (BS).
\f - ASCII Formfeed (FF).
\n - ASCII Linefeed (LF).
\N{name} - Character named name in the Unicode database (Unicode only).
\r ASCII - Carriage return (CR).
\t ASCII - Horizontal Tab (TAB).
\uxxxx - Character with 16-bit hex value xxxx (Unicode only).
\uxxxxxxxx - Character with 32-bit hex value xxxxxxxx.
\v - ASCII Vertical Tab (VT).
\ooo - Character with octal value ooo.
\xhh - Character with hex value hh.
%r will not escape, because it's printing out the raw representation of what you've typed. You will need to use %s to see your escape sequences.
open(...) - The open method/function will open a file using the file() type, returns a file object. Use pydoc for more.
Imports help you, and other programmers reading your code, determine what features you're going to be using in your script (or python file). This helps to keep your programs small.
If you see argv in a programming language, it is commonly referred to as the "argument variable."
When you're importing something, it is referred to as a module, or library.
Here are some important commands to remember:
close -- Closes the file. Like File -> Save.. in your editor.
read -- Reads the contents of the file, you can assign the result to a variable.
readline -- Reads just one line of a text file.
truncate -- Empties the file, watch out if you care about the file.
write(stuff) -- Writes stuff to the file.
The import statement is a way to get other Python code into your set up.
From a file system perspective, packages are directories and modules are files.
Cat, or concatenate, will print out the contents of the file.
len() will get the length of a string.
Closing a file at the end of your Python script is just general good practice, and can save you from many headaches if you don't close a file and Python doesn't close the file as soon as it's done executing (which opens some of the time). If the file remains open it could tie up resources and might not be saved.
Functions can do three things:
1 - They name pieces of code the way variables name strings and numbers.
2 - They take arguments the way your scripts take argv.
3 - Using #1 and #2 they let you make your own "mini scripts" or "tiny commands."
A function name can be anything that doesn't start with a number, and can contain letters, numbers, and underscores.
The asterisk in *args tells python to take all the arguments to the function and put them in args as a list. It's like argv, but for functions.