Variables and constants Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are variables and constants used for?

A

To store values in algorithms and programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Use of identifiers (2), long and short versions, and limitations (2)

A

A name given to a variable or constant

Identifiers describe the data being stored

  • short identifiers are easier to spell correctly each time they are used
  • long identifiers can be used if they are more descriptive (e.g. firstName)

They can’t be the same as reserved words such as print or while
They must be consistent throughout the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Difference between variables and constants

A

Variables can change while a program is running

Constants must not change while a program is running

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

assignment statements

A

Variables are assigned in assignment statements using the = symbol

They can be changed while the program is running, e.g. variable = variable * 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Constants

A

They are assigned using:

const (name) = (value)
const Pi = 3.142

They are used in calculations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Camel case and snake case

A

Ways to format identifiers

In camel case, lower and upper case characters are used
The first word can be lower case (or not):
variableName
VariableName
FirstName
pricePerKilo
PricePerKilo

In snake case words are linked with an underscore:
first_name
price_per_kilo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Assignments

A

The association of a piece of data with a variable or constant

Often done as an input

Variables can be output to the user and will print the value rather than the identifier:
print(name) doesn’t output the word ‘name’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Identify the variables that are used in this algorithm and state the purpose of each. [6]

length = input ("Enter the length of the rectangle.")
width = input ("Enter the width of the rectangle.")

area = length * width

print(area)

A

The variables are:
length, width, and area

length and width store values that are input by the user, and area stores the result of multiplying the length by the width

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A student is writing an algorithm that allows a user to input their marks for five tests and then calculate the average mark.

List suitable variables that can be used in the algorithm. [1]

A

result1, result2, “, “, “, average

How well did you know this?
1
Not at all
2
3
4
5
Perfectly