julia在mac环境变量_Julia的变量和类型

julia在mac环境变量

A brief introduction to Julia’s variables and types.

Julia的变量和类型的简要介绍。

This is part 4 of the Julia Tutorials (a series of tutorials on Julia). If you are not familiar with Julia’s REPL, please go through part 3 for better understanding.

这是Julia教程(Julia的一系列教程)的第4部分。 如果您不熟悉Julia的REPL,请仔细阅读第3部分,以更好地理解。

If you are not familiar with Julia and want to get started with it, feel free to check out the complete series —

如果您不熟悉Julia,并想开始使用它,请随时查看完整的系列-

Note: Use Julia’s REPL to quickly follow along with the codes in this tutorial

注意:使用Julia的REPL可以快速遵循本教程中的代码

Julia中的变量 (Variables in Julia)

Like in every other programming language, variables are locations in the computer memory named by the user to hold some data related to the program.

与其他所有编程语言一样,变量是用户在计算机内存中命名的位置,用于保存与程序有关的某些数据。

Creating variables in JuliaIn Julia, variable names are case-sensitive and a wide range of unicode characters (of UTF-8 encoding) are also supported. Variable can be assigned/created by using the conventional assignment operator “=”.

在Julia中创建变量在Julia中,变量名称区分大小写,并且还支持范围广泛的Unicode字符(采用UTF-8编码)。 可以使用常规的赋值运算符“ =”来赋值/创建变量。

test_string = "Julia variable"
pi_val = 3.14
count = 35
❄ = -12

In the above examples, 3 variables namely “test_string”, “pi_val” and “count” are created/assigned with some values(data types).

在上面的示例中,使用一些值(数据类型)创建/分配了3个变量“ test_string”,“ pi_val”和“ count”。

Variable to Variable AssignmentValue in one variable can be assigned to another by simply following the n be equal to m convention

变量到变量的分配只需遵循n等于m约定,就可以将一个变量中的值分配给另一个变量

m = 10
n = m # value of m will be copied to n

Naming convention for variablesThough there aren’t many restrictions in naming variables, some styling conventions are recommended.

变量的命名约定尽管变量命名没有很多限制,但建议使用一些样式约定。

  • lower cased names are preferred

    首选小写名称
  • Variable names may not start with a number nor an exclamation mark.

    变量名不能以数字或感叹号开头。
  • Though use of “_” in names are less preferred, “snake_cases” can be used for improving readability of long function and variable names.

    尽管在名称中使用“ _”不是首选, 可以使用“ snake_cases ”来提高长函数变量名的可读性。

  • CamelCase is preferred for naming Types and Modules

    CamelCase是命名类型模块的首选

  • Provide leading and trailing white space for the assignment operator “=”

    为赋值运算符“ =”提供开头和结尾的空格

Julia中的类型 (Types in Julia)

A type constrains the values that an expression, such as a variable or a function, might take.

类型限制表达式(例如变量或函数)可能采用的值。

Types can be broadly classified into 2:

类型可以大致分为2类:

  • Static Types - Every executable program expression is defined with a type prior to its execution

    静态类型 -每个可执行程序表达式在执行之前都定义了一个类型

  • Dynamic Types - Compiler has to decide the type of the values at the time of code compilation

    动态类型 -编译器必须在代码编译时决定值的类型

Julia’s type system is primarily dynamic and is augmented by the ability to specify types where needed, meaning that there is no need to tell Julia what type a particular value is unless you want to.

Julia的类型系统主要是动态的,并且通过在需要的地方指定类型的能力得到增强,这意味着除非您愿意,否则无需告诉Julia特定值是什么类型。

Based on Julia’s type hierarchy, types can be abstract or concrete.

基于Julia的类型层次结构,类型可以是抽象的具体的

  • Abstract Types - A type that is intended solely to act as super-types of other types, rather than types of particular objects. i.e, abstract types can be divided into sub-types. Some examples are: Number , String , Bool , Rational , etc…

    抽象类型 -一种类型,旨在仅充当其他类型的超类型,而不是特定对象的类型。 也就是说,抽象类型可以分为子类型。 一些示例是: NumberStringBoolRational 等等。

  • Concrete Types - These are the types of actual objects. They are always sub-types of abstract types and they are not allowed to have any sub-types.

    具体类型 -这些是实际对象的类型。 它们始终是抽象类型的子类型,并且不允许具有任何子类型。

Concrete Types can be again divided into two, primitive and composite.

具体类型可以再次分为原始类型复合 类型两种

Primitive Types - Primitive Types in Julia are the concrete types whose value is in the form of bits. Some examples of primitive types are: Int8 , Int16 , Int64 , Float16 , Float32 , String , etc…

基本类型 - Julia中的原始类型是具体类型,其值采用位的形式。 基本类型的一些示例是: Int8Int16Int64Float16Float32String 等。

Unlike most languages, with Julia you can declare your own primitive types.

与大多数语言不同,使用Julia可以声明自己的原始类型。

Standard primitive types are defined as:

标准基本类型定义为:

primitive type Float64 <: AbstractFloat 64 end
primitive type Bool <: Integer 8 end
primitive type Char <: AbstractChar 32 end
primitive type Int64 <: Signed 64 end

Similarly we can define our own primitives with the following syntax:

同样,我们可以使用以下语法定义自己的原语:

primitive type Byte 8 end    # Define the primitive Byte
Byte(val::UInt8) = reinterpret(Byte, val)
b = Byte(0x02) # Assign a byte value to b

Here, reinterpret is used to assign an unsigned integer with 8 bits (UInt8) into the Byte.

在此, reinterpret用于将具有8位(UInt8)的无符号整数分配给Byte。

Note: You may be wondering what :: operator is for, don’t worry it will be explained in the follow-up tutorial

注意 :您可能想知道::运算符的用途,请不要担心,它将在后续教程中进行解释。

Composite Types - Composite types in Julia are a collection of named fields which can be individually treated as single values of specific types. Composite types can be declared with the use of struct keyword.

复合类型-Julia中的复合类型是命名字段的集合,可以将其单独视为特定类型的单个值。 可以使用struct关键字声明复合类型。

Syntax:

句法:

struct Foo
Field1::Type
Field2::Type
end

Example:

例:

# Define the struct
struct Greet
x
y::String
end
# Creating object with constructor
greet1 =

The values of a composite object can be accessed using the dot notation,

可以使用点符号来访问复合对象的值,

greet1.xOutput -> "Hello"

One interesting thing about composite types is that they are immutable in nature, i.e, we cannot change the value after instantiating the object. Fortunately, Julia also provides mutable types that have a similar syntax like the immutable composite types, the only difference is the usage of the mutable keyword in front of struct .

关于复合类型的一件有趣的事情是它们本质上是不可变的,即,我们无法在实例化对象后更改值。 幸运的是,Julia还提供了具有类似语法的可变类型 ,例如不可变复合类型,唯一的区别是在struct前面使用了mutable关键字。

Syntax:

句法:

mutable struct Foo
Field1::Type
Field2::Type
end

Example:

例:

# Define the mutable struct
mutable struct Num
x::Float64
y::Float64
end# Creating object with constructor
greet1 =# Update a value
greet1.x = 4.5

You can change the values ​​after instantiation. However, these values ​​must comply with the type’s definition in that they must be convertible to the specified type (in our case Float64). For example, an Int64 input would be acceptable because you can easily convert an Int64 to a Float64. On the other hand, a string wouldn’t work because you can’t convert it to Float64 .

您可以在实例化后更改值。 但是,这些值必须符合类型的定义,因为它们必须可以转换为指定的类型(在我们的示例中为Float64 )。 例如,可以接受Int64输入,因为您可以轻松地将Int64转换为Float64 。 另一方面,字符串不起作用,因为您无法将其转换为Float64

Next Up: Dive deep into Julia’s Types

下一步:深入研究Julia的类型

翻译自: https://levelup.gitconnected.com/variables-and-types-in-julia-750b057c4537

julia在mac环境变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值