Ch 2 - Variables, Expressions, and Statements Flashcards
Think Julia
What is: Assignment Statements
Assignment Statements
An assignment statement creates a new variable and gives it a value:
julia > message = “And now for something completely different” “And now for something completely different”
julia > n = 17
17
julia > π_val = 3.141592653589793
3.141592653589793
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 392-402). Kindle Edition.
Think Julia
Rules for making a variable name.
Variable Names
Programmers generally choose names for their variables that are meaningful— they document what the variable is used for.
Variable names can be as long as you like. They can contain almost all Unicode characters (see “Characters”), but they can’t begin with a number.
It is legal to use uppercase letters, but it is conventional to use only lowercase for variable names.
Unicode characters can be entered via tab completion of LaTeX-like abbreviations in the Julia REPL.
The underscore character, _, can appear in a name. It is often used in names with multiple words, such as your_name or airspeed_of_unladen_swallow. If you give a variable an illegal name, you get a syntax error:
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 413-423). Kindle Edition.
Think Julia
What is a: expression
An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions:
julia > 42
42
julia > n # n=17
17
julia > n + 25
42
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 444-449). Kindle Edition.
Think Julia
What is a: statement
A statement is a unit of code that has an effect, like creating a variable or displaying a value:
julia > n = 17
17
julia > println( n)
17
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 453-458). Kindle Edition.
Think Julia
What is a: interactive mode
So far we have run Julia in interactive mode, which means that you interact directly with the REPL. Interactive mode is a good way to get started, but if you are working with more than a few lines of code, it can be clumsy.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 465-467). Kindle Edition.
Think Julia
What is a: script mode
The alternative is to save code in a file called a script and then run Julia in script mode to execute the script. By convention, Julia scripts have names that end with .jl.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 467-470). Kindle Edition.
Think Julia
What is a (with regards to strings): *
The * operator performs string concatenation, which means it joins the strings by linking them end-to-end.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 538-543). Kindle Edition.
Think Julia
What is a (with regards to strings): ^
The ^ operator also works on strings; it performs repetition. For example, “Spam” ^ 3 is “SpamSpamSpam”. If one of the values is a string, the other has to be an integer.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 548-552). Kindle Edition.
Think Julia
What is a: #
For this reason, it is a good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called comments, and they start with the # symbol:
# compute the percentage of the hour that has elapsed percentage = (minute * 100) / 60
Note: Good variable names can reduce the need for comments, but long names can make complex expressions hard to read, so there is a trade-off.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 578-580). Kindle Edition.
Think Julia
What is a: expression
expression
A combination of variables, operators, and values that represents a single result.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 611-613). Kindle Edition.
Think Julia
What is a: evaluate
evaluate
To simplify an expression by performing the operations in order to yield a single value.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 613-616). Kindle Edition.
Think Julia
What is a: statement
statement
A section of code that represents a command or action. So far, the statements we have seen are assignments and print statements.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 616-618). Kindle Edition.
Think Julia
What is: semantics
semantics
The meaning of a program.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 636-637). Kindle Edition.
Think Julia
What is a: semantic error
semantic error
An error in a program that makes it do something other than what the programmer intended.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 637-639). Kindle Edition.