您的位置:首页 > 编程语言 > Python开发

python学习三:byteofpython的学习

2013-08-24 19:17 447 查看
一边看一边写了点测试代码。

"""
this is the learning testing of byteofpython book
"""

print r"RAW STRING: New lines indicated by \n"

words = "what\'s"" your name?"
print "AUTO CATENATE STRING: %s" % words

result = 5/2
print "USE \'/\': 5/2 = %d" % result

result = 5/2.0
print "USE \'/\' IN FLOAT: 5/2.0 = %f" % result

result = 5//2
print "USE \'//\': 5//2 = %d" % result

#result = 5//2.0
print "USE \'//\' IN FLOAT: 5//2.0 =", 5//2.0

print "USE while AND if:"

number = 15
running = True # upper case of first letter

while running:
guess = int(raw_input("Enter an integer: "))

if guess == number:
print "You got it"
running = False
elif guess > number:
print "It is higher"
else:
print "It is lower"
else:
print "while Loop over"

print "USE for:"
for i in range(1, 5):
print "i: ", i
else:
print "for Loop over"

print "USE function AND __doc__"
def printMax(x, y = 10):
"""
this function will compare x and
y then print the Max one
"""
if x > y :
print "x (%d)" % x
else:
print "y (%d)" % y
print printMax.__doc__
printMax(11)

print "USE sys module"
import sys

for item in sys.argv:
print item

print "PYTHONPATH is", sys.path, "\n"

print "sys.__name__:", sys.__name__
print "self.__name__:", __name__

print "DIR():", dir()

print "USE list:"
# the original list
mobile_phone_brands = ['apple', 'sumsung', 'nokia', 'htc', 'sony']
print mobile_phone_brands

""" if value in list?
1. if value in list
2. if list.__contains__(value)
"""
if mobile_phone_brands.__contains__('lenovo'):
print "del lenovo"
del mobile_phone_brands[mobile_phone_brands.index('lenovo')]
else:
print "add lenovo"
mobile_phone_brands = mobile_phone_brands + ['lenovo']
print mobile_phone_brands
""" delete in list
1. del list[index]
2. list.remove(value) # the first value
3. del list[start:last] # start to last -1 will be delete
"""

""" add in list
1. list + another_list
2. list.append(value)
3. list.insert(index, value) # will insert value before index
4. list[start: last] = another_list
5. list*number

"""

print "del sumsung to htc"
del mobile_phone_brands[1:4]
print mobile_phone_brands
print mobile_phone_brands.count('lenovo')
mobile_phone_brands[1:2] = ['sumsung', 'nokia', 'htc']
print mobile_phone_brands
mobile_phone_brands.reverse()
print mobile_phone_brands
for it in mobile_phone_brands.__reversed__():
print it

print mobile_phone_brands.index('htc')
mobile_phone_brands.insert(1, 'huawei')
print mobile_phone_brands
mobile_phone_brands.sort()
print mobile_phone_brands
mobile_phone_brands.sort(reverse = True)
print mobile_phone_brands
mobile_phone_brands.append('apple')
mobile_phone_brands.append('sumsung')
print mobile_phone_brands
del mobile_phone_brands[5]
mobile_phone_brands.remove('sumsung')
print mobile_phone_brands
print mobile_phone_brands*2

print "USE tuple:"
mobile_phone_brands = ('apple', 'sumsung', 'nokia')
print mobile_phone_brands*3
print "I like %s, hate %s, and miss %s" % mobile_phone_brands
print len(mobile_phone_brands)
mobile_phone_brands = ('sumsung', 'nokia')
apple = ('iphone', 'iphone3G', 'iphone3GS', 'iphone4')
mobile_phone_brands = mobile_phone_brands + apple
print mobile_phone_brands
mobile_phone_brands = ('sumsung', 'nokia', apple)
print "%s has retina" % mobile_phone_brands[2][3]

print "USE dict:"
brand_and_price = { 'S4': 5200, 'iphone4': 3088, 'lumia': 3200, 'note2': 4000, 'iphone5': 5200}
print brand_and_price
print brand_and_price.keys()
""" delete in dict
1. del x[key]
2. x.pop(key) -> value
3. x.popitem() -> <key, value>
4. x.clear()
"""
if brand_and_price.has_key('lumia'):
del brand_and_price['lumia']
print brand_and_price

brand_and_price['lumia'] = 3100
print brand_and_price
print brand_and_price.popitem()
print brand_and_price.pop('note2')
print brand_and_price
new_brand_and_price = brand_and_price.copy()
brand_and_price.clear()
print new_brand_and_price

print "USE SEQUENCE (list, tuple, string)"
mobile_phone_brands = ['apple', 'sumsung', 'nokia', 'htc', 'sony']
print mobile_phone_brands[0]
print mobile_phone_brands[-1]
print mobile_phone_brands[0:2]
print mobile_phone_brands[3:]
print mobile_phone_brands[1:-1]

print "REFERENCE AND COPY"
mobile_phone_brands = ['apple', 'sumsung', 'nokia', 'htc', 'sony']
tmp_1 = mobile_phone_brands
tmp_2 = mobile_phone_brands[:]
del mobile_phone_brands[1]
print mobile_phone_brands
print tmp_1
print tmp_2

print "USE CLASS"
class Person:
population = 0

def __init__(self, name):
''' Initialize a new person '''
self.name = name
Person.population += 1

def __del__(self):
Person.population -= 1
if Person.population == 0:
print "I'm the last one"
else:
print "%d person left" % Person.population

def sayHi(self):
print "My name is:", self.name

p = Person("ZhangHang")
p1 = Person("XiaoHong")
p2 = Person("XiaoZong")
print Person.__init__.__doc__
del p
del p1
del p2

class SchoolMember:
def __init__(self, name, age):
self.name = name
self.age = age

def tell(self):
print 'Name: %s, Age: %s' % (self.name, self.age)

class Teacher(SchoolMember):
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary

def tell(self):
SchoolMember.tell(self)
print "Salary: %d" % self.salary
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: