您的位置:首页 > 编程语言 > Python开发

-Python学习笔记@基础讲解&作业1

2020-06-10 04:43 281 查看

作业:

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

 

转载于:https://www.cnblogs.com/Stone-Fei/p/8457130.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: