Variables and Constants in Programming Flashcards
What is a problem (in the context of programming?
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)
What are the steps of problem analysis?
- can it be broken down into smaller parts
- can certain steps be ignored
- can the outcome be predicted
- is the solution optimal
How can generality and flexibility be achieved in a program?
using VARIABLES to store numbers and allowing user to INPUT the numbers at runtime (as the program runs)
What is a VARIABLE?
NAMED STORAGE location in a computer program
How do you define a variable on Python?
tell python:
1. what named it is referred to
2. initial value of the variable
How do you place a value into a variable?
assignment statement (define the variable)
with an assignment statement
ex.
#assignment statement (define the variable)
x=5
is the = sign used for comparison?
NO! it is an assignment statement, - it copies the value on the right side into the variable on the left side
Should the variable always be x?
NO! variable names should describe the purpose of the variable
ex studentGPA vs sgpa
What are the Variable Name rules?
- start with lowercase or underscore
- not spaces
- separate words with ‘camelCase’ notation
- don’t use ‘reserved’ python words (ex. print, input)
- letters numbers and underscore only
- cannot use / or .
What is the undefined variable error?
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
What are the three data types in python?
integer or int - whole number
float - fraction part
string - sequence of characters
how can you accept input from a user?
built-in function used to read a STRING from standard input
input()
what is the syntax of input?
variable = input(argument)
x=input(‘enter a value:’
print(‘you entered:’,x)
What data type is the input () function default?
string
How can you have python read a number from a user?
typecast the string into int or float with int() and float() functions
x=int(input(‘integer:’)
x=float(input(‘fraction:’)