提交 7817c8a2 编写于 作者: J jackfrued

新增了《为什么我选择了Python》

上级 9fdf3874
......@@ -276,7 +276,7 @@ if __name__ == '__main__':
### 使用元组
Python 中的元组与列表类似也是一种容器数据类型,可以用一个变量(对象)来存储多个数据,不同之处在于元组的元素不能修改,在前面的代码中我们已经不止一次使用过元组了。顾名思义,我们把多个元素组合到一起就形成了一个元组,所以它和列表一样可以保存多条数据。下面的代码演示了如何定义和使用元组。
Python中的元组与列表类似也是一种容器数据类型,可以用一个变量(对象)来存储多个数据,不同之处在于元组的元素不能修改,在前面的代码中我们已经不止一次使用过元组了。顾名思义,我们把多个元素组合到一起就形成了一个元组,所以它和列表一样可以保存多条数据。下面的代码演示了如何定义和使用元组。
```Python
# 定义元组
......@@ -319,30 +319,38 @@ Python中的集合跟数学上的集合是一致的,不允许有重复元素
![](./res/python-set.png)
可以按照下面代码所示的方式来创建和使用集合。
```Python
# 创建集合的字面量语法
set1 = {1, 2, 3, 3, 3, 2}
print(set1)
print('Length =', len(set1))
# 创建集合的构造器语法(面向对象部分会进行详细讲解)
set2 = set(range(1, 10))
print(set2)
set3 = set((1, 2, 3, 3, 2, 1))
print(set2, set3)
# 创建集合的推导式语法(推导式也可以用于推导集合)
set4 = {num for num in range(100) if num % 3 == 0 or num % 5 == 0}
```
向集合添加元素和从集合删除元素。
```Python
set1.add(4)
set1.add(5)
set2.update([11, 12])
print(set1)
print(set2)
set2.discard(5)
# remove的元素如果不存在会引发KeyError
if 4 in set2:
set2.remove(4)
print(set2)
# 遍历集合容器
for elem in set2:
print(elem ** 2, end=' ')
print()
# 将元组转换成集合
set3 = set((1, 2, 3, 3, 2, 1))
print(set1, set2)
print(set3.pop())
print(set3)
```
集合的成员、交集、并集、差集等运算。
```Python
# 集合的交集、并集、差集、对称差运算
print(set1 & set2)
# print(set1.intersection(set2))
......
class Test01 {
public static void main(String[] args) {
System.out.println("hello, world!");
}
}
\ No newline at end of file
class Test02 {
public static void main(String[] args) {
int total = 0;
for (int i = 1; i <= 100; ++i) {
total += i;
}
System.out.println(total);
}
}
\ No newline at end of file
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class Test03 {
/**
* 产生[min, max)范围的随机整数
*/
public static int randomInt(int min, int max) {
return (int) (Math.random() * (max - min) + min);
}
public static void main(String[] args) {
List<Integer> redBalls = new ArrayList<>();
for (int i = 1; i <= 33; ++i) {
redBalls.add(i);
}
List<Integer> selectedBalls = new ArrayList<>();
for (int i = 0; i < 6; ++i) {
selectedBalls.add(redBalls.remove(randomInt(0, redBalls.size())));
}
Collections.sort(selectedBalls);
selectedBalls.add(randomInt(1, 17));
for (int i = 0; i < selectedBalls.size(); ++i) {
System.out.printf("%02d ", selectedBalls.get(i));
if (i == selectedBalls.size() - 2) {
System.out.print("| ");
}
}
System.out.println();
}
}
\ No newline at end of file
print('hello, world!')
\ No newline at end of file
print(sum(range(1, 101)))
\ No newline at end of file
from random import randint, sample
# 初始化备选红色球
red_balls = [x for x in range(1, 34)]
# 选出六个红色球
selected_balls = sample(red_balls, 6)
# 对红色球进行排序
selected_balls.sort()
# 添加一个蓝色球
selected_balls.append(randint(1, 16))
# 输出选中的随机号码
for index, ball in enumerate(selected_balls):
print('%02d' % ball, end=' ')
if index == len(selected_balls) - 2:
print('|', end=' ')
print()
\ No newline at end of file
## 为什么我选择了Python
目前,Python语言的发展势头在国内国外都是不可阻挡的,Python凭借其简单优雅的语法,强大的生态圈从众多语言中脱颖而出,如今已经是稳坐编程语言排行榜前三的位置。国内很多Python开发者都是从Java开发者跨界过来的,我自己也不例外。我简单的跟大家交代一下,我为什么选择了Python。
### Python vs. Java
我们通过几个例子来比较一下,做同样的事情Java和Python的代码都是怎么写的。
例子1:在终端中输出“hello, world”。
Java代码:
```Java
class Test {
public static void main(String[] args) {
System.out.println("hello, world");
}
}
```
Python代码:
```Python
print('hello, world')
```
例子2:从1到100求和。
Java代码:
```Java
class Test {
public static void main(String[] args) {
int total = 0;
for (int i = 1; i <= 100; i += 1) {
total += i;
}
System.out.println(total);
}
}
```
Python代码:
```Python
print(sum(range(1, 101)))
```
例子3:双色球随机选号。
Java代码:
```Java
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class Test {
/**
* 产生[min, max)范围的随机整数
*/
public static int randomInt(int min, int max) {
return (int) (Math.random() * (max - min) + min);
}
public static void main(String[] args) {
// 初始化备选红色球
List<Integer> redBalls = new ArrayList<>();
for (int i = 1; i <= 33; ++i) {
redBalls.add(i);
}
List<Integer> selectedBalls = new ArrayList<>();
// 选出六个红色球
for (int i = 0; i < 6; ++i) {
selectedBalls.add(redBalls.remove(randomInt(0, redBalls.size())));
}
// 对红色球进行排序
Collections.sort(selectedBalls);
// 添加一个蓝色球
selectedBalls.add(randomInt(1, 17));
// 输出选中的随机号码
for (int i = 0; i < selectedBalls.size(); ++i) {
System.out.printf("%02d ", selectedBalls.get(i));
if (i == selectedBalls.size() - 2) {
System.out.print("| ");
}
}
System.out.println();
}
}
```
Python代码:
```Python
from random import randint, sample
# 初始化备选红色球
red_balls = [x for x in range(1, 34)]
# 选出六个红色球
selected_balls = sample(red_balls, 6)
# 对红色球进行排序
selected_balls.sort()
# 添加一个蓝色球
selected_balls.append(randint(1, 16))
# 输出选中的随机号码
for index, ball in enumerate(selected_balls):
print('%02d' % ball, end=' ')
if index == len(selected_balls) - 2:
print('|', end=' ')
print()
```
相信,看完这些例子后,你一定感受到了我选择了Python是有道理的。
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册