且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python基础复习

更新时间:2022-09-25 07:38:13

#!/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")
print list
list.insert(1,"Fuck")
print list
list.reverse()
print list 
######################################
#字典实例复习
dict = {"a":"Hello","b":"World"}
print dict
for i in map(None,dict):
    print i
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:
    print "Same"
else:
    print "Different"
#字符串的查找与分割复习
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):
    print i
   

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,如需转载请自行联系原作者