[note] Basic Information

1.Keywords


Keywords

Keywords are special words that are reserved by Python and cannot be used by you to name things. They indicate commands to the Python interpreter. The following is a complete list of Python keywords:

and del from not while
as elif global or with
assert else if pass yield
break except import nonlocal class
lambda in raise continue finally
is return def for  
try True False None  

2. Operators



Operators

Operators are special tokens (sequence of characters) that have meaning to the Python Interpreter.  Using them implies some form of mathematical operation. The following is the complete list of Operators. 

( ) [ ] { }
, : . ` = ;
' '' # \ @  



3. Punctuators and Delimiters


Punctuators and Delimiters

Punctuators which are also known as delimiters, separate different elements in Python statements and expressions. Here is the complete list:

( ) [ ] { }
, : . ` = ;
' '' # \ @  

4.Python Arithmetic Operators

Operator Meaning Usage
+ Addition or unary plus x + y
- Subtraction or unary minus x - y
* Multiplication x * y
/ Division (result is always a float) x / y
% Modulus x % y (remainder of x/y)
// Floor division - results into whole number (may be float) x // y
** Exponent x**y (x to the power y)

5. Python Operators Precedence Table

Operator Description
** Exponentiation
~, +, - Complement, unary plus and unary minus
*, /, %, // Multiply, divide, modulo and whole (floor) division
+, - Addition and subtraction
>>, << Right and left bitwise shift
& Bitwise and
^ ,| Bitwise exclusive or and bitwise or
<=, < ,> ,>=, Relational operators
<> ,== ,!= Equality operators
= ,%= ,/= ,//= ,-= ,+= ,*=, **= Assignment operators
is , is not Identity operators
in not in Membership operators
not , or , and Logical operators

6.Python Relational Operators

Operator Meaning Usage
> Greater than x > y
< Less than x < y
== Equal to x == y
!= Not equal to x != y
>= Greater than or equal to x >= y
<= Less than or equal to  x <= y

7.Python Logical Operators

Operator Meaning Usage
and True if both the operands are true x and y
or True if either of the operands is true x or y
not complements the operand not x

8.Boolean Operators

The basic Boolean operators are: and, or, not . The following is the truth table for each of these operators. Note that A and B in the tables below are the names of the operands that represent a Boolean Expression. 

not A

The not operator flips the value of the Boolean operand. It converts True to False and False to True.

A not A
True False
False True

and B

The and operator requires both A and B to be True for the whole expression to be True. If only one of them isFalse then the entire expression is False.

A B A and B
True True True
True False False
False True False
False False False

or B

The or operator only requires one of A or B to be True for the entire expression to be True. The whole expression is False only when neither A nor B is True

A B A or B
True True True
True False True
False True True
False False False

9.Python Membership Operators

Operator Meaning Usage
in Check to see if value is in the sequence 5 in [2,5,3,7]
not in Check to see if value is not in the sequence 5 not in [2,5,3,7]

10.Complete Precedence Table

The following table shows the complete precedence of Python Operators: Highest to Lowest

OPERATOR DESCRIPTION
() Parenthesis (grouping)
f(args...), x[i:i], x[i], x.attr Function call, slicing, subscript, dot
** Exponentiation
+, -, ~ Unary Positive, Unary Negative, bitwise NOT
*, /, % Multiplication, Division, Remainder
+, - Addition, Subtraction
<<, >> Shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
<, <=, >, >=, !=, ==, is, is not, in, not in Comparisons, identity, membership
not Boolean NOT
and Boolean AND
or Boolean OR
lambda Lambda Expression

11.Built-in Functions

Table of common methods for lists

list.append(x)Add an item to the end of the list. Equivalent to a[len(a):] = [x].
list.extend(L)Extend the list by appending all the items in the given list. Equivalent to a[len(a):] = L.
list.insert(i, x)Insert an item at a given position. The first argument is the index of the element to insert.
list.remove(x)Remove the first item from the list whose value is x. It is an error if there is no such item.
list.copy()Return a shallow copy of the list. Equivalent to a[:].
list.pop([i])Remove the item at the given position and return it. If no index is specified, removes the last item.
list.clear()Remove all items from the list. Equivalent to del a[:].
list.index(x)Return the index in the list of the first item whose value is x. It is an error if there is no such item.
list.count(x)Return the number of times x appears in the list.
list.reverse()Reverse the elements of the list in place.
list.sort(key=None, reverse=False) Sort the items of the list in place (the arguments can be used for sort customization).

13.Most commonly used UTF-8 Character Codes

Source: wiki

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

 
2
 

SP
0020
32

!
0021
33

