Bitwise操作:
<span style="font-size:18px;">print 5 >> 4 # Right Shift
print 5 << 1 # Left Shift
print 8 & 5 # Bitwise AND
print 9 | 4 # Bitwise OR
print 12 ^ 42 # Bitwise XOR
print ~88 # Bitwise NOT</span>
二进制数:
print 0b1, #1
print 0b10, #2
print 0b11, #3
print 0b100, #4
print 0b101, #5
print 0b110, #6
print 0b111 #7
print "******"
print 0b1 + 0b11
print 0b11 * 0b11
——bin()返回一个数字的二进制数;oct(),hex()返回8进制和16进制
——int("10",2)的第二个参数可以规定进制
——>>,<<右移,左移,相当于乘除2
&——按位与操作
|——按位或操作
^——XOR操作(异或)
pass
——不做任何事情,跳过Python认为应该有语句的地方
类:
class Animal(object):
def __init__(self,name):#self相当于c中的this,如果函数不接受任何参数,也需包含self
self.name=name
zebra =Animal("Jeffrey")
print zabra.name
class ShoppingCart(object):#必须包含object
"""Creates shopping cart objects
for users of our fine website."""
items_in_cart = {}
def __init__(self, customer_name):
self.customer_name = customer_name
def add_item(self, product, price):
"""Add product to the cart."""
if not product in self.items_in_cart:
self.items_in_cart[product] = price
print product + " added."
else:
print product + " is already in the cart."
def remove_item(self, product):
"""Remove product from the cart."""
if product in self.items_in_cart:
del self.items_in_cart[product]
print product + " removed."
else:
print product + " is not in the cart."
my_cart=ShoppingCart('Moon')
my_cart.add_item('min',0)
类的继承:
class Customer(object):
"""Produces objects that represent customers."""
def __init__(self, customer_id):
self.customer_id = customer_id
def display_cart(self):
print "I'm a string that stands in for the contents of your shopping cart!"
class ReturningCustomer(Customer):
"""For customers of the repeat variety."""
def display_order_history(self):
print "I'm a string that stands in for your order history!"
monty_python = ReturningCustomer("ID: 12345")
monty_python.display_cart()
monty_python.display_order_history()
类方法重写(略)
子类调用父类方法(无论重写与否)
class Derived(Base):
def m(self):
return super(Derived, self).m()
class Employee(object):
"""Models real-life employees!"""
def __init__(self, employee_name):
self.employee_name = employee_name
def calculate_wage(self, hours):
self.hours = hours
return hours * 20.00
# Add your code below!
class PartTimeEmployee(Employee):
def calculate_wage(self, hours):#此时也有<span style="font-family: Arial, Helvetica, sans-serif;">__init__()从父类继承,如果没有定义,系统会自己定义一个默认的</span>
self.hours = hours
return hours * 12.00
def full_time_wage (self, hours):
return super(PartTimeEmployee,self).calculate_wage(hours)
milton=PartTimeEmployee ("Min")#定义时候需要一个self关键词,类定义为你自动添加
print milton.full_time_wage (10)
class Point3D(object):
def __init__(self, x, y, z):
self.x=x
self.y=y
self.z=z
def __repr__(self):<span style="white-space:pre"> </span>#重新定义print格式
return "(%d, %d, %d)" % (self.x, self.y, self.z)
my_point=Point3D(1,2,3)
print my_point<span style="white-space:pre"> </span>#(1,2,3)
向文件输出:
my_list = [i**2 for i in range(1,11)]
# Generates a list of squares of the numbers 1 - 10
f = open("output.txt", "w")<span style="white-space:pre"> </span>#"w"是打开方式写,"r+"是读加写
for item in my_list:
f.write(str(item) + "\n")
f.close()
读操作:
my_file =open("output.txt",'r')
print my_file.read()
my_file.close()
.readline()——按行读取
# Open the file for reading
read_file = open("text.txt", "r")
# Use a second file handler to open the file for writing
write_file = open("text.txt", "w")
# Write to the file
write_file.write("Not closing files is VERY BAD.")
write_file.close()
# Try to read from the file
print read_file.read()
read_file.close()
with open("text.txt", "w") as textfile:
textfile.write("Success!")<span style="white-space:pre"> </span>#不需要close(),with。。as。。会做这些
文件有close成员变量来记录是否关闭,不同于close()函数