unit 2a - programming basics Flashcards
what is a variable?
a named space in memory, large enough to store a single piece of data
what is a constant?
a named space in memory with a value that can never change while the program is running
typically shown in uppercase and are separated by underscores
python DOES NOT use constants
what are the different data types?
- boolean
- character
- integer
- real
- string
what is boolean?
- can either be TRUE or FALSE
python: b = TRUE
what is a character?
- a single letter, number, space etc
python doesn’t have characters
what are integers?
- whole numbers - positive, negative or 0
python: i = 5
what is a real?
- decimal numbers
python: pi = 3.14
what is a string?
- a collection of characters (word or sentence)
python: name = “bob”
what is a declaration?
- code that causes a variable to exist
- once a variable has been declared, it can be used in the program
- you can’t use a variable before it has been declared
what is assignment?
- process of putting a value into a variable eg. i = 5
what is sequence?
- that instructions will always execute in the order in which they are written
example:
mark 1 <- 78
mark 2 <- 64
total <- mark1 + mark2
average <- total/2
OUTPUT average
what is selection?
- when a decision has to be made depending on whether the condition being tested is true or false
- IF is a common indicator of selection
example:
IF average >= 80 THEN
OUTPUT “distinction”
ELSE
OUTPUT “pass”
ENDIF
why declare a constant instead of a variable?
- prevents the value from being changed accidentally by part of code
- indicates that the value should stay constant throughout the program
can a constant ever change its value?
- a constant cannot be changed when the programming is running
- but can be changed before the program is compiled or translated
input statements in python
firstName = input (“what is your first name?”)
what are the arithmetic operators?
*
/
MOD
DIV
what is used to convert data types?
functions
what is casting?
functions being used to convert data types
what are some examples of functions?
- STRING_TO_INT(s) - converts a string s to an integer
- INT_TO_STRING(x) - converts an integer to a string
- CHAR_TO_CODE(‘a’) - evaluates to 97, using ASCII
- CODE_TO_CHAR(97) - evaluates to ‘a’, using ASCII
how do you allow a user to input a numerical value? (pseudocode)
- inputs from users are strings so they have to be converted before they can be used in a calculation eg.
OUTPUT “please enter the number of cats:”
tickets <- STRING_TO_INT(USERINPUT)
how do you allow a user to input a numerical value in python?
cats = int(input(“please enter the number of tickets:”))
what is concatenating?
joining two string variables together eg.
firstname <- “rose”
surname <- “chan”
fullname <- firstname + “ “ + surname
OUTPUT fullname
how do you identify the length of a string? (string handling functions)
LEN(str): word <- “algorithm”
OUTPUT LEN(word)
the output would be 9 because it is 9 letters long
how do you get a substring? (string handling functions)
SUBSTRING(start, end, str):
word <- “algorithm”
OUTPUT(3,6, word)
the output would be ‘orit’ because those are the letters between index 3 and 6
NOTE: strings always start their index at 0