"
0022
34

#
0023
35

$
0024
36

%
0025
37

&
0026
38

'
0027
39

(
0028
40

)
0029
41

*
002A
42

+
002B
43

,
002C
44

-
002D
45

.
002E
46

/
002F
47

 
3
 

0
0030
48

1
0031
49

2
0032
50

3
0033
51

4
0034
52

5
0035
53

6
0036
54

7
0037
55

8
0038
56

9
0039
57

:
003A
58

;
003B
59

<
003C
60

=
003D
61

>
003E
62

?
003F
63

 
4
 

@
0040
64

A
0041
65

B
0042
66

C
0043
67

D
0044
68

E
0045
69

F
0046
70

G
0047
71

H
0048
72

I
0049
73

J
004A
74

K
004B
75

L
004C
76

M
004D
77

N
004E
78

O
004F
79

 
5
 

P
0050
80

Q
0051
81

R
0052
82

S
0053
83

T
0054
84

U
0055
85

V
0056
86

W
0057
87

X
0058
88

Y
0059
89

Z
005A
90

[
005B
91

\
005C
92

]
005D
93

^
005E
94

_
005F
95

 
6
 

`
0060
96

a
0061
97

b
0062
98

c
0063
99

d
0064
100

e
0065
101

f
0066
102

g
0067
103

h
0068
104

i
0069
105

j
006A
106

k
006B
107

l
006C
108

m
006D
109

n
006E
110

o
006F
111

 
7
 

p
0070
112

q
0071
113

r
0072
114

s
0073
115

t
0074
116

u
0075
117

v
0076
118

w
0077
119

x
0078
120

y
0079
121

z
007A
122

{
007B
123

|
007C
124

}
007D
125

~
007E
126

DEL
007F
127


14.Table of ASCII Control Characters

Decimal Hexadecimal Binary Character Description
0 0 0 NUL null
1 1 1 SOH start of header
2 2 10 STX start of text
3 3 11 ETX end of text
4 4 100 EOT end of transmission
5 5 101 ENQ enquiry
6 6 110 ACK acknowledge
7 7 111 BEL bell
8 8 1000 BS backspace
9 9 1001 HT horizontal tab
10 0A 1010 LF line feed
11 0B 1011 VT vertical tab
12 0C 1100 FF form feed
13 0D 1101 CR enter / carriage return
14 0E 1110 SO shift out
15 0F 1111 SI shift in
16 10 10000 DLE data link escape
17 11 10001 DC1 device control 1
18 12 10010 DC2 device control 2
19 13 10011 DC3 device control 3
20 14 10100 DC4 device control 4
21 15 10101 NAK negative acknowledge
22 16 10110 SYN synchronize
23 17 10111 ETB end of trans. block
24 18 11000 CAN cancel
25 19 11001 EM end of medium
26 1A 11010 SUB substitute
27 1B 11011  ESC escape
28 1C 11100  FS file separator
29 1D 11101 GS group separator
30 1E 11110 RS record separator
31 1F 11111 US unit separator

15.Table of printable ASCII Characters


Decimal
Hexadecimal
BinaryCharacterDescription
3220100000Spacespace
3321100001!exclamation mark
3422100010"double quote
3523100011#number
3624100100$dollar
3725100101%percent
3826100110&ampersand
3927100111'single quote
4028101000(left parenthesis
4129101001)right parenthesis
422A101010*asterisk
432B101011+plus
442C101100,comma
452D101101-minus
462E101110.period
472F101111/slash
48301100000zero
49311100011one
50321100102two
51331100113three
52341101004four
53351101015five
54361101106six
55371101117seven
56381110008eight
57391110019nine
583A111010:colon
593B111011;semicolon
603C111100<less than
613D111101=equality sign
623E111110>greater than
633F111111?question mark
64401000000@at sign
65411000001A 
66421000010B 
67431000011C 
68441000100D 
69451000101E 
70461000110F 
71471000111G 
72481001000H 
73491001001I 
744A1001010J 
754B1001011K 
764C1001100L 
774D1001101M 
784E1001110N 
794F1001111O 
80501010000P 
81511010001Q 
82521010010R 
83531010011S 
84541010100T 
85551010101U 
86561010110V 
87571010111W 
88581011000X 
89591011001Y 
905A1011010Z 
915B1011011[left square bracket
925C1011100\backslash
935D1011101]right square bracket
945E1011110^caret / circumflex
955F1011111_underscore
96601100000`grave / accent
97611100001a 
98621100010b 
99631100011c 
100641100100d 
101651100101e 
102661100110f 
103671100111g 
104681101000h 
105691101001i 
1066A1101010j 
1076B1101011k 
1086C1101100l 
1096D1101101m 
1106E1101110n 
1116F1101111o 
112701110000p 
113711110001q 
114721110010r 
115731110011s 
116741110100t 
117751110101u 
118761110110v 
119771110111w 
120781111000x 
121791111001y 
1227A1111010z 
1237B1111011{left curly bracket
1247C1111100|vertical bar
1257D1111101}right curly bracket
1267E1111110~tilde
1277F1111111DELdelete


