Variables and Constants in Programming Flashcards

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

What is a problem (in the context of programming?

A

a specific task that a computer program is designed to solve with a logical solution with a set of STEPS in an algorithm (ex. counting students)

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

What are the steps of problem analysis?

A
  • can it be broken down into smaller parts
  • can certain steps be ignored
  • can the outcome be predicted
  • is the solution optimal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can generality and flexibility be achieved in a program?

A

using VARIABLES to store numbers and allowing user to INPUT the numbers at runtime (as the program runs)

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

What is a VARIABLE?

A

NAMED STORAGE location in a computer program

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

How do you define a variable on Python?

A

tell python:
1. what named it is referred to
2. initial value of the variable

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

How do you place a value into a variable?

A

assignment statement (define the variable)

with an assignment statement

ex.
#assignment statement (define the variable)
x=5

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

is the = sign used for comparison?

A

NO! it is an assignment statement, - it copies the value on the right side into the variable on the left side

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

Should the variable always be x?

A

NO! variable names should describe the purpose of the variable
ex studentGPA vs sgpa

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

What are the Variable Name rules?

A
  1. start with lowercase or underscore
  2. not spaces
  3. separate words with ‘camelCase’ notation
  4. don’t use ‘reserved’ python words (ex. print, input)
  5. letters numbers and underscore only
  6. cannot use / or .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the undefined variable error?

A

must define variable before you use it above the line of code where you 1st use the variable

ex. literPerOunce = 0.0296
canVolume = 12*literPerOunce

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

What are the three data types in python?

A

integer or int - whole number
float - fraction part
string - sequence of characters

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

how can you accept input from a user?

A

built-in function used to read a STRING from standard input

input()

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

what is the syntax of input?

A

variable = input(argument)

x=input(‘enter a value:’
print(‘you entered:’,x)

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

What data type is the input () function default?

A

string

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

How can you have python read a number from a user?

A

typecast the string into int or float with int() and float() functions

x=int(input(‘integer:’)
x=float(input(‘fraction:’)

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

What happens when you update a variable?

A

the new value assignment of a variable replaces the previous contents of the variable

17
Q

What are the steps to update a variable to assign a value?

A
  1. calculate right-side of the assignment
  2. store the result in the variable names on the left-hand side of the assignment operator
18
Q

Can a variable be assigned different values at different places?

A

YES; data type (string, float, int) is associated with the VALUE, NOT the variable, so the variable can be associated with a string and float and int at different places
–> if you use a variable and it has an unexpected type, there will be an error

19
Q

What is a constant?

A

VARAIBLE that should not be changed after it’s assigned an initial value

20
Q

How are constants named?

A

Capitalized! name constants to explain numerical values to be used in calculations

21
Q

what are unexpected types of data that will result in error?

A

fractions and numbers with commas