Skip to content

Commit a000f38

Browse files
authored
Feat: Add i18n (#17)
* Add i18n and en docs * Translate zh-CN chars to en * Update readme
1 parent 942b069 commit a000f38

File tree

99 files changed

+6414
-60
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+6414
-60
lines changed

README-zh_CN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div style="text-align: center;">
2+
<a href="./README.md">English</a> | 简体中文
3+
<p>一本基于 <a href="https://github.com/luckrnx09/abook">abook</a> 编写的开源电子书</p>
4+
</div>
5+
6+
7+
<div style="text-align:center; margin: 2em 0;">
8+
<img src="./static/img/book_cover_zh-cn.png" style="width: 80%;" />
9+
</div>
10+
11+
12+
《JavaScript 工程师的 Python 指南》是一本开源电子书,涵盖了从 Python 环境安装到项目开发的方方面面。本书通过案例对比了 JavaScript 和 Python 语言的异同,帮助 JavaScript 工程师快速掌握 Python 语言。
13+
14+
<div style="text-align: center; margin-bottom:2em;">
15+
<a href="https://luckrnx09.github.io/python-guide-for-javascript-engineers/zh-cn">在线阅读 🚀</a>
16+
</div>
17+
18+
欢迎提交 PR 对本书的内容进行补充、修正以及翻译。

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1-
# python-guide-for-javascript-engineers
1+
<div style="text-align: center;">
2+
English | <a href="./README-zh_CN.md">简体中文</a>
3+
<p>An open-source ebook powered by <a href="https://github.com/luckrnx09/abook">abook</a></p>
4+
</div>
5+
6+
7+
<div style="text-align:center; margin: 2em 0;">
8+
<img src="./static/img/book_cover.png" style="width: 80%;" />
9+
</div>
10+
11+
12+
The Python Guide for JavaScript Engineers is an open source ebook that covers everything from Python environment installation to project development. This book compares the similarities and differences between JavaScript and Python through cases to help JavaScript engineers quickly master the Python language.
13+
14+
<div style="text-align: center; margin-bottom: 2em;">
15+
<a href="https://luckrnx09.github.io/python-guide-for-javascript-engineers">Read it online 🚀</a>
16+
</div>
17+
18+
Welcome to submit PR to supplement, revise and translate the content of this book.

docs/about-the-book.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
id: about-the-book
3+
slug: /about-the-book
4+
title: About the Book
5+
sidebar_label: About the Book
6+
---
7+
8+
Hello, I'm luckrnx09, a frontend engineer who makes a living with React. I'm glad to introduce to you my first open-source e-book, "Python Guide for JavaScript Engineers".
9+
10+
The content of this book is completely free and open-source. You can find the repository here: https://github.com/luckrnx09/python-guide-for-javascript-engineers
11+
12+
### Why Write This Book
13+
14+
In 2022, ChatGPT made a huge splash, ushering in a new era of artificial intelligence. Python once again became the most popular programming language in the field of AI. Many AI-related tools have emerged rapidly, with new ones appearing every day. Python's simplicity and its dominance over the years have made it the preferred language for many popular AI projects. Therefore, learning Python has become a ticket to enter these projects. It is an unstoppable trend in the field of AI.
15+
16+
Recently, I finally took the time to systematically learn Python. However, as someone who already mastered C# and JavaScript, the learning process was not easy for me due to the following reasons:
17+
- I was unsure about the differences between Python and the languages I already knew. To avoid missing important knowledge, I had to study every detail instead of just skimming the surface.
18+
- I was unsure about the similarities between Python and the languages I already knew. As a result, I wasted a lot of time relearning programming concepts that I had already fully grasped.
19+
20+
Due to these two uncertainties, I put in a tremendous amount of effort and persevered to complete my systematic learning. After finishing my studies, I explained key concepts in Python to my frontend developer colleagues by combining and comparing the syntax and features of Python and JavaScript. I found that they were able to quickly grasp Python development just like I did.
21+
22+
Therefore, I decided to organize the key points I encountered during the learning process into a more detailed book. I hope it can help more JavaScript engineers avoid common pitfalls and obstacles when learning Python.
23+
24+
### Key Features of This Book
25+
26+
To improve the learning efficiency, this book extensively uses examples and a comparative teaching approach. I first demonstrate how to achieve a requirement using JavaScript and then reimplement it using Python code. Through this comparison, you will gain an intuitive understanding of the differences between the two languages.
27+
28+
At the end of each example, I also present a table showing equivalent APIs in JavaScript and Python. These APIs may not appear in the example code but are commonly used in development. While it is not necessary to master all of them immediately, knowing about their existence in advance can be very helpful for writing simple and efficient Python code.
29+
30+
### Target Audience
31+
32+
This book is only suitable for JavaScript developers, especially those who are familiar with the latest ES standards and Node.js.
33+
34+
### How This Book Was Written
35+
36+
My open-source project abook https://github.com/luckrnx09/abook, is an AI book-writing tool based on ChatGPT. The core idea is the "examples + comparative teaching" approach mentioned above. The initial draft of this book was generated using that tool. I then spent several months proofreading, adjusting, translating, and polishing the draft to present the final version to you.
37+
38+
### Errata
39+
40+
Due to my limited knowledge, errors may occur during the writing process. If you discover any mistakes in the book, I welcome you to submit a pull request through the "Edit this page" link at the bottom left of the article to help improve the content of this book.
41+
42+
<div align="right">
43+
<span> — </span>
44+
<span>luckrnx09</span>
45+
</div>

docs/afterword.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
id: afterword
3+
slug: /afterword
4+
title: Afterword
5+
sidebar_label: Afterword
6+
---
7+
8+
Unknowingly, you have completed the entire content of this book.
9+
10+
The end of this book is not the end of the Python learning journey, but a new beginning. The Python ecosystem is extremely prosperous. Due to limited space, many interesting topics were not mentioned in this book. With the blessing of AI, I believe you will no longer be afraid of reading and writing Python code.
11+
12+
Finally, thank you again for choosing to read this book. The initial draft of this book was generated based on my open source project abok - https://github.com/luckrnx09/abook. If this book has been helpful to you, I hope you can give this project a Star, which will encourage me to create more high-quality content.
13+
14+
I hope this is a good experience for you in your journey of learning Python!
15+
16+
<div align="right">
17+
<span> —— luckrnx09</span>
18+
</div>

docs/anonymous-functions.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
id: anonymous-functions
3+
slug: /anonymous-functions
4+
title: Anonymous Functions
5+
sidebar_label: Anonymous Functions
6+
---
7+
8+
When you need to inline execute a small piece of code without defining a separate function, you can use anonymous functions. In Python, anonymous functions are called "lambda expressions" or "lambda functions".
9+
10+
### Task
11+
12+
Using anonymous functions, map each item in a list of numbers to its square and print the new list.
13+
14+
#### JavaScript implementation
15+
16+
```javascript
17+
const numbers = [1, 2, 3, 4, 5];
18+
19+
const squaredNumbers = numbers.map(number => number ** 2);
20+
console.log(squaredNumbers);
21+
```
22+
23+
#### Python implementation
24+
25+
```python
26+
numbers = [1, 2, 3, 4, 5]
27+
28+
squared_numbers = list(map(lambda number: number ** 2, numbers))
29+
print(squared_numbers)
30+
```
31+
32+
### Code Highlight
33+
- Python uses the `lambda` keyword to create anonymous functions, while JavaScript uses `function` or `=>` to create anonymous functions.
34+
- Python's `lambda` functions cannot contain a function body, and the value of the expression is automatically returned as the return value. In contrast, anonymous functions in JavaScript can have function bodies and `return` statements.
35+
36+
### Difference Quick View
37+
38+
| Feature | JavaScript | Python |
39+
|---------|---------------------------------|--------------|
40+
| Creating Anonymous Functions | `(params) => expression` | `lambda params: expression` |
41+
42+
:::tip
43+
When a lambda function in Python has multiple parameters, separate each parameter with a `,`.
44+
:::
45+
46+
### Resources
47+
48+
- https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions

docs/arithmetic-operation.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
id: arithmetic-operation
3+
slug: /arithmetic-operation
4+
title: Arithmetic Operations with Basic Data Types
5+
sidebar_label: Arithmetic Operations
6+
---
7+
8+
Arithmetic operators in Python are used to perform basic arithmetic operations such as addition, subtraction, multiplication, and division.
9+
10+
### Task
11+
12+
Create a program that defines two variables of number data types and performs various arithmetic operations on them. The program should perform the following tasks:
13+
14+
1. Add the two numbers and print the sum.
15+
2. Subtract the first number from the second number and print the difference.
16+
3. Multiply the two numbers and print the product.
17+
4. Divide the first number by the second number and print the quotient.
18+
5. Calculate the remainder when the first number is divided by the second number and print the result.
19+
6. Raise the first number to the power of the second number and print the result.
20+
7. Increment the value of the first number by 1 and print the updated value.
21+
8. Decrement the value of the second number by 1 and print the updated value.
22+
23+
#### JavaScript implementation
24+
```javascript
25+
let num1 = 10;
26+
let num2 = 5;
27+
28+
// 1. Addition
29+
let sum = num1 + num2;
30+
console.log("Sum:", sum);
31+
32+
// 2. Subtraction
33+
let difference = num1 - num2;
34+
console.log("Difference:", difference);
35+
36+
// 3. Multiplication
37+
let product = num1 * num2;
38+
console.log("Product:", product);
39+
40+
// 4. Division
41+
let quotient = num1 / num2;
42+
console.log("Quotient:", quotient);
43+
44+
// 5. Remainder
45+
let remainder = num1 % num2;
46+
console.log("Remainder:", remainder);
47+
48+
// 6. Exponentiation
49+
let power = num1 ** num2;
50+
console.log("Power:", power);
51+
52+
// 7. Increment
53+
num1++;
54+
console.log("Updated num1 after increment:", num1);
55+
56+
// 8. Decrement
57+
num2--;
58+
console.log("Updated num2 after decrement:", num2);
59+
```
60+
61+
#### Python implementation
62+
```python
63+
num1 = 10
64+
num2 = 5
65+
66+
# 1. Addition
67+
sum = num1 + num2
68+
print("Sum:", sum)
69+
70+
# 2. Subtraction
71+
difference = num1 - num2
72+
print("Difference:", difference)
73+
74+
# 3. Multiplication
75+
product = num1 * num2
76+
print("Product:", product)
77+
78+
# 4. Division
79+
quotient = num1 / num2
80+
print("Quotient:", quotient)
81+
82+
# 5. Remainder
83+
remainder = num1 % num2
84+
print("Remainder:", remainder)
85+
86+
# 6. Exponentiation
87+
power = num1 ** num2
88+
print("Power:", power)
89+
90+
# 7. Increment
91+
num1 += 1
92+
print("Updated num1 after increment:", num1)
93+
94+
# 8. Decrement
95+
num2 -= 1
96+
print("Updated num2 after decrement:", num2)
97+
```
98+
99+
### Code Highlight
100+
- The syntax for addition, subtraction, multiplication, division, remainder, and exponentiation in Python is the same as in JavaScript.
101+
- Python does not have the `++` and `--` operators. Instead, `+=` and `-=` are used to increment or decrement variables.
102+
103+
### Difference Quick View
104+
105+
| Feature | JavaScript | Python |
106+
|---------|------------|--------|
107+
| Addition | `+` | `+` |
108+
| Subtraction | `-` | `-` |
109+
| Multiplication | `*` | `*` |
110+
| Division | `/` | `/` |
111+
| Integer Division | - | `//` |
112+
| Remainder | `%` | `%` |
113+
| Exponentiation | `**` | `**` |
114+
| Increment | `++` <br /> `+=1` | `+= 1` |
115+
| Decrement | `--` <br/> `+=1` | `-= 1` |
116+
117+
:::danger
118+
In Python, arithmetic operations can only be performed between **compatible** data types. For example, the result of a calculation between a floating-point number and an integer will be a floating-point number. However, performing arithmetic operations between a string and a number will result in an error. To perform arithmetic operations between a number and a string, one of them must be converted to the appropriate data type.
119+
:::
120+
121+
### Resources
122+
123+
- https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex

docs/basic-types.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
id: basic-types
3+
slug: /basic-types
4+
title: Basic Data Types
5+
sidebar_label: Basic Data Types
6+
---
7+
8+
Python basic data types include:
9+
10+
- Integer: Numbers without decimal points.
11+
- Float: Numbers with decimal points.
12+
- String: A sequence of characters enclosed in single quotes or double quotes.
13+
- Boolean: Represents True or False.
14+
- None: Represents empty value.
15+
16+
### Task
17+
18+
Define 5 variables to represent a person's payslip, including: name, month, salary, whether they have perfect attendance, and project team.
19+
20+
#### JavaScript implementation
21+
22+
```javascript
23+
let name = 'Bob' // string
24+
let month = 12 // number
25+
let salary = 10000.12 // number
26+
let isPerfectAttendance = true // boolean
27+
let team = null // null
28+
console.log(typeof name) // Get the type of the variable
29+
```
30+
31+
#### Python implementation
32+
33+
```python
34+
name = 'Bob' # str
35+
month = 12 # int
36+
salary = 10000.12 # float
37+
is_perfect_attendance = True # bool
38+
team = None # None
39+
print(type(name)) # Get the type of the variable
40+
```
41+
42+
### Code Highlight
43+
- Python has two types of number types, `int` for integers and `float` for floating-point numbers, while JavaScript uses the `number` type for both integers and floating-point numbers.
44+
- Python uses `None` to represent empty, while JavaScript uses `null`.
45+
- Python does not have a type similar to JavaScript's `undefined`.
46+
- Python uses the built-in function `type()` to get the variable type, while JavaScript uses the `typeof` keyword.
47+
48+
### Difference Quick View
49+
| Type | JavaScript | Python |
50+
|---------|------------|--------|
51+
| Integer | `let myInt = 10;`<br />`let myInt = Number(10);` | `my_int = 10` |
52+
| Float | `let myFloat = 3.14;`<br />`let myFloat = Number(3.14);` | `my_float = 3.14` |
53+
| String | `let myStr = "Hello World";` | `my_str = "Hello World"` |
54+
| Boolean | `let myBool = true;`<br />`let myBool = false;` | `my_bool = True`<br />`bool_val = False` |
55+
| None | `let myNull = null;` | `my_none = None` |
56+
57+
String is one of the most widely used data types in programming languages, mastering them can greatly simplify programming. Here are the common string APIs in JavaScript and Python:
58+
59+
| Method | JavaScript | Python |
60+
|--------------|--------------------------------|----------------------------------|
61+
| Get length | `myStr.length` | `len(my_str)` |
62+
| Concatenate | `myStr1 + myStr2` | `my_str1 + my_str2` |
63+
| Slice | `myStr.slice(start, end)` | `my_str[start:end]` |
64+
| Find | `myStr.indexOf(substring)` | `my_str.index(substring)` |
65+
| Replace | `myStr.replace(old, new)` | `my_str.replace(old, new)` |
66+
| Split | `myStr.split(separator)` | `my_str.split(separator)` |
67+
| Convert to uppercase and lowercase | `myStr.toUpperCase()`<br />`myStr.toLowerCase()` | `my_str.upper()`<br />`my_str.lower()` |
68+
| Reverse | `myStr.split('').reverse().join('')` | `my_str[::-1]` |
69+
| Count occurrences of a substring | `myStr.split(substring).length - 1` | `my_str.count(substring)` |
70+
| Check if a string starts with a specified character(s) | `myStr.startsWith(prefix)` | `my_str.startswith(prefix)` |
71+
| Check if a string ends with a specified character(s) | `myStr.endsWith(suffix)` | `my_str.endswith(suffix)` |
72+
| Trim whitespace from both ends of a string | `myStr.trim()` | `my_str.strip()` |
73+
| Center align a string with a specified width | - | `my_str.center(width)` |
74+
| Capitalize the first letter of the first word | - | `my_str.capitalize()` |
75+
| Capitalize the first letter of each word | - | `my_str.title()` |
76+
77+
### Resources
78+
- https://docs.python.org/3/library/stdtypes.html

0 commit comments

Comments
 (0)