zoukankan      html  css  js  c++  java
  • <潭州教育>-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

  • 相关阅读:
    Android应用程序请求SurfaceFlinger服务创建Surface的过程分析
    和菜鸟一起学linux总线驱动之初识i2c驱动主要结构
    和菜鸟一起学linux之container_of实例
    和菜鸟一起学linux总线驱动之初识spi驱动主要结构
    和菜鸟一起学android4.0.3源码之按键驱动短长按功能
    和菜鸟一起学linux总线驱动之初识i2c总线协议
    程序溢出的基础和原理
    Network Information Detection程序作品+源代码
    Network Information Detection程序作品+源代码
    渗透asp后门源代码
  • 原文地址:https://www.cnblogs.com/Stone-Fei/p/8457130.html
Copyright © 2011-2022 走看看