p03

1. input()使用raw_input()读入一个合法表达式

2. 求阶层,递归和非递归方法:

def p(n):
    x = 1
    while (n > 1):
        x *= n 
        n -= 1
    return x

def pp(n):
    if n == 1 or n == 0:
        return 1
    else:
        return n * pp(n - 1)

print pp(input('input a int: '))

3. 斐波那契数列:

def ff(n):
    if n == 1 or n == 2:
        return 1
    else:
        return f(n - 1) + f(n - 2)

def f(n):
    if n == 1 or n == 2:
        return 1
    else:
        f1 = 1
        f2 = 1
        while(n > 2):
            f3 = f1 + f2
            f1 = f2
            f2 = f3
            n -= 1
        return f3
print f(input('input a int: '))


4. 汉诺塔:

def han(n, A, B, C):
    if n == 1:
        print A,"==>",C
    else:
        han(n-1, A, C, B)
        han(1, A, B, C)
        han(n-1, B, A, C)
han(input('input an int: '), 'A', 'B', 'C')

5. 字符串的长度:

>>> name = 'hero'

>>> len(name)

7


6. 字符串的拼接:

>>> na = name + ' you'

>>> print na

hero you


7. 字符串重复:

>>> name * 3

'heroherohero'

8. 判断一个字符串是否为另一个字符串的子串:

>>> name = 'how are you'

>>> 'h' in name

True

>>> 'how' in name

True

>>> 'x' in name

False


9. for遍历字符串:

for char in name:

print char


10. 计算字符串中元音个数:

def vowels_count(string):
    count = 0
    for c in string:
        if c in 'aeiouAEIOU':
            count += 1
    return count

11. >>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


12. 字符串索引:前向从0开始, 后向从-1开始,使用和c相同


13. 切片:

语法:[start : finish : countBy]

start :子序列开始位置的索引,如不提供默认第一个字符

finish :子序列结束位置的下一个字符的索引值, 默认最后一个字符

countBy :计数参数,默认1

eg : str[6 : 10], str[:10], str[6:], str[1:2:2]


14.  获取逆字符串:

str[::-1];


15. 字符串不可变:一旦生成,不可改变;要改变字符串中字符可通过切片方式:要改变下标1的字符

>>> string = 'hello'
>>> string = string[:1] + 'a' + string[2:]
>>> print string
hallo


16. string的replace(old, new)函数;replace方法返回一个新的字符串,原字符串内容不变;新字符串重新赋值给原来的变量

>>> string = 'hello'
>>> string = string.replace('l', 'p')
>>> print string
heppo


17. string.find('l'), 返回'l'的下标


18. split:

>>> s = 'hello world'
>>> s.split()
['hello', 'world']


19. 更多方法:dir(str)


20. 将name.txt文件中名字首字母大写(文件中每行一个名字):

f = open('names.txt')

for line in f:
    name = line.strip() #strip()将结尾的回车删除
    print name.title()    #title()将字符串首字母大写,其余小写

f.close()

21. 判断名字是否是回文:

<pre name="code" class="python">def is_palindrome(s):
    low = 0
    hi = len(s) - 1
    while low < hi:
        if s[low] != s[hi]:
            return False
        else:
            low += 1
            hi -=1
    return True
def is_palin(s):
    if s == s[::-1]:
        return True
    else:
        return False
def is_palind(s):
    if len(s) < 2:
        return True
    if s[0] != s[-1]:
        return False
    else:
        return is_palind(s[1:-1])
f = open('names.txt')
for line in f:
    name = line.strip()
    if is_palind(name):
        print name
f.close()


 

22. 判断字符串是否为升序:

def is_ascending(s):
    p = s[0]
    for c in s:
        if c < p:
            return False
        else:
            p = c
    return True

23. 字符串格式化:

>>> print "hello{}good{}.".format(5, 'day')
hello5goodday.
>>> print 'hello{}good{}.'.format(5, 'day')
hello5goodday.
>>> print 'hello {} good {}.'.format(5, 'day')
hello 5 good day.

括号的格式:

{field name:align width.precision type}

>>> import math
>>> print math.pi
3.14159265359
>>> print 'pi is {:f}'.format(math.pi)
pi is 3.141593
>>> print 'pi is {:.4f}'.format(math.pi)
pi is 3.1416
>>> print 'pi is {:9.4f}'.format(math.pi)
pi is    3.1416
>>> print 'pi is {:e}'.format(math.pi)
pi is 3.141593e+00


24. 正则表达式:导入re模块

. 表示任意字符

\d 表示任意数字

\d+ 表示一系列数字

[a-z]表示一个小写字母

import re
f = open('names.txt')
pattern = 'CC.A'
for line in f:
    name = line.strip()
    result = re.search(pattern, name)
    if result:
        print name
f.close()







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值