潭州教育学python_<潭州教育>-Python学习笔记@基础讲解&作业1

作业:

1.定义讲过的每种数值类型,序列类型

数值类型:

整型:int

字符型:float

字符串:str

布尔型: bool

序列类型:

列表: 有序,可变,元素类型没有固定要求

lst = [1,2,3]

元祖:有序,不能改变,元素类型没有固定要求

tuple_list = (1,2,3,4,5)

字典: 无序,键值对组合,利于检索

dict = {'username':'Stone','passwd':'helloworld'}

集合: 无序,元素类型没有固定要求

set_list = {1,2,3,4}

2.python里面怎么注释代码?

方法1:代码开头加'#'

# this is a example

##方法2: 三引号包裹注释部分

方法2: 三引号注释

'''

this is two example

this is three example

'''

拓展部分:注释代码应用,构造文档字符串#文档字符串,用来解释功能函数的帮助文档

#文档的使用要求:必须在函数的首行,

#查看方式: 交互模式下用help查看函数,文本模式下用function.__doc__查看

def inputxy():

'''this is a documents file'''

print 'hello Stone'

inputxy()

print inputxy.__doc__

##3.简述变量的命名规则

'''

Python变量主要由字母,数字,下划线组成,跟C语言一样,数字不能在变量的开头,为了便于区分识别,定义变量最好简单易懂,与实际吻合便于理解,变量

命名比较好的是遵循驼峰命名法。

'''

4.有一个列表 li= ['a','b','c','d','e'],用第一节课的知识,将列表倒序,然后用多种方法# 取出第二个元素

# 不能用列表方法reverse以及函数reversed,方法越多越好。

li = ['a','b','c','d','e']

##方法1

li = li[::-1] # 倒序切片

print li

#方法2:

li = ['a','b','c','d','e']

li.reverse()

print li

#方法3:

li = ['a','b','c','d','e']

l = reversed(li)

new_list = []

for x in l:

# print x,

new_list.append(x)

print new_list

##print list(reversed(li))

#方法四:

li = ['a','b','c','d','e']

print sorted(li,reverse = True)

5.有个时间形式是(20180206),通过整除和取余,来得到对应的日,月,年。请用代码完成。

#时间字符串处理

date = '20180206'

# way 1 利用字符串切片操作

year = date[0:4]

month = date[4:6]

day = date[6:8]

print year,month,day

# way 2 ## 利用除&取余求值

Year = int(date)/10000

Month = (int(date)/100)%100

Day = int(date)%100

print Year

print Month

print Day

#way 3 利用时间数组求值

import time

time_struct = time.strptime(date,'%Y%m%d')

years = time.strftime('%Y',time_struct)

months = time.strftime('%m',time_struct)

days = time.strftime('%d',time_struct)

print years,months,days,

6.有一个半径为10的圆,求它的面积(math模块有π)

import math

pi = math.pi

r = 10

s = pi*r*r

print '面积是:', '%.6s'%s

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值