16.string methods

                     Method                                               Description
s.capitalize() The first character of s is put in uppercase
s.center([width]) Centers s in a field of length width
s.count(sub [,start [, end]]) Counts occurrences of sub between start and end
s.encode([encoding [, errors]]) Encode s using encoding as code and error
s.expandtabs([tabsize]) Expands tabs
s.find(sub [, start [, end]]) Finds the first occurrence of sub between start and end
s.index(sub [,start [, end]]) same as find but raises an exception if no occurrence is found
s.islower() Returns True if all chracters are lowercase, False otherwise
s.isupper() Returns True if all chracters are uppercase, False otherwise
s.join(words) Joins the list of words with s as delimiter
s.ljust(width) Left align s in a string of length width
s.lower() Returns a lowercase version of s
s.lstrip() Removes all leading whitespace characters of s
s.replace(old, new [, maxrep]) Replace maximal maxrep versions of substring old with substring new
s.rfind(sub [, start [, end]]) Finds the last occurrence of substring sub between start and end
s.rindex(sub [,start [, end]]) Same as rfind but raise an exception if sub does not exists
s.rjust(width) Right-align s in a string of length width
s.rstrip() Removes trailing whitespace characters
s.split([sep [, maxsplit]])) Split s into maximal maxsplit words using sep as separator (default whitespace)
s.splitlines([keepends]) Split s into lines, if keepends is 1 keep the trailing newline
s.strip() Removes trailing and leading whitespace characters
s.swapcase() Returns a copy of s with lowercase letters turn into uppercase and vice versa
s.title() Returns a title-case version of s (all words capitalized)
s.upper() Returns an uppercase version of s


17.common methods for Dictionaries


Table of common methods for Dictionaries

Method Name

Description
dict.clear()Removes all elements of dictionary.
dict.copy()Returns a shallow copy of dictionary.
dict.fromkeys(seq[,value])Create a new dictionary with keys from seq and values set to value.
dict.get(key, default=None)For key key, returns value or default if key not in dictionary
dict.items()Returns a view object of dict items.
dict.keys()Returns a view object of dict keys.
dict.pop(key)Remove key, Return value
dict.setdefault(key, default=None)Similar to get(), but will set dict[key]=default if key is not already in dict
dict.update(dict2)Adds dictionary dict2's key-values pairs to dict
dict.values()Returns a view object of dict_values.

18.File access Modes

File access Modes

Mode Description
r Opens a file for reading only. This is the default mode.
rb Opens a file for reading only in binary format
r+ Opens a file for both reading and writing. 
rb+ Opens a file for both reading and writing in binary format. 
w Opens a file for writing only. Overwrites the file if the file exists. Create a new file if it does not exist.
wb Opens a file for writing only in binary format. 
w+ Opens a file for both writing and reading. Overwrites the file if the file exists. Create a new file if it does not exist.
wb+ Opens a file for both writing and reading in binary format. 
a Opens a file for appending. The file pointer is at the end of the file if the file exists. 
ab Opens a file for appending in binary format. 
a+ Opens a file for both appending and reading. 
ab+ Opens a file for both appending and reading in binary format. 

19. Common File Methods

Common File Functions and Methods

Methods and Functions Description
open() returns a file object, and is most commonly used with two arguments: open(filename, mode).
file.close() Close the file
file.read([size])   Read entire file. If size is specified then read at most size bytes.
file.readline([size]) Read one line from file. If size is specified then read at most size bytes.
file.readlines([size])  Read all the lines from the file and return a list of lines. If size is specified then read at most size bytes.
file.write() Writes the contents of string to the file, returning the number of characters written.
file.tell() Returns an integer giving the file object’s current position in the file
file.seek() Changes the file object’s position

20.String Formatting

String Formatting

