python基础1—零碎

已然好久没有在csdn上写文章了,这些系列文章是为了学习python而做的笔记 

# -*- coding: utf-8 -*-
# coding: utf-8

# 单行注释

'''
第一行
第二行
'''

"""
多行注释
在python中 ' 和 " 都是一样的
"""

# 连接行
str = 'abcd' \
       'efgh'
print(str)

str = 'Hello \n World'
print(str) 

str = """Hello
World"""
print(str)                      # 俩行  


print('abc"123"efg')      # abc"123"efg
print("abc'123'efg")                   # abc'123'efg
print('abc\'123\'efg')    # abc'123'efg

#----------------------------------#

print(type([1, 2, 3, 'a', 'b']))          # list
print(type((1, 'abc')))        # tuple
print(type(set(['a', 'b', 3])))        # set
print(type({'a':1, 'b':2}))               # dict

def func(a, b, c):
    print(a, b, c)
print(type(func))   # function
a = func
print(type(a))        # function

import string
print(string)   # module

# 类和类的实例
class MyClass(object):
    pass
print(type(MyClass))      # type
my_class = MyClass()
print(my_class)            # __main__.MyClass object at ox101caca10

#  for 循环
for i in range(0, 30, 5):
    print(i)


while 循环
a = 0
i = 1
while i<=100:
    a +=i
    i +=1
print(a)    


#  函数元组
def func_name(arg_1, arg_2):
    print(arg_1, arg_2)        # (1, 2)
    return arg_1, arg_2    
r = func_name(1, 2)
print(type(r))                 # tuple
print(r[0], r[1])


def func(x, y=500):
    print('x=', x)
    print('y=', y)
    return x+y
print(func(100))
print(func( y = 300, x = 200 ))
print(func( x = 400))

def func(p):
    print('x=', p['x'])
    print('y=', p['y'])

print(func({'x':100, 'y':200}))

# 可变参数   *numbers是元组 会将后面的参数打包成一个只读数组
def func(name, *numbers):
    print(type(numbers))
    print(numbers)

func('Tom', 1, 2, 3, 4, 'adb12', 'd')


def func(*args):
    print(args)
    print(type(args))  # tuple

func(1, 2, 3, 'a', 'b', 'c')
func('x=', 100, '; y=', 200)  

# 字典  **kvs means key/values
def func(name, **kvs) :
    print(name)
    print(type(kvs))        # dict
    print(kvs)
func('Tom', china = 'Beijing', uk = 'London')

# 可变参数必须放在后面 且 * 为必须带名字
def func(a, b, c, *, china, uk):
    print(china, uk)
func(1, 2, 3, china = 'BJ', uk = 'LD')    # 2.7.X 貌似不支持


def func(a, b, c=0, *args, **kvs):
    print(a, b, c)
    print(args)
    print(kvs)
func(1, 2)
func(1, 2, 3)
func(1, 2, 3, 'a', 'b', 'c')
func(1, 2, 3, 'a', 'b', china = 'BJ', uk = 'LD')
func(1, 2, 3, *('a', 'b'), **{'china' : 'BJ', 'uk' : 'LD'})


# 递归问题
def my_sum(i):
    if i < 0 : 
        raise ValueError
    elif i <= 1 :
        return i
    else :
        return i + my_sum(i-1)

print(my_sum(1))
print(my_sum(5))
print(my_sum(500))

# f(n) = f(n-1) + f(n-2)

def fib(n):
    if n < 1 :
        raise ValueError
    elif n <=2 :
        return 1
    else :
        return fib(n-1) + fib(n-2)

print(fib(40))

#  函数是可以作为参数
def sum(x, y, p = None ) :
    s = x + y
    if p:
        p(s)
    return s

sum(100, 200) 
sum(100, 200, print)      # can not run in python2.7x

def cmp(x, y, cp = None) :
     if not cp :
         if x > y :
             return 1
         elif x < y :
             return -1
         else :
             return 0
     else :
         return cp(x, y)

def my_cp(x, y):
    if x < y :
        return 1
    elif x == y :
         return 0
    else : 
        return -1
print(cmp(100, 200))
print(cmp(100, 200, my_cp))

print([1,2,3,4], sum)

def do_sum(data , method):
    return method(data)

print(sum([10, 20]))
print(do_sum([1, 2, 3, 4], sum) )


# homework : change '  Hello, how are u? ' to  ' u? are how ,Hello  '
# 思路:先将单词逆序,再将整个句子逆序
def reverse(str_list, start, end):
    while(start < end):
        str_list[start], str_list[end] = str_list[end], str_list[start]
        start += 1
        end -= 1
setence = '  Hello, how are u? '
str_list = list(setence)
i = 0 
while i < len(str_list):
    if str_list[i] != ' ':
        start = i
        end = start + 1
        while (end < len(str_list)) and str_list[end] != ' ':
            end +=1
        reverse(str_list, start, end - 1)
        i = end
    else:
        i += 1
str_list.reverse()
print(''.join(str_list))

 

posted @ 2018-01-22 16:07  zeroonec  阅读(199)  评论(0编辑  收藏  举报