python基础语法小汇总
test.py
# coding utf-8
#1. 打印常量
print("1. 打印常量")
print("hello python")
#2. 定义变量
print("2. 定义变量")
a = 10
b = 20
c=a+b
print("c = {0}".format(c))
#3. 条件判断
print("3. 条件判断")
score = 65
if 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 other
class1 = 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