第二章 :Python语言介绍与概览
4、python语言运行环境
python, pycharm的安装,就不再详述
5、第一个python程序
依旧是hello world!
6、集成开发工具pycharm
很好用的开发工具,强烈推荐
https://www.jetbrains.com/pycharm/download/
下载community即可
如何使用Pycharm,建议百度一下,很多视频教学
7、上机练习:体验python程序
输入以下代码并运行成功,注意对齐和缩进,字母大小写、空格,左右括号的配对
import datetime
dtstr = input('Enter the datetime:(20170228):')
dt = datetime.datetime.strptime(dtstr, "%Y%m%d")
another_dtstr = dtstr[:4] + '0101'
another_dt = datetime.datetime.strptime(another_dtstr, "%Y%m%d")
print(int((dt - another_dt).days)+1)
运行结果
Enter the datetime:(20170228):20190108
8
# 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
import string # 不import也行
s = input('input a string:')
letter = 0
space = 0
digit = 0
other = 0
for c in s:
if c.isalpha():
letter += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
other += 1
print('There are %d letters, %d spaces, %d digits, and %d other characters in your string.'
% (letter, space, digit, other))
运行结果
input a string:abc 123 !@#
There are 3 letters, 2 spaces, 3 digits, and 3 other characters in your string.
# merge sort
# 归并排序
import random
def merge_sort(data_list):
if len(data_list) <= 1:
return data_list
middle = int(len(data_list) / 2)
left = merge_sort(data_list[:middle])
right = merge_sort(data_list[middle:])
merged = []
while left and right:
merged.append(left.pop(0) if left[0] <= right[0] else right.pop(0))
merged.extend(right if right else left)
return merged
data_list = [random.randint(1, 100) for _ in range(50)]
print(merge_sort(data_list))
运行结果
[3, 5, 10, 10, 11, 13, 14, 25, 28, 28, 28, 29, 29, 30, 33, 34, 34, 34, 39, 39, 41, 42, 53, 54, 57, 59, 59, 61, 62, 64, 66, 68, 68, 69, 69, 70, 70, 72, 73, 73, 74, 79, 80, 80, 81, 82, 84, 85, 88, 91]
# 猜数字游戏
import random
secret = random.randint(1, 100)
print('''猜数游戏!
我想了一个1-100的整数,你最多可以猜6次,
看看能猜出来吗?''')
tries = 1
while tries <= 6:
guess = int(input("1-100的整数,第%d次猜,请输入:" % tries))
if guess == secret:
print("恭喜答对了!你只猜了%d次!\n就是这个:%d!" % (tries, secret))
break
elif guess > secret:
print("不好意思,你的数大了一点儿!")
else:
print("不好意思,你的数小了一点儿!")
tries += 1
else:
print("哎呀!怎么也没猜中!再见!")
运行结果
猜数游戏!
我想了一个1-100的整数,你最多可以猜6次,
看看能猜出来吗?
1-100的整数,第1次猜,请输入:55
不好意思,你的数大了一点儿!
1-100的整数,第2次猜,请输入:33
不好意思,你的数小了一点儿!
1-100的整数,第3次猜,请输入:45
不好意思,你的数小了一点儿!
1-100的整数,第4次猜,请输入:50
不好意思,你的数大了一点儿!
1-100的整数,第5次猜,请输入:47
不好意思,你的数大了一点儿!
1-100的整数,第6次猜,请输入:46
恭喜答对了!你只猜了6次!
就是这个:46!
8、python程序设计风格
Python 哲学
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
9、数据对象及其组织
简单类型用来表示值
整数int、浮点数float、复数complex、逻 辑值bool、字符串str
容器类型用来组织这些值
列表list、元组tuple、集合set、字典dict
10、计算和控制流
控制流语句:顺序结构,条件分支,循环结构
定义语句:def 、 class
测验题
1、Python语言可以在哪些操作系统上运行?
A. Windows
B. Linux
C. macOS
D. 以上都可以
2、Python官方软件包自带的一个集成开发环境是(_____)。
A. PyCharm
B. Shell-IDLE
C. Eclipse
D. Anaconda
3、Python不支持以下哪种数据类型?
A. complex
B. list
C. char
D. float
4、Python中调用(____)模块的(_____)函数来实现求实数平方根的操作。
A. cmath abs
B. cmath sqrt
C. math sqrt
D. math abs
5、以下选项属于Python哲学内容的是(_____)。
A. 简单胜过复杂
B. 单纯不如冗余
C. 扁平胜于嵌套
D. 优美胜于丑陋
6、以下关于数据,描述正确的是(_____)。
A. 数据是信息的表现形式和载体。
B. 数据是对现实世界实体和概念的抽象。
C. Python语言在大数据分析处理领域应用广泛。
D. 数据类型多种多样,包括数值型、文本字符串型等等。
答案:
- D
- B
- C
- C
- ACD
- ABCD