SymbolDescription
bFormat an integer in binary. 
cGiven a number, display the character that has that code. 
dDisplay a number in decimal (base 10). 
eDisplay a float value using the exponential format. 
ESame as e, but use a capital “E” in the exponent. 
fFormat a number in fixed-point form. 
gGeneral numeric format: use either f or g, whichever is appropriate. 
GSame as “g”, but uses a capital “E” in the exponential form. 
nFor formatting numbers, this format uses the current local setting to insert separator characters.
oDisplay an integer in octal format. 
xDisplay an integer in hexadecimal (base 16). Digits greater than 9 are displayed as lowercase characters. 
XDisplay an integer in hexadecimal (base 16). Digits greater than 9 are displayed as uppercase characters. 
%Display a number as a percentage: its value is multiplied by 100, followed by a “%” character. 

21.String Formatting Example

String Formatting Example Table

Number Format Output Description 
3.1415926{:.2f} 3.142 decimal places 
3.1415926{:+.2f} 3.142 decimal places with sign 
-1{:+.2f} -12 decimal places with sign 
2.71828{:.0f} 3No decimal places 
5{:0>2d} 5Pad number with zeros (left padding, width 2) 
5{:x<4d} 5xxx Pad number with x's (right padding, width 4) 
10{:x<4d} 10xx Pad number with x's (right padding, width 4) 
1000000{:,} 1,000,000Number format with comma separator 
0.25{:.2%} 25.00%Format percentage 
1E+09{:.2e} 1.00E+09Exponent notation 
13{:10d} 13Right aligned (default, width 10) 
13{:<10d} 13Left aligned (width 10)
13{:^10d} 13 Center aligned (width 10) 


22. Standard Exceptions

EXCEPTION NAME DESCRIPTION
ArithmeticError Base class for all errors that occur for numeric calculation.
AssertionError Raised in case of failure of the Assert statement.
AttributeError Raised in case of failure of attribute reference or assignment.
EnvironmentError Base class for all exceptions that occur outside the Python environment.
EOFError Raised when there is no input from either the raw_input() or input() function and the end of file is reached.
Exception Base class for all exceptions
FloatingPointError Raised when a floating point calculation fails.
ImportError Raised when an import statement fails.
IndentationError Raised when indentation is not specified properly.
IndexError Raised when an index is not found in a sequence.
IOError Raised when an input/ output operation fails, such as the print statement or the open() function when trying to open a file that does not exist.
IOError Raised for operating system-related errors.
KeyboardInterrupt Raised when the user interrupts program execution, usually by pressing Ctrl+c.
KeyError Raised when the specified key is not found in the dictionary.
LookupError Base class for all lookup errors.
NameError Raised when an identifier is not found in the local or global namespace.
NotImplementedError Raised when an abstract method that needs to be implemented in an inherited class is not actually implemented.
OverflowError Raised when a calculation exceeds maximum limit for a numeric type.
Raised when Python interpreter is quit by using the sys.exit() function. If not handled in the code, causes the interpreter to exit. Raised when an operation or function is attempted that is invalid for the specified data type.
RuntimeError Raised when a generated error does not fall into any category.
StandardError Base class for all built-in exceptions except StopIteration and SystemExit.
StopIteration Raised when the next() method of an iterator does not point to any object.
SyntaxError Raised when there is an error in Python syntax.
SystemError Raised when the interpreter finds an internal problem, but when this error is encountered the Python interpreter does not exit.
SystemExit Raised by the sys.exit() function.
SystemExit Raised when Python interpreter is quit by using the sys.exit() function. If not handled in the code, causes the interpreter to exit.
UnboundLocalError Raised when trying to access a local variable in a function or method but no value has been assigned to it.
ValueError Raised when the built-in function for a data type has the valid type of arguments, but the arguments have invalid values specified.
ZeroDivisonError Raised when division or modulo by zero takes place for all numeric types.





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++Write a program that will prepare a shipping label and determine the cost for a box of any size and mass. five attributes: type double length, width, height, mass, price six attributes: type string senderName, receiverName, senderAddress, receiverAddress, originCountry, destinationCountry (you can shorten names if you like, just make a comment about their meaning) a default constructor – basic initial values for all parameters a user constructor – input sender Name, senderAddress, originCountry a double function volume() – a function to return l×w×h a double function surfArea() – a function to return 2×(l×w+l×h+w×h) a double function girth() – a function to return the perimeter for the two smallest dimensions P=2(x+y), where x and y are the two smalles of l,w,h a double function maxDim() – a function to return the largest dimension from l,w,h a double function pricing() – a function to return the shipping price based on the table above an operator+ function – a function that determines the total cost of shipping (return type double).... (you may decide how to calculate.... a) add prices from both packages, b) apply fee or discount for number of packages) an operator>> function – a function to get the information from the user about the package.... OR.... a set of “ask” functions to ask the same information an operator<< function – a function to type the information in the shipping label format.... OR.... a set of “print” functions – to print the same information
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值