File tree Expand file tree Collapse file tree 1 file changed +39
-39
lines changed Expand file tree Collapse file tree 1 file changed +39
-39
lines changed Original file line number Diff line number Diff line change 100
100
2. 请尝试编写脚本文件使其能正确输出 1. 中的欢迎信息。并思考,C++ 中你是怎样实现对于不同的用户,可以呈现不同的欢迎信息的呢?
101
101
102
102
提示:
103
-
103
+
104
104
- 你可以通过仔细观察上面的图片来解决问题
105
105
- 接下来我们将讲解变量的定义与程序的输入输出
106
106
145
145
与其相对应的是**动态类型语言**:变量并没有确定的数据类型,这意味着变量的类型可以在运行期改变。
146
146
147
147
比如,在 C++ 中:
148
-
148
+
149
149
```cpp
150
150
int c = 1;
151
151
char m = 'x';
154
154
我们不能尝试使用 `a = 1`,因为我们已经规定了 `a` 是 `std::string` 类型。
155
155
156
156
而在 Python 中,我们可以尝试:
157
-
157
+
158
158
```python
159
159
a = 123
160
160
a = 100.0
@@ -168,41 +168,41 @@ c7w
168
168
!!! note "Python 是一种强类型语言"
169
169
170
170
注意区分“强/弱类型”与“静态/动态类型”,这是一对相互正交的概念。
171
-
172
- **强类型**指值拥有确定的类型且不可(隐式地)相互转换,**弱类型**则指不同类型的值之间可以隐式转换。
173
-
174
- 类型系统的强/弱并不像动态/静态那样有明显的界限。一般认为,JavaScript、Visual Basic 等是典型的弱类型语言,允许一部分隐式转换的 C 和 C++ 也被认为是弱类型语言,而 Java、C#、Python 等则是典型的强类型语言。
175
-
176
- 例如,在 JavaScript 中:
177
-
178
- ```javascript
179
- var a = "5";
180
- var b = 4;
181
- var c = [3];
182
- var d = null;
183
- console.log(a - b + c - d);
184
- // Output:
185
- // 13
186
- ```
187
-
188
- 可以看到即使这些变量的类型完全不同,它们也能进行运算并产出不太符合直觉的结果。
189
-
190
- 而在 Python 中:
191
-
192
- ```python
193
- a = "5"
194
- b = 4
195
- print(a + b)
196
- // Output:
197
- // Traceback (most recent call last):
198
- // File "<stdin>", line 1, in <module>
199
- // TypeError: can only concatenate str (not "int") to str
200
- print(a + str(b))
201
- // Output:
202
- // 54
203
- ```
204
-
205
- 可见将不同类型的值直接进行运算会导致错误,必须先将其中的一些值显式转换为正确类型的值。
171
+
172
+ **强类型**指值拥有确定的类型且不可(隐式地)相互转换,**弱类型**则指不同类型的值之间可以隐式转换。
173
+
174
+ 类型系统的强/弱并不像动态/静态那样有明显的界限。一般认为,JavaScript、Visual Basic 等是典型的弱类型语言,允许一部分隐式转换的 C 和 C++ 也被认为是弱类型语言,而 Java、C#、Python 等则是典型的强类型语言。
175
+
176
+ 例如,在 JavaScript 中:
177
+
178
+ ```javascript
179
+ var a = "5";
180
+ var b = 4;
181
+ var c = [3];
182
+ var d = null;
183
+ console.log(a - b + c - d);
184
+ // Output:
185
+ // 13
186
+ ```
187
+
188
+ 可以看到即使这些变量的类型完全不同,它们也能进行运算并产出不太符合直觉的结果。
189
+
190
+ 而在 Python 中:
191
+
192
+ ```python
193
+ a = "5"
194
+ b = 4
195
+ print(a + b)
196
+ # Output:
197
+ # Traceback (most recent call last):
198
+ # File "<stdin>", line 1, in <module>
199
+ # TypeError: can only concatenate str (not "int") to str
200
+ print(a + str(b))
201
+ # Output:
202
+ # 54
203
+ ```
204
+
205
+ 可见将不同类型的值直接进行运算会导致错误,必须先将其中的一些值显式转换为正确类型的值。
206
206
207
207
### 运算符
208
208
278
278
2. ` 3 and 4 `
279
279
280
280
3. ` 0 or 1 and 2 `
281
-
281
+
282
282
4. ` 0 and 1 or 2 `
283
283
284
284
5. ` 1 or 0 and 2 `
You can’t perform that action at this time.
0 commit comments