您的位置:首页 > 编程语言 > Python开发

es6 和 python 语法比较

2017-12-31 16:06 525 查看
http://www.linchaoqun.com/html/cms/content.jsp?id=1509528630774  Python3笔记:Python与ECMAScript部分语法对比
https://frankfang.github.io/es-6-tutorials/  ES 6 新特性列表
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference  官方的javascript参考文档
python:支持面向对象和函数式编程的多范式编程语言。

es6:是一门面向原型的编程语言

最近再看了一遍es6,发现部分语法有点接近python,js再不是当年那个仓促诞生、简陋甚至丑陋的动态弱类型语言了。

hello world

简单查看和遍历

作用域、赋值

解构赋值

lambda

文本文件

遍历

迭代器和生成器

字符串

集合

hello world:

py:

print("hello")

python hello.py  #运行python程序

python -V   #查看版本 python 3.6.3  https://www.python.org/downloads/  去官网下载安装python(升级python版本时,要先卸载旧的,再重新安装新版本)

pip install bs4   # 安装第三方包

import bs4   # 代码中导入第三方包

ES6:

console.log("hello");

node hello.js  // 运行

node -v   //查看版本 v9.3.0    https://nodejs.org/dist/  下载node-v9.3.0-x64.msi。(升级node版本时,到官网下载新版本,直接安装即可,无须卸载旧版。)

npm install vue  //安装第三方包  npm install vue -g // 全局安装(如果npm受限,就用cnpm。自己搜索)

var http = require("http");  // 导入第三方包

import { stat, exists, readFile } from 'fs';  //导入第三方包

简单查看和遍历:

py:

a1 = "helloworld"            # 字符串
a1 = [1, 2, 3, 4, 55, 6]     # 列表
a1 = ("aa", "bb")            # 元组
print("类型:",type(a1))  # 查看类型
print("长度:",len(a1))   # 查看长度
for i in a1:    # 遍历
print(i)

dict1 = {'name': 'pp', 'age': 20, "gender": "man"}    # 字典
for key, val in dict1.items():    # 遍历字典
print(key, "--", val)


es6:

var a1 = "helloworld";              //字符串
var a1 = [1, 2, 3, 4, 55, 6];       //数组
console.log("类型:" +(typeof a1));   // 查看类型
console.log("长度:" + a1.length);    // 查看长度
for (let i=0;i<a1.length;i++){       // 遍历
console.log(a1[i]);
}

let dict1 = new Map([["a","aa1"], ['b', 'bb2'], ['c', 'ccc']]);    //字典
dict1.forEach(function (value, key) {       // 遍历字典
console.log(key,value);
})
for (let [key,value] of dict1) {            // 这样遍历也可以
console.log(key,value);
}


作用域、赋值:

py:  http://www.runoob.com/python3/python3-function.html

函数内是局部变量,函数外是全局变量。  当内部作用域想修改外部作用域的变量时,就要用到global和nonlocal关键字

'''  iterator, generator。   以下是些基本概念:
iterator:它是这么一个对象,拥有一个next方法,这个方法返回一个对象{done,value},这个对象包含两个属性,一个布尔类型的done和包含任意值的value
iterable: 这是这么一个对象,拥有一个obj[@@iterator]方法,这个方法返回一个iterator
generator: 它是一种特殊的iterator。反的next方法可以接收一个参数并且返回值取决与它的构造函数(generator function)。generator同时拥有一个throw方法
generator 函数: 即generator的构造函数。此函数内可以使用yield关键字。在yield出现的地方可以通过generator的next或throw方法向外界传递值。generator 函数是通过function*来声明的
yield 关键字:它可以暂停函数的执行,随后可以再进进入函数继续执行 '''


基本概念

字符串:

py:

# 字符串重复n遍
print("hello"*3)       #hellohellohello

# 替换
str1 = "123aaa321abc".replace("a", "z")    # 替换全部   123zzz321zbc


ES6:

//字符串重复n遍
console.log( 'hello'.repeat(3) ); // "hellohellohello"

//替换
let str1="123aaa321abc";
let str2 = str1.replace('a', 'z');//普通的只能替换掉一个 123zaa321abc
let str3 = str1.replace(/a/g, 'z');//正则可以替换全部 123zzz321zbc(这是js比较坑的地方,必须用正则,才能实现全部替换)


集合:

py:

a = set(["aa", "bb", "cc", "cc"])  # 集合的项是不重复的,加入重复的也没用。集合是无序的。
a.add("dd")  # 集合add方法
a.remove("bb")  # 集合删除方法
print("cc 在集合里吗? ", "cc" in a)  # 判断是否在集合里    True
print(a)  # {'cc', 'aa', 'dd'}

for item in a:          # SET集合的遍历      cc aa dd
print(item, end=" ")
for i in enumerate(a):      # (0, 'dd') (1, 'cc') (2, 'aa')
print(i, end=" ")


a = set("abcde")
b = set("defg")
print(a & b)  # 交集         {'e', 'd'}
print(a | b)  # 合集         {'b', 'e', 'c', 'd', 'a', 'f', 'g'}
print(a - b)  # 相对补集、差集     {'a', 'b', 'c'}
print(a - b)  # 相对补集、差集      {'g', 'f'}


ES6:

a = new Set(["aa", "bb", "cc", "cc"])  // 集合的项是不重复的,加入重复的也没用。集合是无序的。
a.add("dd")  // 集合add方法
a.delete("bb")  // 集合删除方法
console.log("cc 在集合里吗? ", a.has('cc'))  // 判断是否在集合里    True
console.log("集合的长度? ", a.size)  // 集合的长度
console.log(a)  // {'cc', 'aa', 'dd'}
for (let x of a) { // SET集合的遍历
console.log(x);
}


let a = new Set(["a","b","c","d","e"]);
let b = new Set(["d","e","f","g"]);
let unionSet = new Set([...a, ...b]);                  // 并集  {"a", "b", "c", "d", "e","f","g"}
let intersectionSet = new Set([...a].filter(x => b.has(x)));    // 交集{"d", "e"}
let differenceABSet = new Set([...a].filter(x => !b.has(x)));   // 差集 {"a", "b", "c"}


...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: