python基础语法

来源:互联网 发布:单片机设计实例 编辑:程序博客网 时间:2024/05/19 02:30
python基础语法小汇总
test.py
# coding utf-8#1. 打印常量print("1. 打印常量") print("hello python") #2. 定义变量print("2. 定义变量") a = 10b = 20c=a+bprint("c = {0}".format(c))#3. 条件判断print("3. 条件判断") score = 65if score>=80 :    print("good")elif score>=60 :    print("及格")    print ("马马虎虎")elif score>=40 :    print("not good")else :    print("bad")    #4. 循环print("4. 条件判断")    for i in range(0,5):    print(i)    #5. 定义函数print("5. 定义函数")     def hello():    print("hello wlh")hello()#6.面向对象print("6. 面向对象 class")     class test :    def hi() :        print("print from test hi()")        hello()        test.hi()#7.引入python文件print("7.引入python文件")   import otherclass1 = other.otherclass()class1.display()

other.py

#另一个文件class otherclass:    def display(self):        print("open the other file")#otherclass.display()

输出结果:

1. 打印常量
hello python
2. 定义变量
c = 30
3. 条件判断
及格
马马虎虎
4. 条件判断
0
1
2
3
4
5. 定义函数
hello wlh
6. 面向对象 class
print from test hi()
hello wlh
7.引入python文件
open the other file