03_data_containers.ipynb
- Lecture 3: Data Containers
- Outline
- `String`: Container of `Char` Values
- Converting to `String`
- Converting from `String`
- `String` and Print Statements
- `Vector`: Container of Data Types
- The Dot `.` Syntax
- `String` vs. `Vector`: Similarities
- `String` vs. `Vector`: Differences
- `String` Concatenation and Interpolation
- The `push!` Function
- Summary: Looking Back and looking forward
- File 文件
- Edit 编辑
- View 看法
- Run
- Kernel 核心
- Settings 设置
- Help 帮助
Introduction to Programming for Business Analytics
业务分析编程简介 ¶
业务分析编程简介
Murwan Siddig, Stefan Pilot
穆尔万·西迪格、斯特凡·派洛特
String
: Container of Char
Values¶
String
: Char
值的容器 ¶
A
Char
(short for character) is a data type that represents a single glyph, whitespace, and special control values.
Char
(字符的缩写)是一种数据类型,表示单个字形、空格和特殊控制值。- Glyphs can be any letter, digit, or symbol.
字形可以是任何字母、数字或符号。
- Glyphs can be any letter, digit, or symbol.
To define a
Char
value in Julia we use single quotation delimeters''
.
要在 Julia 中定义Char
值,我们使用单引号分隔符''
。
Examples: 例子:
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
Char
'5': ASCII/Unicode U+0035 (category Nd: Number, decimal digit)
Char
' ': ASCII/Unicode U+0020 (category Zs: Separator, space)
Char
- We can also type unicode characters can be entered via tab completion of LaTeX-like abbreviations.
我们还可以通过类似 LaTeX 的缩写的制表符补全来输入 unicode 字符。 - For example, to get the greek letter
we write\pi
and press and holdTAB (⇥)
例如,要获取希腊字母 ,我们写\pi
并按住TAB (⇥)
'π': Unicode U+03C0 (category Ll: Letter, lowercase)
Char
A
String
is a container data type that contains a sequence ofChar
values.
String
是包含一系列Char
值的容器数据类型。
String
can represent arbitrary pieces of text.
String
可以表示任意文本片段。A
String
literal is the string's value enclosed between delimiters""
.
String
文字是分隔符""
之间的字符串值。
Examples: 例子:
I am a String that contains " as a character. I am a String that contains \ as a character.
In the Julia code of the above examples, \" and \\ are examples of what is known as an escape sequence.
We can also use escape sequences to repre- sent special values such as the linebreak or the tab whitespace ' '.
Converting to String
¶
转换为 String
¶
- We can use the built-in function
string
to convert any Julia value to aString
.
我们可以使用内置函数string
将任何 Julia 值转换为String
。
"1"
"340282366920938463463374607431768211456"
"3.14"
"true"
Converting from String
¶
从 String
转换 ¶
- We can also convert some
String
s into other data types using the built-in functionparse
.
我们还可以使用内置函数parse
将一些String
转换为其他数据类型。
1
340282366920938463463374607431768211456
3.14
false
String
and Print Statements¶
String
和打印语句 ¶
String
values are very useful inside of a print statement.
String
值在 print 语句中非常有用。- In the following example, we include a
String
value in the print statement to display what is being calculated and the result of the calculation in the same line.
在下面的示例中,我们在 print 语句中包含一个String
值,以在同一行中显示正在计算的内容和计算结果。
x+y= 4
Note: 笔记:
- The comma
,
between the string"x+y= "
and the algebraic expressionx+y
is a delimiter.
字符串"x+y= "
和代数表达式x+y
之间的逗号,
是分隔符。 - We can have as many string representation and algebraic expression pair as we want.
我们可以拥有任意数量的字符串表示形式和代数表达式对。
x+y= 4 and x-y= 2
Vector
: Container of Data Types¶
Vector
:数据类型容器 ¶
- A vector is a data container that may contain more than one value of any data types.
向量是一种数据容器,可以包含多个任意数据类型的值。 - Vectors are also very often referred to as Arrays.
向量也经常被称为数组。- In many programming languages, what is known in Julia as a
Vector
is called an Array.
在许多编程语言中,Julia 中的Vector
称为数组。 - In Julia, a
Vector
is a special case of the data typeArray
.
在 Julia 中,Vector
是数据类型Array
的特例。 - We will discuss the Julia data type
Array
later, in another lecture.
我们将在稍后的另一场讲座中讨论 Julia 数据类型Array
。
- In many programming languages, what is known in Julia as a
- A vector literal is a comma-separated list of values inside the delimiters
[
and]
.
向量文字是分隔符[
和]
内以逗号分隔的值列表。
Examples: 例子:
Any[]
3-element Vector{Int64}: 1 2 3
4-element Vector{Any}: 1.0 [1, 2, 3] "Hello!" Any[]
The Dot .
Syntax¶
点 .
语法 ¶
For every binary operator like +
, there is a corresponding dot operator .+
that is automatically defined to perform (broadcast) +
element-by-element on vectors.
对于像 +
这样的每个二元运算符,都有一个相应的点运算符 .+
,它被自动定义为在向量上逐个元素地执行(广播) +
。
Examples: 例子:
- Scalar addition, subtraction, multiplication, division and exponentiation:
标量加、减、乘、除和幂:
data1 .+ 2 = [3, 4, 5] data1 .- 2 = [-1, 0, 1] data1 .* 2 = [2, 4, 6] data1 ./ 2 = [0.5, 1.0, 1.5] data1 .^ 2 = [1, 4, 9]
- Vector elementwise addition, subtraction, multiplication, division and exponentiation:
向量元素加法、减法、乘法、除法和求幂:
data1 .+ data2 = [4, 4, 4] data1 .- data2 = [-2, 0, 2] data1 .* data2 = [3, 4, 3] data1 ./ data2 = [0.3333333333333333, 1.0, 3.0] data1 .^ data2 = [1, 4, 3]
- Any (built-in and user-defined) function can be applied elementwise to any (compatible) vector with the dot
.
syntax.
任何(内置和用户定义)函数都可以使用点.
语法按元素应用于任何(兼容)向量。
3-element Vector{Float64}: 2.0 3.0 4.0
6-element Vector{DataType}: Int64 BigInt Float64 Bool String Vector{Any} (alias for Array{Any, 1})
3-element Vector{Float64}: 18979.785277440005 20528.53575607911 22203.664273775164
String
vs. Vector
: Similarities¶
String
与 Vector
:相似之处 ¶
String
s and Vector
s share many characteristics.
String
和 Vector
有许多共同特征。
- Values inside of vectors and strings are stored sequentially.
向量和字符串内部的值是按顺序存储的。- Every value is associated with a position.
每个值都与一个位置相关联。 - Positions are associated with numbers referred to as indices (plural for index).
头寸与称为指数(指数的复数形式)的数字相关联。 - Values can be retrieved using their index (i.e., the position's number).
可以使用索引(即位置编号)检索值。
- Every value is associated with a position.
- To retrieve a value from a
String
orVector
, we pass the index of the position to the bracket operator.
要从String
或Vector
检索值,我们将位置索引传递给括号运算符。
Examples: 例子:
6
3
1
'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)
'l': ASCII/Unicode U+006C (category Ll: Letter, lowercase)
'l': ASCII/Unicode U+006C (category Ll: Letter, lowercase)
Instead of single numbers, we can also pass a range
除了单个数字之外,我们还可以传递一个范围
Examples: 例子:
3-element Vector{Int64}: 5 4 3
"ello, world"
0
5
String
vs. Vector
: Differences¶
String
与 Vector
:差异 ¶
- We can change the values contained in a
Vector
, but not the values contained in aString
.
我们可以更改Vector
中包含的值,但不能更改String
中包含的值。
aString
is an immutable data type.
aString
是不可变的数据类型。
aVector
is a mutable data type.
aVector
是可变数据类型。
- If we try to mutate a
String
an error will be raised.
如果我们尝试改变String
则会引发错误。
MethodError: no method matching setindex!(::String, ::Char, ::Int64) Stacktrace: [1] top-level scope @ In[44]:1
[3, 4]
String
Concatenation and Interpolation¶
String
连接和插值 ¶
- We can concatenate two
String
s using the*
operator.
我们可以使用*
运算符连接两个String
。
"Hello, world!"
We have said "Hello, world!" 3 times in this lecture!
- We can interpolate into a
String
using the$
operator.
我们可以使用$
运算符插入String
中。
Here is the 4th time: Hello, world!
The push!
Function¶ push!
函数 ¶
- A very useful built-in function for working with vectors is the
push!
function.
push!
函数是处理向量的一个非常有用的内置函数。 - The
push!
can be used to insert one or more items at the end of the vector.
push!
可用于在向量末尾插入一项或多项。
[3, 4, 7]
[3, 4, 7, 11, 18, 29]
Note: A stylistic convention in the Julia language is' to indicate any function that changes at least one of their arguments by using ! as the last character in its name.
注意:Julia 语言中的一种风格约定是 ' 来指示任何使用 ! 更改至少一个参数的函数。作为其名称的最后一个字符。
Summary: Looking Back and looking forward¶
摘要:回顾与展望 ¶
Looking Back: 回头看:
String
: a set of characters for representing text.
String
:表示文本的一组字符。- Strings are made up of characters (
Char
).
字符串由字符 (Char
) 组成。 - A
Char
(short for character) is a data type that represents a single glyph, whitespace, and special control values.
Char
(字符的缩写)是一种数据类型,表示单个字形、空格和特殊控制值。 Vector
: a data container that may contain more than one value.
Vector
:可能包含多个值的数据容器。- Vectors are mutable but strings are immutable.
向量是可变的,但字符串是不可变的。 - The dot syntax performs element-by-element operations on vectors.
点语法对向量执行逐元素运算。
Looking Forward: 期待:
- How can we use these different data types in a meaningful context.
我们如何在有意义的上下文中使用这些不同的数据类型。 - The conditional execution and repetition parts of a program.
程序的条件执行和重复部分。