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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: