python全天课学习笔记

简介:
if
奇数 1分钟:
>>> a=range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> n=0
>>> while n<len(a):
...     if n%2==1:
...         print a[n]
...     n+=1

>>> n=0
>>> while n<len(a):
...     if n%2==1:
...         print a[n]
...     n+=1
n=0
>>> a="abcefghijk"
>>> while n<len(a):
...     if n%2==1:
...         print a[n]
...     n+=1
    int i
>>> while i <= 10:
...     if i %2 != 0:
...         print i
...     i += 1
>>>
>> a =" 1 2 3 4 5 6 7 8 9 0"
>>> a1 = a.split()
>>> a1
['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
确定是否是fals

查找一句中有多少字母
s = “I am a boy”
>>> count=0
>>> word_list=s.split()
>>> word_list
['I', 'am', 'a', 'boy!']
>>> for i in word_list:
...     if 'a' in i:
...         count+=1
...
>>> print count
    ncount = 0
s = 'I ama aa boy'
s_list = s.split()
print s_list
for i in range(len(s)):
    print s[i]
    if s[i] == 'a':
        ncount += 1
print 'how many words :%s'%ncount

统计长度
>>> print len(word_list)
4
>>> len("abc")
3
>>> len({1:2}) 

死循环
while True:
    pass    

遍历
>>> for i in range(1,11,2):
...     print i
>>> for i in range(1,11,2):
...     print i >>> s="abcedfg"
>>> for i in range(len(s)):
...     print s[i]

质数
>>> 方法1:使用2-到它本身之间的所有数做除法,没有发生整除,则就是质数
方法2:使用2-到它本身平方根之间的所有数做除法,没有发生整除,则就是质数 
#encoding=utf-8
import math

number=int(raw_input("input a number:"))

for i in range(2,int(math.sqrt(number)+1)):
    if number %i ==0:
        print "%s is not prime number " %number
        break
else:
    print "%s is  prime number " %number

小题
a=[1,2,3,4,5,(1,2),(5,6)]
>>> for i in a:
...     if isinstance(i,(list,tuple)):
...         for j in i:
...             print j
...     else:
...         print i

for
>>> for i in range(5):
...     print i
... else:
...     print "no break happens!"
...
    >>> for i in range(5):
...     print i
...     break
... else:
...     print "no break happens!"
...
0

while
>>> n=2
>>> while n>=2:
...     n-=1
... else:
...     print "no break happens"
...
no break happens

习题
>>> for i in range(1,5):
...     for j in range(1,6):
...         print int(str(i)+str(j))
    >>> exec("print 'hello'")
hello
>>> eval("2*4")
8

Pass 和是否可迭代
from collections import Iterable
print isinstance('abc',Iterable)
True    

退出多重循环方法:

class getoutofloop(Exception): pass

try:
    for i in range(5):
        for j in range(5):
            for k in range(5):
                if i == j == k == 3:
                    raise getoutofloop()
                else:
                    print i, '----', j, '----', k
except getoutofloop:
    pass

def test():
    for i in range(5):
        for j in range(5):
            for k in range(5):
                if i == j == k == 3:
                    return
                else:
                    print i, '----', j, '----', k

test()

作业一去重:使用尽可能多的方法实现list去重
>>> set([1,1,1,2,2,3,3])
set([1, 2, 3])
>>> list(set([1,1,1,2,2,3,3]))
[1, 2, 3]
>>>
>>> d={}
>>> a=[1,1,1,2,2]
>>> for i in a:
...     d[i]=None
...
>>> print d.keys()
[1, 2]
算法:
1 声明一个新的list
2 把原list中的元素进行遍历,尝试放入到新list中
3 如果要放入的元素已经在新list中存在了,就不再次放入了,否则就放入
4 打印新list的内容
    a=[1,1,2,2,2,2,3,3]

for i in a:
    for j in range(a.count(i)-1):
        a.remove(i)

print a

七种方式去重
# #coding=utf-8
import time

time_start=time.time()
print u"列表去重的七种方法"
print u"第一种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
result=[]
for i in repeat_list:
    if i not in result:
        result.append(i)
print u"第一种去重结果: ",result
print u"第二种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
result={}
print u"第二种去重结果: ",list(result.fromkeys(repeat_list))
print u"第三种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
print u"第三种去重结果: ",list(set(repeat_list))
print u"第四种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
import itertools
def test_groupby(x):
    if x==1:
        print "lower"
    elif x>1 and x<4:
        print "middle"
    elif x>=4:
        print "higher"
repeat_list=sorted(repeat_list)
data=itertools.groupby(repeat_list,key=test_groupby)
for i,j in data:
    print list(j)
data=itertools.groupby(repeat_list)
result=[]
for i,j in data:
    result.append(i)
print u"第四种去重结果: ",result
print u"第五种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
for i in [j for j in repeat_list if repeat_list.count(i)>1]:
    for x in range(repeat_list.count(i)-1):
     repeat_list.remove(i)
print u"第五种去重结果: ",repeat_list
print u"第六种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
i=0
while i<=len(repeat_list)-1:
    if repeat_list.count(repeat_list[i])>1:
        repeat_list.pop(i)
    else:
        i+=1
print u"第六种去重结果: ",repeat_list
print u"第七种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
func=lambda x,y:x if y in x else x + [y]
print u"第七种去重结果: ",reduce(func,[[],]+repeat_list)
print "_"*20
print u"去重程序耗时%f" % (time.time()-time_start)
print "_"*20
time.sleep(3)
    Testing started at 17:38 ...
列表去重的七种方法
第一种测试方法
第一种去重结果:  [1, 2, 4, 5]
第二种测试方法
第二种去重结果:  [1, 2, 4, 5]
第三种测试方法
第三种去重结果:  [1, 2, 4, 5]
第四种测试方法
lower
lower
lower
middle
middle
higher
higher
higher
[1, 1, 1, 2, 2, 4, 5, 5]
第四种去重结果:  [1, 2, 4, 5]
第五种测试方法
第五种去重结果:  [4, 1, 2, 5]
第六种测试方法
第六种去重结果:  [4, 1, 2, 5]
第七种测试方法
第七种去重结果:  [1, 2, 4, 5]
____________________
去重程序耗时0.001000
____________________
=================
Process finished with exit code 0
Empty test suite.
3.实现数学中多项式求和公式的打印
result=[]
for i in range(6,-1,-1):
    if i == 0:
        result.append("a0")
        break
    result.append("a%sx^%s" %(i,i))
print "+".join(result

转换:
>>> "*".join(["1","2","3"]).split()
['1*2*3']
>>> "*".join(["1","2","3"]).split('*')
['1', '2', '3']

统计名字列表中,各名字的首字母在名字列表中出现的次数
第一种
name_list=['foster',"janet",'jessus','david']
count_dict={}
for i in name_list:
    count_dict[i]="".join(name_list).count(i[0])

print count_dict

第二种方法
name_list=['foster',"janet",'jessus','david']
count_dict={}
for i in name_list:
    count=0
    for j in name_list:
        if j.count(i[0])>=1:
             count+=j.count(i[0])
    count_dict[i]=count

print count_dict

输入三个数,判断是否能构成三角形

import math
a,b,c=input("please input three num a,b,c:")
d=min(a,b,c)
e=max(a,b,c)
if d<=0:
    print "error"
elif (a+b+c)>2*e:
    print U"能组成三角形"
else:
    print u"不能组成三角形"    输入三个数,判断是否能构成三角形
能构成三角形三边关系:
三边都大于零
两边之和大于第三边,两边之差小于第三边









本文转自 知止内明 51CTO博客,原文链接:http://blog.51cto.com/357712148/2046832,如需转载请自行联系原作者
目录
相关文章
|
13天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
14 1
|
17天前
|
Python
Python文件操作学习应用案例详解
【4月更文挑战第7天】Python文件操作包括打开、读取、写入和关闭文件。使用`open()`函数以指定模式(如'r'、'w'、'a'或'r+')打开文件,然后用`read()`读取全部内容,`readline()`逐行读取,`write()`写入字符串。最后,别忘了用`close()`关闭文件,确保资源释放。
18 1
|
9天前
|
Python
python学习3-选择结构、bool值、pass语句
python学习3-选择结构、bool值、pass语句
|
7天前
|
Python
基于Django的Python应用—学习笔记—功能完善
基于Django的Python应用—学习笔记—功能完善
|
7天前
|
机器学习/深度学习 算法 Python
使用Python实现集成学习算法:Bagging与Boosting
使用Python实现集成学习算法:Bagging与Boosting
18 0
|
8天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
49 0
|
9天前
|
Python
python学习14-模块与包
python学习14-模块与包
|
9天前
|
Python
python学习12-类对象和实例对象
python学习12-类对象和实例对象
|
9天前
|
数据采集 Python
python学习9-字符串
python学习9-字符串
|
9天前
|
Python
python学习10-函数
python学习10-函数

热门文章

最新文章