整理时间:2020-02-19
内容来源:B站视频-北大陈斌老师
内容大纲:通过几个例子帮助大家对比两者语言的不同点,理解如何转化
从hello world 开始
高斯求和
检验素数
打印一个朴素的三角形
1.hello world
C语言
#include
int main()
{
// say hello world
printf(“hello world!\n”);
}
Python语言
代码1(带有C语言口音的Python代码)
def main(): #这里要以main来命名,这是和C的不同
# say hello world
print(‘hello world’)
main()
代码2(正统Python 表达)
print(‘hello world’)
对比说明:
(1)C语言是的代码运行过程是这样的,首先是Compile编译成机器码,然后Link 各种库链接,最后Excute 执行目标程序。
(2)为什么C代码第一句是“#include ”,因为后边的printf要用到这个库。
(3)Python是一种解释性语言,点击运行就直接运行了,不像是C语言需要编译,连接,执行这三步骤。
(4)C语言的格式要求不严格,你可以不考虑缩进与分行等;Python需要严格的格式,一定要注意缩进,还有冒号!!
(5)