针对初学者的《python 核心编程第二章习题答案》——半详解

在上一篇中已经说过了在哪里下载以及安装对应的 python 编码环境,有需要学习的初学者宝宝去上篇瞅瞅。

本篇着重讲《Python 核心编程》的第二章习题哦~  我从一个初学者的角度来讲哈(我自己也是初学者啦)


这个是最简单的输出 hello world ,话说有个笑话是以为退休的程序员百无聊赖想着得学点什么,最后选择了书法,拿起毛笔,举笔良久不知该写什么,最后毅然落下“hello world”2333
print 语法在不同的 python 版本里会有些许偏差,还有一种是不带括号的写法
#print("hello world!")


注释语法
单行注释时在句首加一个 # 号即可
多行注释时,在行首和行尾分别添加 ''' 即可


另一个必用的简单函数,初学者一定会用到的等待用户输入。
#等待用户输入内容函数: input, 3.x 以后 Input 等效于之前版本的 raw_input
#input_name = input("enter your name:")
#print(input_name)


#python 核心编程第二章习题 2-7
    #while 循环
'''
string1 = input("enter a string please:")
i = len(string1)                           # 或者用户输入的 string 长度, len 是自带函数
j = 0
while j < i:                               # while 循环的语法跟其他语言类似,不往细了说咯
    print(string1[j])
    j += 1

    #for 循环
string1 = input("enter a string please:")    
for item in string1:                       # 这个是 for 循环跟其他语言不太一样,需要理解一下,这里相当于把每个字母当成一个 item 去输出
    print(item)
'''

#while 循环实现 2-8
'''
atuple = (1,2,3,4,5)
tulength = len(atuple)
i = amount = 0
while i<tulength:
    amount += atuple[i]
    i+=1
print(amount)

i = 0
amount = 0
list_for_user = []
while i<5:
    j = int(input("enter a number:"))
    list_for_user.append(j)
    amount += list_for_user[i]
    i += 1
print(list_for_user)
print(amount)
'''

#for 循环实现2-8
'''
alist = []                  #创建一个空的列表
i = 0
amount = 0
for i in range(5):
    j = int(input("enter a number:"))
    alist.append(j)                       #由于列表是空的,所以不能使用[1]这种序号来查询,只能用 append 函数在列表尾部添加元素
    amount += alist[i]
print(alist)
print(amount)
'''

#2-9

atuple = (1,2,3,5,7)
i=0
len_of_atuple = len(atuple)
for item in atuple:
    i += item
print(i/len_of_atuple)


#2-10
'''
num = int(input("enter a number between 1 to 100:"))        #我这个用户默认输入的是 string 不进行 int 转换的话会报错
i = 1
while i != 0:
    if 1 <= num <= 100:
        print("succeed")
        i = 0;
    else:
        num = int(input("you shoule enter a num between 1 to 100:"))
'''

#2-11
'''
print("1 means get amount of five nums, 2 means get average of five nums, X means ending ")
flag = 1
amount = 0
condition = "c"
while condition != "X":
    while condition != "X":
        while flag == 1:
            j = input("enter your choose:")
            if j == "1" or j == "2" or j == "X":
                flag = 2
            else:
                print("selece your choice from 1,2,X")
        if j == "1":
            for i in range(5):
                amount += int(input("enter a num:"))
            print(amount)
            flag = 1
        elif j == "2":
            for i in range(5):
                amount += int(input("enter a num:"))
            print(amount / 5)
            flag = 1
        else:
            condition = "X"
'''

#2-12
#print(dir.__doc__)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值