ESS多版本 【TA】【ESS】01 变量和数据类型

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
1: Variables and data types 变量和数据类型

Every proper programming language has variables. These variables are used throughout your code or program to hold certain information so you can use it whenever you’d like.
Such a variable also has a name. You can look at it as a box: the name is like a label which allows you to identify the different boxes (variables) you have, and the information/data the variables store can be considered the content of those boxes.

变量非常重要,变量在整个代码中的作用为——存储某些信息,或者说数据。
在编程的过程中,只要通过正确的方法,你就可以随时使用变量中存储的信息。
每一个变量都有一个名字,并且名字是唯一的,比如说某个变量的名字为abc,而另一个变量的名字为def,等等。
在不同的时间点,同一个变量存储的信息可能是不同的,比如说某时abc这个变量存储的信息是123,而另一个时间点abc可能存储的就是456了,等等。
 

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
Data types 数据类型

The information a variable stores is also called the value of the variable. This value is of a certain type: a number, text, a fraction, a list of things, or else. There are a few names to remember here:

变量存储的数据,或者说保存的数据,也被称为变量的值,比如说abc这个变量保存的数据是123,我们就可以说abc的值是123。
数据是多种多样的,比如说有这么几种类型:

Fixnum: This is an integer, referring to any number without any decimal numbers (e.g. 3, -85, 9230)
Float: This is a fraction, referring to any number with decimal numbers (e.g. 5.6, -372.5234, 2.0)
String: A series of characters which together represent a word, sentence, or else.

Fixnum/整数型: 整数,指的是任何没有小数点的数字 (例如 3, -85, 9230)
Float/浮点数型: 小数,指的是任何带有小数点的数字 (例如 5.6, -372.5234, 2.0)
String/字符串型: 一系列字符构成的词语或者句子 (例如 你好, 今天天气真好!, 你好漂亮。)

什么是字符?
简单的说就是啥都是字符。
英文字母是字符,中文单字是字符,标点符号也是字符,等等。
所以,字符串就是一串字符。

Here’s where we start coding: we’re going to define/initialize/create a variable by giving it a name and a value. We do this like so:

那就让我们从这里开始我们的编程之旅吧。
我们通过给一个变量名字和值,来定义一个变量(或者说初始化一个变量/创建一个变量)。
我们可以这样做:


Here, we’ve created a new variable called my_variable and given it a value of 10. 10 is a Fixnum (integer), so we would say that my_variable is a Fixnum or my_variable is of type Fixnum.

现在,我创建了一个叫做my_variable(我的变量)的变量,并且赋予了它一个值 10。
所以目前这个变量,名字是my_variable,值是10。
因为10是一个整数,所以我们也可以说my_variable是一个整数。
同时也可以发现,我是通过一个=(等于号)来给这个变量指定一个值的,目前值是10,这个过程,或者说这个操作,叫做赋值,即给某一个变量赋予某一个值。
也就是说,我随时可以使用my_variable = xxx这样的写法格式来为my_variable指定新的值。

Ruby is a dynamically typed language, which means you do not need to specify a data type (such as Fixnum, Float, or String) when defining a variable. The value of the variable does not always need to have the same type and may change, unlike statically typed languages such as C#.
 
  • Like
反馈: qwionz

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
Strings 字符串

While Fixnums and Floats are very basic (5, 8.3), Strings have a bit more to them.
Let’s say we want to put Hello world! in a string. A string is created by surrounding the text you want to put into the string with double quotes. This would get us "Hello world!". This is now a valid string with the content of Hello world!.

接着,我们来讲解字符串。
为一个变量指定字符串值和指定数字值不太一样。
数字值你只需要直接写一个数字就好了,比如my_variable = 10,my_variable = 10.0,等等。
但是,当你想要指定字符串时,你需要把你想要指定的字符串内容放到一个引号里面。
比如说,我现在想要指定的字符串是“Hello world!”(你好,世界。),那就需要这样写:my_variable = “Hello world!”。
注意,这里的引号是英文的引号。

