Advertisement

MOOC —— Python语言基础与应用 by 北京大学 第二章 :Python语言介绍与概览

阅读量:

第二章 :Python语言介绍与概览

  • 4、Python编程语言的运行平台
  • 5、编写第一个Python程序
  • 6、使用PyCharm作为集成开发环境
  • 7、实践环节:通过编写和调试Python程序熟悉其基础用法
  • 8、编程风格与规范
  • 9、数据对象及其组织结构
  • 10、程序流程控制与运算符
  • 测验题:章节测试题
4、python语言运行环境

python, pycharm的安装,就不再详述

5、第一个python程序

依旧是hello world!

6、集成开发工具pycharm

功能强大的开发工具,请您放心选择。https://www.jetbrains.com/pycharm/download/无需注册直接访问该平台即可下载。请参考官方文档或在线教程以掌握具体操作方法。

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等基本数据类型的变量
数据容器用于存储这些类型的数值对象
如列表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. 数据类型种类繁多,涵盖数值型、文本字符串型等多种形态。


答案:

  1. D
  2. B
  3. C
  4. C
  5. ACD
  6. ABCD

全部评论 (0)

还没有任何评论哟~