惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CERT Recently Published Vulnerability Notes
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Tor Project blog
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Latest news
Latest news
L
LINUX DO - 热门话题
罗磊的独立博客
T
Tenable Blog
The Hacker News
The Hacker News
美团技术团队
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
博客园 - 司徒正美
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
云风的 BLOG
云风的 BLOG
S
Secure Thoughts
Cloudbric
Cloudbric
S
Security @ Cisco Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
U
Unit 42
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
Webroot Blog
Webroot Blog
爱范儿
爱范儿
博客园 - 【当耐特】
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
P
Palo Alto Networks Blog
Google Online Security Blog
Google Online Security Blog
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
F
Full Disclosure
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Security Affairs
Project Zero
Project Zero

卢贤泼的博客

漫长的告别 04: 答案在别处 | 卢贤泼的博客 漫长的告别 03: 人生修炼 | 卢贤泼的博客 漫长的告别 02: 赢过昨天,输给明天 | 卢贤泼的博客 漫长的告别 01: 不期而遇 | 卢贤泼的博客 漫长的告别 00: 创刊号 | 卢贤泼的博客 WebAssembly With Rust | 卢贤泼的博客 2020 这一年 | 卢贤泼的博客 热血合唱团 | 卢贤泼的博客 我的 macOS 常用工具和配置 | 卢贤泼的博客 《The Social Dilemma》智能陷阱 | 卢贤泼的博客 《目光》:向光而行 | 卢贤泼的博客 1024 的周末 | 卢贤泼的博客 纪录片《人生果实》 | 卢贤泼的博客 读书笔记:小狗钱钱 | 卢贤泼的博客 国庆假期的一些小事 | 卢贤泼的博客 这个博客背后的技术细节 | 卢贤泼的博客 多重身份 | 卢贤泼的博客 一往无前 | 卢贤泼的博客 2019 这一年 | 卢贤泼的博客 VueConf 2019 | 卢贤泼的博客 2018 这一年 | 卢贤泼的博客 伙伴,路途上的星光 | 卢贤泼的博客 《黑客与画家》--- 一个程序员的自我修养 | 卢贤泼的博客
Python for JavaScript Developer | 卢贤泼的博客
alanlupublic@gmail.com (卢贤泼) · 2020-05-30 · via 卢贤泼的博客

我的日常主要是写 JavaScript,这篇文章的原始内容来自于我读 《Python 编程:从入门到实践》的读书笔记。分享从 JavaScript 开发者的角度出发来学习 Python 这门语言。在 JS 的基础上学习一门的新的语言,会降低非常多的入门成本。

语法上的差异

想要实际感受一下代码的运行效果可以在 https://repl.it/languages/python3 这个上面尝试

  1. 不在需要写 let、var 之类的声明,同时没有自动的类型转换
age = 23
message = "Happy "+str(age)+"rd Birthday!"
print(message)
  1. 注释使用 #
# print hello world
print("hello world")
  1. 数组操作 push 使用 append,插入使用 insert,删除使用 del,取出元素 pop,根据值删除元素 remove
list = ['a', 'b']
list.append('c')
list.insert(4, 'd')
del list[0]
lastListItem = list.pop() # 取出最后一个
firstListItem = list.pop(0) # 取出任意位置的元素
list.remove('b')  # 根据值来删除
  1. 循环使用 for ... in
list = ['a', 'b', 'c', 'd']

for item in list:
    print(item)
  1. 数值列表 range
  2. python 里面数组中间的获取,使用切片
  3. 元祖是一个只能整个变的变量
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
    print(dimension)

dimensions = (400, 100)
print("Modified dimensions:")
for dimension in dimensions:
    print(dimension)
  1. & -> and,|| -> or
a = 1
b = 2

print(a >= 1 and b >= 3) # False
print(a >= 1 or b >= 3) # True
  1. 布尔值首字母大写,isOk = True, isOk= False
  2. else if 简化为 elif
  3. 访问对象内的值统一使用 dic['a'] 的形式,同时对象的 key 也要带上单引号,dicTest = {'a': 'aValue'}
  4. 删除对象使用 del dicTest['a']
  5. 遍历使用: for key, value in dicTest.items()
  6. Object.keys(dicTest) -> dicTest.keys(),Object.values(dicTest) -> dicTest.values()
  7. 函数通过 def 声明

    函数整体比较复杂,更加详细的部分大家可以看:Python 编程:从入门到实践 · 函数

def greet_user():
    # 特殊的注释
    """显示简单的问候语"""
    print("Hello!")
greet_user()
  1. 类的使用
class Dog():
    """一次模拟小狗的简单尝试"""
    def __init__(self, name, age):  # __init__ 类比 JS 中的 constructor
        """初始化属性name和age"""
        self.name = name
        self.age = age
    def sit(self):
        """模拟小狗被命令时蹲下"""
        print(self.name.title()+" is now sitting.")
    def roll_over(self):
        """模拟小狗被命令时打滚"""
        print(self.name.title()+" rolled over!")

myDog = Dog('dogName', 16)  # 不需要 new

# 调用和 JS 基本相同
print(myDog.name)
myDog.sit()


# 类的继承
class SuperDog(Dog):
    def __init__(self, name, age):
        super().__init__(name, age)
        self.isSuper = True

mySuperDog = SuperDog('super', 1)
print(mySuperDog.isSuper)
mySuperDog.sit()
  1. 类的导入

和 JS 的 ESModule 语法类似

# dog.py 内容和上面的 Dog 类相同

# main.py

# 单个导入
from dog import Dog, SuperDog


# 整个导入
import dog
myDog = dog.Dog('name', 10)

依赖管理

Pyhone 使用 pip:https://pip.pypa.io/en/stable/

后记

以上是在阅读 《Python 编程:从入门到实践》 的一些关键点的摘录,还是很推荐可以阅读一下这本书的原文。Pyhone 其实没有像中的那么难,有很多还是和 JS 很像的,在 JS 的基础上可以很快上手。

最后分享非常喜欢书中献言部分,很暖:

谨以此书献给我的父亲,以及儿子 Ever。感谢父亲抽出时间来回答我提出的每个编程问题,而儿子 Ever 也开始向我提问了。

#tech