python 基础语法

1、hello world!

import this
print "hello world!"
View Code

2、判断语句 注意导入编码utf-8 否则打印中午报错 

#coding=utf-8

score = 90
if score>=80:
    print ("很好")
elif score>=60:
    print ("及格")
else:
    print("马马虎虎")

3、循环语句  输出0-100 所有自然数 但是不包括100 

for i in range(0,100):
    print (i)
View Code

注意python不支持“+”将字符串拼接 这种在java 中很常见的用法

for i in range(0,100):
    print ("Item {0},{1},{3}".format(i,"hehehe","xixi","enen"))

输出:Item 82,hehehe,enen

4、定义函数

def max(a,b):
    if a>b:
        return a
    else:
        return b

c=max(2,3)
print c

5、面向对象

class Dog:
    def bark(self):
        print("wangwangwang")


d = Dog()
d.bark()

5.1、对象的构造方法

class Dog:
    def __init__(self,name):
        self._name=name

    def bark(self):
        print("wangwangwang {0}".format(self._name))


d = Dog("haha")
d.bark()

5.2、继承

#coding=utf-8
class Dog:
    def __init__(self,name):
        self._name=name

    def bark(self):
        print("wang {0}".format(self._name))


class BabiDog(Dog):
    # def __init__(self,name):
    #     Dog.__init__(self,name)

    def bark(self):
        print("{0} xixixix".format(self._name))

babi = BabiDog("babi")
babi.bark()

6、引入外部包


#coding=utf-8

import mylib  #此种方式 需要有包类全名

x=mylib.Hi()
x.sayHello()

from mylib import Hi  #此种相对简单
y=Hi()
y.sayHello()

 

posted @ 2016-12-19 22:08  pikzas  阅读(135)  评论(0编辑  收藏  举报