柱状图

饼图

小孩子才做选择!

图片来源:https://mp.weixin.qq.com/s/crkjqbV5ljerVKMXVegUYA。

Variables, Names, and Objects

In Python, everything—booleans, integers, floats, strings, even large data structures, functions, and programs—is implemented as an object. This gives the language a consistency (and useful features) that some other languages lack.

The type also determines if the data value contained by the box can be changed (mutable) or is constant (immutable). Think of an immutable object as a closed box with a clear window: you can see the value but you can’t change it. By the same analogy, a mutable object is like an open box: not only can you see the value inside, you can also change it; however, you can’t change its type.

if you want to know the type of anything (a variable or a literal value), use type(). In Python, “class” and “type” mean pretty much the same thing.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> type(a)
<class 'int'>
>>> type(b)
<class 'int'>
>>> type(58)
<class 'int'>
>>> type(99.9)
<class 'float'>
>>> type('abc')
<class 'str'>

Variable names can only contain these characters. Names cannot begin with a digit.

  • Lowercase letters (a through z)
  • Uppercase letters (A through Z)
  • Digits (0 through 9)
  • Underscore (_)

Finally, don’t use any of these for variable names, because they are Python’s reserved words:

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

Numbers

Here’s how to get both the (truncated) quotient and remainder at once:

1
2
>>> divmod(9,5)
(1, 4)

Otherwise, you could have calculated them separately:

1
2
3
4
>>> 9 // 5
1
>>> 9 % 5
4

Bases

A base is how many digits you can use until you need to “carry the one.” In base 2 (binary), the only digits are 0 and 1. 0 is the same as a plain old decimal 0, and 1 is the same as a decimal 1. However, in base 2, if you add a 1 to a 1, you get 10 (1 decimal two plus 0 decimal ones).

In Python, you can express literal integers in three bases besides decimal:

  • 0b or 0B for binary (base 2).
  • 0o or 0O for octal (base 8).
  • 0x or 0X for hex (base 16).

The interpreter prints these for you as decimal integers. Let’s try each of these bases.

1
2
3
4
5
6
7
8
>>> 10
10
>>> 0b10
2
>>> 0o10
8
>>> 0x10
16

In case you’re wondering what “digits” base 16 uses, they are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f. 0xa is a decimal 10, and 0xf is a decimal 15. Add 1 to 0xf and you get 0x10 (decimal 16). See more:https://hoas.xyz/post/getting-started-with-html-and-css-css/#web-colors.

int() will make integers from floats or strings of digits, but won’t handle strings containing decimal points or exponents:

1
2
3
4
5
6
7
8
>>> int('98.6')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '98.6'
>>> int('1.0e4')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.0e4'