前言
“如何摆脱不停切图的困局?”
这不是一篇制造焦虑的文章,而是充满真诚建议的Python
推广文。
当谈论到编程入门语言时,大多数都会推荐Python
和JavaScript
。
实际上,两种语言在方方面面都非常强大。
而如今我们熟知的ES6
语言,很多语法都是借鉴Python
的。
有一种说法是 “能用js实现的,最后一定都会用js实现。”
那么这里可以说:“能跟python
长得像的,最后一定会像python
。”
**注:*有需要Python安装包和Pycharm激活码可以在评论区留言.
*
1. Python
和ES6
语法差别
1. 基本类型
值得注意的是,尽管两者都是动态类型,但python
连接时并不会自动转换类型。
// JavaScript
let coerced = 1;
let concatenated = coerced + 'string';
// Python
not_coerced = 1
concatenated = not_coerced + 'string'
直接报错:TypeError: cannot concatenate 'str' and 'int' objects
只有提前把num
转换为字符串类型才能正确运行
# Python
not_coerced = 1
concatenated = str(not_coerced) + 'string'
2. Functions
ormethods
?
在JavaScript
和Python
中,函数和条件的结构极为相似。例如:
// JavaScript
function drSeuss(catInTheHat, thing1, thing2) {
if (catInTheHat == true &&
thing1 == true &&
thing2 == true) {
console.log('is cray');
} else if (catInTheHat != true) {
console.log('boring');
} else {
console.log('so boring');
}
}
# Python
def dr_seuss(cat_in_the_hat, thing1, thing2):
if cat_in_the_hat == True and
thing2 == True and
thing2 == True:
print 'is cray'
elif cat_in_the_hat != True:
print 'boring'
else:
print 'so boring'
但在JavaScript
中,“methods
”的通俗定义是指语言规范中内置的方法,例如:Function.prototype.apply()
。
在MDN
上有对二者的解释:
在大多数方面,Functions</