python习题实例

  用以记录python学习过程中做过的小习题~ 

1.生成两个列表,分别存放将100以内的偶数&奇数

odd_number=[]
even_number=[]
for i in range(1,101):
    if i%2==0:
        odd_number.append(i)
    else:
        even_number.append(i)
print 'the odd number list is:','\n',odd_number
print 'the even number list is:','\n',even_number

执行结果

the odd number list is: 
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
the even number list is: 
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]

知识点

range()

创建一个整数列表,一般用在 for 循环中

range(start, stop[, step])

参数说明:

start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);

stop: 计数到 stop 结束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5

step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

>>> a=range(1,5)
>>> print a
[1, 2, 3, 4]

list.append()

在列表末尾添加新的对象, 该方法无返回值,但是会修改原来的列表。

list.append(obj)

obj -- 添加到列表末尾的对象

>>> a=[1,2,3]
>>> a.append(4)
>>> a
[1, 2, 3, 4]

‘\n’

换行符

>>> print 'hello','\n','world'
hello 
world

2.生成8位密码,需包含大小写字母,数字,下划线

import random

number=str(random.randint(0,9))
lower=str(chr(random.randint(97,122)))
capital=str(chr(random.randint(65,90)))
underline='_'

type=[number,lower,capital,underline]
new_type=random.choice(type)
password=[number,lower,capital,underline]

i=1
while i <=4:
    password.append(new_type)
    i+=1
else:
    random.shuffle(password)

print ''.join(password) 

 

执行结果

_yyy5yyX

_xxx7xBx

……

知识点

random模块

获取随机数模块,使用前需导入,import random。

常用函数:

random.random

返回随机生成的一个实数,它在[0,1)范围内。

>>> random.random()
0.005544345491154901

random.randint

生成一个指定范围内的整数,其中范围包含上下限

>>> random.randint(1,10)
4

random.choice

从序列中获取一个随机元素,并返回

>>> a=[1,2,3]
>>> random.choice(a)
3
>>> random.choice('hello')
'l'

random.shuffle

随机排列列表中的元素,会修改列表本身。

>>> a
[2, 1, 3]
>>> a=[1,2,3]
>>> random.shuffle(a)
>>> a
[3, 2, 1]
>>> random.shuffle(a)
>>> a
[2, 3, 1]

join()

将序列中的元素以指定的字符连接生成一个新的字符串

str.join(sequence)

sequence -- 要连接的元素序列。

>>> str='-'
>>> seq=['a','b','c']
>>> print str.join(seq)
a-b-c

  

3.列表元素去重

方法一

list_a=[1,1,1,2,2]
list_b=[]

for i in list_a:
    if i in list_b:
        continue
    else:
        list_b.append(i)

print list_b

执行结果

[1, 2]

  

方法二

>>> list_a=[1,1,1,2,2]
>>> list(set(list_a))
[1, 2]

知识点

set()

创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等

set([iterable])

iterable -- 可迭代对象对象

>>> x=set('hello')
>>> y=set('good world')
>>> x,y      # 重复的被删除
(set(['h', 'e', 'l', 'o']), set([' ', 'd', 'g', 'l', 'o', 'r', 'w']))
>>> x&y      # 交集
set(['l', 'o'])
>>> x|y      # 并集
set([' ', 'e', 'd', 'g', 'h', 'l', 'o', 'r', 'w'])
>>> x-y      # 差级
set(['h', 'e'])

4.统计一个字符串,有多少个特定字符

 统计这句话有几个字母‘a’

方法一

s='i am a boy'
count=0
for i in s:
    if i =='a':
        count+=1
print count

执行结果

2

方法二

>>> s='i am a boy'
>>> s.count('a')
2

知识点

count()

用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

str.count(sub, start= 0,end=len(string)) 

>>> s='i am a boy'
>>> s.count('a')
2

>>> s='i am a boy'
>>> s.count('a',3,9)
1

对序列也可以使用

>>> ss=['1','a','a','3']     #列表
>>> ss.count('a')
2
>>> ss=('1','a','a','3')     #元祖
>>> ss.count('a')
2

统计这句话有几个单词包含字母‘a’

s='i am a boy'
count=0
a_word=[]
new_dict=s.split(' ')
for i in new_dict:
    if 'a' in i:
        count+=1
        a_word.append(i)
print 'count is:',count
print 'word list is:',a_word

运行结果

count is: 2
word list is: ['am', 'a']

知识点

split()

通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串

str.split(str="", num=string.count(str)).

str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。

num -- 分割次数。

>>> str='hello-good-world'
>>> print str.split('-')
['hello', 'good', 'world']

5.判断一个数是不是质数

# -*- coding: utf-8 -*- 
import math
number=int(raw_input('please enter a number:'))
for i in range(2,int(math.sqrt(number)+1)):     #平方根方法
#for i in range(2,number):
    if number % 2 != 0:
        print u'是质数'
        break
else:
    print u'不是质数'

运行结果

please enter a number:89
是质数

知识点

Math模块

math.sqrt

返回数字的平方根,使用前先导入math模块

>>> import math
>>> math.sqrt(6)
2.449489742783178

math.pow

返回 xy(x的y次方) 的值

math.pow( x, y )

>>> math.pow(2,3)
8.0

math.ceil/floor

向上/下取整

>>> math.ceil(1.1)
2.0
>>> math.floor(1.1)
1.0

其他几个常见函数

abs

返回数值的取绝对值

>>> abs(1)
1
>>> abs(-1)
1

round

将数值四舍五入

>>> round(1.1111)
1.0
>>> round(1.5555)
2.0

cmp

比较两个数值的大小

>>> cmp(1,1)    #相等
0
>>> cmp(2,1)    #大于
1
>>> cmp(1,2)    #小于
-1

max&min

找出序列中最大/最小的数值

>>> max([1,2,3,4])
4
>>> min([1,2,3,4])
1

未完待续 ..........

猜你喜欢

转载自www.cnblogs.com/lilip/p/9231142.html