python语法
2015-10-29 16:18
519 查看
if x < 0: x = 0 print('Negative changed to zero') elif x == 0: print('Zero') elif x == 1: print('Single') else: print('More') for w in words: print(w, len(w)) for i in range(5): print(i) break and continue pass def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.""" a, b = 0, 1 while a < n: print(a, end=' ') a, b = b, a+b print() import fibo from fibo import fib, fib2 from fibo import * import sound.effects.echo from sound.effects import echo from sound.effects.echo import echofilter while True: try: x = int(input("Please enter a number: ")) break except ValueError: print("Oops! That was no valid number. Try again") try: f = open('myfile.txt') s = f.readline() i = int(s.strip()) except OSError as err: print("OS error: {0}".format(err)) except ValueError: print("Could not convert data to an integer.") except: print("Unexpected error:", sys.exc_info()[0]) raise for arg in sys.argv[1:]: try: f = open(arg, 'r') except IOError: print('cannot open', arg) else: print(arg, 'has', len(f.readlines()), 'lines') f.close() raise NameError('HiThere') class MyError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) try: raise KeyboardInterrupt finally: print('Goodbye, world!') with open("myfile.txt") as f: for line in f: print(line, end="") class ClassName: <statement-1> . . . <statement-N> class MyClass: """A simple example class""" i = 12345 def f(self): return 'hello world' def __init__(self): self.data = [] class DerivedClassName(BaseClassName): <statement-1> . . . <statement-N> class DerivedClassName(Base1, Base2, Base3): <statement-1> . . . <statement-N> class Mapping: def __init__(self, iterable): self.items_list = [] self.__update(iterable) def update(self, iterable): for item in iterable: self.items_list.append(item) __update = update # private copy of original update() method class MappingSubclass(Mapping): def update(self, keys, values): # provides new signature for update() # but does not break __init__() for item in zip(keys, values): self.items_list.append(item) class Employee: pass john = Employee() # Create an empty employee record # Fill the fields of the record john.name = 'John Doe' john.dept = 'computer lab' john.salary = 1000
相关文章推荐
- Panedwindow 实例
- Python 机器学习——解决过拟合的方法
- Python Paramiko模块安装和使用
- Python Paramiko模块安装和使用
- 分割线实例
- Python性能鸡汤
- (6)python语句嵌套
- 小小Python爬虫(0)
- Python循环语句之while循环22
- Windows下Python环境搭建(包括Win7 64位下安装setuptools)
- 如何统计Python代码行数
- 如何统计Python代码行数
- Windows下python安装问题解决方案
- Python 中的input(),raw_input()与zip()
- python如何定义不定参数函数
- Python 中的join()与split()
- 性能测试培训: 监控CPU之python
- python 升级导致yum无法正常使用
- 基于Python实现自动慢查询分析,邮件自动发送
- 基于Python实现自动慢查询分析,邮件自动发送