There are a few exceptions to this basic rule though. If you want to use a double quote (“) inside the string, such as in I like the word “wholesome”, you have to put a backslash (\) before each double quote. In string-form, this would look like "I like the word \"wholesome\"".

同时,如果你的字符串里面也有引号的话,比如说“I like the word “wholesome”.”(我喜欢“有益健康的”这个词。),你不能直接写my_variable = “I like the word “wholesome”.”,你需要这样写my_variable = “I like the word\ “wholesome\”.”。
也就是说,你需要在字符串里面的引号的每个半引号前加一个反斜杠。
另外,额外补充,/是斜杠(Slash),\是反斜杠(Backslash)。

This process of adding backslashes to certain characters is called escaping that character. When you escape the letter n, which would look like \n when used inside a string, is a new line, for example.

If you wanted to create a variable of the String type, you would write down something akin to this:

现在,我们来创建一个新的字符串型变量my_greeting(我的问候):
这边也额外说一下,虽然可能是没必要的,你在写代码的时候,需要使用英文字母,不能使用中文。
如果你不会英文,你可以用拼音,比如这里的这个变量名叫“我的问候”,你不能写“我的问候”,但是你可以写“wodewenhou”。


To print out the value of a variable, "Hello everyone!" in the example above, for now, you should use print.

如果你要打印my_greeting这个变量的值“Hello everyone!”(大家好)的话,你可以使用print(打印)。


As you can see, everything that comes after the # is grey. This is a comment, and not actually part of the code. You can use it to leave notes inside your code to make it more readable or understandable, which is what I will be doing a lot in this tutorial series.
Whenever I use #=>, that still is a comment, I just add the => part to indicate that it’s being printed out.

#是注释的意思,注释就是解释。
就是说#后面的部分并不是实际代码的一部分,它们只是起到一个解释说明的作用,使你的代码更易于理解。
 

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
Basic mathematical operations 基础数学运算

The basic mathematical operations all have unique mathematical operators:

基础的数学运算依赖于它们各自的数学运算符:

+ Addition 加法运算
- Subtraction 减法运算
* Multiplication 乘法运算
/ Division 除法运算
** Power To 乘方运算

Let’s start by getting a number.

让我们从简单的开始:


To perform math, we take either a number or a variable and use one of the operators listed above.

请看下面的一系列进行数学运算的例子:


This was with numbers, but we can also do this with plain variables:

上面的例子是直接使用数字进行数学运算,我们也可以直接使用变量进行数学运算,只要变量的值是数字就好了:


Everything we’ve done so far doesn’t actually change anything though. It just performs the math we want, and that’s that. If we want to actually change the value of a variable, we have to reassign to that variable.

到目前为止,我们只是对变量进行数学运算,并没有实际上改变变量的值。
前面我们已经说过了“同一个变量存储的信息可能是不同的”和“随时可以使用my_variable = xxx这样的写法格式来为my_variable指定新的值”,也就是说,我们可以通过给变量指定不同的值进而来改变变量的值。
我们来看下面的例子:


So based on this example, to increment a variable by 2, you should use var = var + 2. While this is fine, you can write it in a shorter way. Introducing:

请看下面的例子,当你想要对一个变量本身的值进行数学运算的时候,你当然可以如下面左边的例子所示那样写,同时,你也可以使用简便的写法,也就是右边的所示的那样的写法:

var = var + 2 is the same as var += 2 原数值加2
var = var - 2 is the same as var -= 2 原数值减2
var = var * 2 is the same as var *= 2 原数值乘2
var = var / 2 is the same as var /= 2 原数值除以2
var = var ** 2 is the same as var **= 2 原数值的平方

Obviously, these also work with variables rather than numbers:

我们再来看下面的不是使用数字,而是直接使用变量的例子:


当你使用除法运算的时候,一定需要特别注意,比如说:

本章完。
 
  • Like
反馈: qwionz

在线成员

现在没有成员在线。

论坛统计

主题
543
消息
2,484
成员
3,165
最新成员
暗鹰ex