Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next Scratch ActiveCode Profile
title here
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 2.2 Variables
One of the most powerful features of a programming language is the ability to manipulate
variables . A variable is a name that refers to a value.
An
assignment statement creates new variables and gives them values:
This example makes three assignments and then prints the value of each of the variables. The first assigns a string to a new variable named
message; the second assigns the integer 17 to a variable named
n; and the third assigns the (approximate) value of pi to a variable named
pi.
The type of a variable is the type of the value it refers to.
Activity 2.2.1 .
Activity 2.2.2 .
Click the variables representing strings in this block.
Remember that the type of a variable is the type of the value it refers to.
message = ’And now for something completely different’
n = 17
number = "123456789"
pi = 3.1415926535897931
Activity 2.2.3 .
How would you assign the variable
name to the string
Penelope?
name = ’Penelope"
The quotation marks on each side of the string need to be the same, either single or double, not a mix.
name = "Penelope"
An equals sign is needed to assign a variable and quotation marks tell the program that the value is a string.
name = Penelope
What symbols are missing to make ``Penelope`` a string?
name, "Penelope"
Look at the variable assignments above, what’s missing?
You have attempted
of
activities on this page.