python小练习

1.查看当前时间

import time

curTime = time.time()

zs = int(curTime) #总秒数

nows = zs % 60 #当前秒数

zm = zs // 60 #总分钟数

nowm = zm % 60 #当前分钟数

zh = zm // 24 #总小时数

nowh = zh % 24 #当前小时数

print("现在是格林尼治时间",nowh,"时",nowm,"分",nows,"秒")

print("从1970.1.1零点到现在,经历了",zs,"秒")


 

import datetime

curDate = datetime.datetime.now()

print("现在是:",curDate.year,"-",curDate.month,"-",curDate.day,"\n\t",curDate.hour,":",curDate.minute,":",curDate.second)

print(type(curDate))


2.自定义函数,收电费

#自定义函数

def costCompute(iStart,iEnd):

    iConsume = iEnd - iStart

    return iConsume*0.75

#可多次使用

fElecFee1 = costCompute(100,200)   #张

fElecFee2 = costCompute(230,400)   #李

print("张家电费:",fElecFee1)

print("李家电费:",fElecFee2)




3.乌龟画图

import turtle

t = turtle.Pen() #pen 对象

iCirclesCount = 30

for x in range(iCirclesCount):   #for循环

    t.circle(100)   #圆大小

    t.left(360/iCirclesCount)    #每一次向左旋转度数

4.《孙子算经》:今有雉兔同笼,上有三十五头,下有九十四足,问雉兔
各几何?

Head = 35   #头

Feet = 94   #脚  

a = Feet - Head * 2

Rabbits = a / 2     #兔子数量

Chicken = Head - Rabbits    #鸡数量

print(Feet == Chicken * 2 + Rabbits * 4)

print("鸡有:%d"%Chicken,"兔子有:%d"%Rabbits)

5.增 插入 删除

words = ["sasda","213232",32,"casa"]

words.append("add")  #增加一个

print("增加后的:",words)

words.insert(2,"mmm")  #插入到下标为2的位置

print("插入后:",words)

del words[2]

print("删除下标为2元素的后:",words)

sTitle = words.pop()

print("弹出后的:",words)

print("弹出的元素是:",sTitle)

6.数组嵌套

A = ["1000","2000","sasda","sdcs"]

B = ["2000","4000","swesda","scs"]

C = ["3000","5000","sfvdda","swwcs"]

D = ["4000","6000","sadbda","svvs"]

employees = [A,B,C] #把A,B,C数组嵌套进employees数组中

employees.append(D) #把D加入

print(employees[2])

print(employees[2][1])

 7.名字绑定

person1 = ["12","wang","98"]

person2 = person1   #赋予2地址,而不是内容

person2[1] = "zhang"  #改变了根本值

print(person1)

print(person2)  

8. 元素顺序

names = ["zhang","wang","li","cao","lin"]

names.sort()

print(names)

names.sort(key=len)  #名字参数 key = len(函数) 通过长度排序

print(names)

scores = [98,83,75,12,94,23,65.6]

scores.sort(reverse=True)   #倒着排

print(scores)

names = ["zhang","wang","li","cao","lin"]

namesSorted = sorted(names)  #names.sort()

print("names:",names)

print("namesSoryed:",namesSorted)

可以先不改变原本的元素顺序排序

names = ["zhang","wang","li","cao","lin"]

names.reverse()   #倒序

print(names)

9.for循环遍历

dummy = ["sacsa",2123,True,"sasa",2332,["A","B"]]

for x in dummy:

    print(x)

    print(type(x))

10.range数值列表

print("output of range(5):")

for i in range(5):   #[0,1,2,3,4]  range(0,5)

    print(i)

print("output of range(2,5):")  #[2,3,4]

for i in range(2,5):

    print(i)

x = range(5)  #range(5)只是一个生成规则

print(x[2])

print(x)

print(type(x))

 把一个range函数转化为列表

x = range(5)

x = list(x)

print(x,type(x))

fours = list(range(0,17,4))  #rang(从0开始,17结束,间隔4)

print(fours)

numbers = list(range(10,1,-2))

print(numbers)

 

11.列表统计

scores = [45,67,98,43,21,10,99]

print(max(scores))

print(min(scores))

print(sum(scores))

print("平均值:",sum(scores)/len(scores))

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值