Chinaunix首页 | 论坛 | 博客
  • 博客访问: 298251
  • 博文数量: 240
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-04 18:14
文章分类

全部博文(240)

文章存档

2017年(8)

2014年(4)

2013年(15)

2012年(4)

2011年(14)

2010年(55)

2009年(140)

我的朋友

分类: Python/Ruby

2010-07-30 11:38:49

if.py
 

# if elif else语句
score = input("score:")
if(score >= 90) and (score <= 100):
    print "A"
elif(score >= 80) and (score < 90):
    print "B"
elif(score >= 60) and (score < 80):
    print "C"
else:
    print "D"


 

case_switch.py

x = 1; y = 2

operator = "+"

def fun01():
      return x + y

def fun02():
      return x - y

def fun03():
      return x * y

def fun04():
      return x / y

def fun05():
      print 'error'

result = {
    "+" : fun01(),
    "-" : fun02(),
    "*" : fun03(),
    "/" : fun04(),
        "" : fun05()
}

print result.get(operator)


 

while.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
x = 0
while x < 50:
    x += 1
    print '第 %d 次循环.' % x


 
for.py
 

#!/usr/bin/python
# -*- coding: UTF-8 -*-
for x in range(0, 10, 2):
    print x


for_else.py

#!/usr/bin/python
# -*- coding: gbk -*-
import types
import os,sys

x = raw_input("输入x的值:")

if not x.isdigit():
    print 'Error: not a number!!!'
    sys.exit()
else:
    x = int(x)
    
for y in range(0, 100):
    if x == y:
        print "找到数字:", x
        break
else:
    print "没有找到您输入的数字!"

 

冒泡法排序:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 冒泡排序
def sort(numbers):
    for j in xrange(len(numbers)-1, -1, -1):
        for i in xrange(j):
            if numbers[i] > numbers[i+1]: # 把数值大的数字放到末端
                numbers[i], numbers[i+1] = numbers[i+1], numbers[i]

def main():
    numbers = [23, 12, 9, 15, 6, 8, 92, 88]
    sort(numbers)
    print numbers

if __name__ == '__main__':
    main()


if_elif_else.py

 

score = input("score:")
if(score >= 90) and (score <= 100):
    print "A"
elif(score >= 80) and (score < 90):
    print "B"
elif(score >= 60) and (score < 80):
    print "C"
else:
    print "D"

 

tuple.py

#打包
tuple = ("apple", "banana", "grape", "orange")


#解包
a, b, c, d = tuple
print a, b, c, d


list.py

list1 = ["grape", "grape"]
list2 = ["apple", "lst", "orange"]
list = ["apple", "banana", "grape", "orange"]

mylist=[i for i in list1 if i not in list2]

list1.extend(list2)
list1 = ["apple", "banana"] * 2
list1 = list1 + list3

list.sort()
list.reverse()
sorted(set(list))

队列:
list.append("orange")
list.pop()
list.pop(0)

list.insert(1, "grape")
list.remove("grape")
list.index("apple")

print list[-2]
print list[1:3]
print list[-3:-1]


 
dic.py
 

dict = {1 : "apple", 2 : "banana", 3 : "grape", 4 : "orange"}
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}


# 字典的元素
print dict.items()
>>> [('a', 'apple'), ('b', 'banana'), ('o', 'orange'), ('g', 'grape')]
print dict.keys()
>>> ['a', 'b', 'o', 'g']
print dict.values()
>>> ['apple', 'banana', 'orange', 'grape']


# 添加删除元素
dict["w"] = "water"
del dict['w']

dict.pop("b")
dict.clear()



# 字典遍历
for k in dict:
    print "dict[%s] =" % k,dict[k]

for (k, v) in dict.items():
    print "dict[%s] =" % k, v


#字典更新
dict = {"a" : "apple", "b" : "banana"}
print dict
>>> {'a': 'apple', 'b': 'banana'}
dict2 = {"c" : "grape", "d" : "orange"}
print dict2
>>> {'c': 'grape', 'd': 'orange'}
dict.update(dict2)
print dict
>>> {'a': 'apple', 'c': 'grape', 'b': 'banana', 'd': 'orange'}


# 字典排序
# 按照key排序
print sorted(dict.items(), key=lambda d: d[0])
# 按照value排序
print sorted(dict.items(), key=lambda d: d[1])


阅读(1062) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~