天天看點

Python基礎複習

#!/usr/bin/python

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

temp = "hackfreer"

temp1 = 1.5

print "%s,%.1f"%(temp,temp1)

######################################

#判斷結構複習

def compare(number):

    if(number > 10):

        return 0

    if(number == 10):

        return 10

    if(number < 10):

        return 1

num = input("Please input a number:")

print compare(num)

#while循環結構複習

def circle(array):

    i = 0

    while i < len(array):

        print array[i]

        i += 1

tmp = raw_input("Please enter a num string:").split(",")

circle(tmp)

#元組複習

tuple = ("I","Love","You","And","You")

print tuple[0:3]

print tuple[3:]

a = "HackBy:"

b = "Hackfreer"

tmp = (a,b)

print tmp

tup1 = (1,2,3,4,5,6,7)

for a in map(None,tup1):

    print a

#清單執行個體複習

list = ["Hello","World","I","Love","You"]

print list

for i in map(None,list):

    print i

list.append("Fuck")

list.insert(1,"Fuck")

list.reverse()

print list 

#字典執行個體複習

dict = {"a":"Hello","b":"World"}

print dict

for i in map(None,dict):

print "%s,%(a)s,%(b)s"%{"a":"Hello","b":"World"}

#apply()函數執行個體,将變量傳遞給函數

def useApply(x = 1,y = 2):

    return x * y

print apply(useApply,(1,4))

#filter()函數執行個體複習,對資料指定函數過濾

def useFilter(i):

    if i > 0:

        return i

print filter(useFilter,range(-8,10))

#map()函數執行個體複習,避免使用循環

def useMap(x):

    return x ** x

print map(useMap,range(1,3))

#Buffer()函數執行個體複習,定位取值

print buffer("helloworld",1,4)

####################################

#Lambda函數執行個體複習

x = lambda a,b:a + b

print x(1,2)

#字元串的格式化

str1 = "Code By Hackfreer"

str2 = "Version 3.0"

print "%s %s"%(str1,str2)

print str1.center(20)

print str2.ljust(10)

#字元串的轉義

string1 = "Helllo\tThe\tWorld\n"

print len(string1)

string2 = r"Helllo\tThe\tWorld\n"

print len(string2)

#字元串的連接配接執行個體

strs1 = "hello"

strs2 = "the"

strs3 = "world"

strs4 = "too"

result = strs1+strs2+strs3

result += strs4

print result

rs = ["My","Name","is","Hackfreer"]

result1 = " ".join(rs)

print result1

#字元串的比較複習

pass1 = 123456

pass2 = "123456"

if pass1 == pass2:

    print "Same"

else:

    print "Different"

if str(pass1) == pass2:

#字元串的查找與分割複習

word = "select * from admin where id=1"

print word[9]

print word.split(" ")

#

import re

f1 = file("my.txt","r")

for i in map(None,f1):

count = 0

s = ""

for s in f1.readlines():

    li = re.findall("my",s)

    if len(li) > 0:

        count = count + li.count("my")

print "This file have "+str(count)+" my"

f1.close()

本文轉sinojelly51CTO部落格,原文連結:http://blog.51cto.com/pnig0s1992/392124,如需轉載請自行聯系原作者