unit 2a - programming basics Flashcards

1
Q

what is a variable?

A

a named space in memory, large enough to store a single piece of data

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

what is a constant?

A

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

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

what are the different data types?

A
  • boolean
  • character
  • integer
  • real
  • string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is boolean?

A
  • can either be TRUE or FALSE
    python: b = TRUE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is a character?

A
  • a single letter, number, space etc
    python doesn’t have characters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what are integers?

A
  • whole numbers - positive, negative or 0
    python: i = 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is a real?

A
  • decimal numbers
    python: pi = 3.14
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is a string?

A
  • a collection of characters (word or sentence)
    python: name = “bob”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is a declaration?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is assignment?

A
  • process of putting a value into a variable eg. i = 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is sequence?

A
  • 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

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

what is selection?

A
  • 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

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

why declare a constant instead of a variable?

A
  • prevents the value from being changed accidentally by part of code
  • indicates that the value should stay constant throughout the program
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

can a constant ever change its value?

A
  • a constant cannot be changed when the programming is running
  • but can be changed before the program is compiled or translated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

input statements in python

A

firstName = input (“what is your first name?”)

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

what are the arithmetic operators?

A

*
/

MOD
DIV

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

what is used to convert data types?

A

functions

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

what is casting?

A

functions being used to convert data types

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

what are some examples of functions?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

how do you allow a user to input a numerical value? (pseudocode)

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

how do you allow a user to input a numerical value in python?

A

cats = int(input(“please enter the number of tickets:”))

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

what is concatenating?

A

joining two string variables together eg.
firstname <- “rose”
surname <- “chan”
fullname <- firstname + “ “ + surname
OUTPUT fullname

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

how do you identify the length of a string? (string handling functions)

A

LEN(str): word <- “algorithm”
OUTPUT LEN(word)
the output would be 9 because it is 9 letters long

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

how do you get a substring? (string handling functions)

A

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

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

how do you find a character’s index position?

A

POSITION(str,char):
word <- “algorithm”
POSITION(word, ‘r’)

the output would be 4 because ‘r’ is at index 4

26
Q

how do you convert a character to ASCII using functions?

A

CHAR_TO_CODE(‘character’)
CODE_TO_CHAR(integer)

example: num <- CHAR_TO_CODE(‘c’)
letter <- CODE_TO_CHAR(102)

27
Q

how do convert from uppercase to lowercase (and vice versa) in python?

A

phrase1 = “good morning”
phrase2 = “HAPPY BIRTHDAY”
print(phrase1,upper()) #”GOOD MORNING”
print(phrase2.lower()) #”happy birthday”

28
Q

what are comments used for in programs?

A
  • describing the purpose of the program
  • state the author
  • explaining what the code does
29
Q

which symbol is used to write a comment in python?

A

#

30
Q

what are the three basic control structures in programming?

A

sequence, selection and iteration

31
Q

what are the three boolean operators?

A

AND, OR, NOT

32
Q

what are the boolean comparison operators? what will the outcome always be?

A

= equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to

the outcome of comparison expressions will always be true or false

33
Q

what do IF statements do?

A

they allow different branches to be executed based on the result of a boolean expression

example:
IF average >= 80 THEN
OUTPUT “distinction”
ELSE IF average >=
OUTPUT “merit”
ELSE
OUTPUT “fail”
ENDIF

34
Q

what is a nested if statement?

A

an if statement within another loop

example:
IF member = “child” THEN
IF day = “saturday” THEN
swimPrice <- 2.00
ELSE
swimPrice <- 2.50
ELSE
swimPrice <- 4.00
ENDIF

35
Q

what are complex boolean expressions?

A

those that contain AND, OR or NOT ‘
example: IF (mark < 0) AND (mark > 100) THEN

36
Q

what does the AND operator do?

A

returns TRUE if both conditions are TRUE

37
Q

what does the OR operator do?

A

returns TRUE if either of the conditions are TRUE

38
Q

what does the NOT operator do?

A

a TRUE expression become FALSE and vice versa

39
Q

how do get a random number in python?

A

import random
die = random.randint(0,100)
print(die)

the import random will import the random library of functions

40
Q

how do you get a random number in pseudocode?

A

die = RANDOM_INT(0,100)
OUTPUT die

41
Q

what is iteration?

A

the repetition of a section

42
Q

what are the three types of iteration statements?

A

FOR…ENDFOR
WHILE..ENDWHILE
DO…UNTIL (not in python)

43
Q

what do FOR loops do?

A

used when you want the loop to execute a specific number of times

example:
total <- 0
FOR i <- 1 TO 7
dailyTemp <- USERINPUT
total <- total + dailyTemp
ENDFOR
averageWeeksTemp <- total/7
OUTPUT averageWeeksTemp

i starts at 1 and goes up to 7 adding one every time the loop is completed. loop takes place 7 times

44
Q

what do WHILE loops do?

A

used when you want the loop to be executed WHILE a certain condition is true

example:
OUTPUT “please enter password:”
password <- USERINPUT
WHILE password != “abc123”
OUTPUT “invalid password - try again”
password <- USERINPUT
ENDWHILE
OUTPUT “correct password”

it checks the condition each time and performs the iteration if the condition is true

45
Q

what does a REPEAT..UNTIL loop do?

A

used when you want to execute a loop until a certain condition is true. the loop will always execute once because the condition is tested at the end of the loop

example:
password <- “”
REPEAT
OUTPUT “invalid password - try again”
password <- USERINPUT
UNTIL password = “abc123”
OUTPUT “correct password”

46
Q

what is an array?

A

a data structure that allows you to hold many different items of data that is referenced by one identifier - all items of an array must be the same data type

example: an array of 10 integers called userNames could be declared as array userNames[10]

47
Q

how can information be red from or added to an array?

A

by using the indexes, which start at 0

48
Q

how would you add an item to an array?

A

by referencing the specific index number

example:
array usernames[10]
usernames[0] <- “patel”
usernames[1] <- “smith”

49
Q

how do you find the length of an array?

A

LEN(name of array) which returns the number of items in the array

50
Q

can arrays be edited?

A

no, they have a fixed length meaning that items cannot be added.
- to add more items you would have to create a new, larger array and copy the previous items across

51
Q

does python have arrays?

A

no, python doesn’t have an array structure. instead, it uses lists which can alter their length

52
Q

how can a for loop be used to be used to output al the items in an array?

A

example:
FOR i <- 0 TO LEN(usernames)
OUTPUT username[i]
ENDFOR

53
Q

how can a linear search be conducted on an array?

A

example:
OUTPUT “type in username:”
search <- USERINPUT
FOR i <- 0 TO LEN(usernames)
IF usernames[i] = search THEN
OUTPUT “name found”
ENDIF
ENDFOR

54
Q

what are the advantages of using an array?

A
  • you can easily input, sort and print many items rather than having to use lots of different variable names
55
Q

what is a bubble sort?

A
  • each item in a list is compared to the next one to it and if greater, they swap places
  • at the end of the first pass the largest item is at the end and so forth
  • this is repeated until all the items are sorted + another pass takes place to verify that they are in order
56
Q

how do you swap items in an array?

A

example:
temp <- names[j]
names[j] <- names[j + 1]
names[j + 1] <- temp

57
Q

what are 2D arrays?

A

arrays but with rows and columns

array marks[2][4] - has 3 rows and 5 columns because indexes start at 0

58
Q

how do you identify an individual item in a 2D array?

A

example: mark[1][3]
that would output what is in row index 1 and column index 3

59
Q

what is a record?

A

a data structure that consists of different fields which can all be of different types

example:
RECORD film
filmID: integer
title: string
yearReleased: integer
ENDRECORD

60
Q

what is a field/element in a record?

A

a single item of data

61
Q

what is instantiation?

A

when new instances of the record structure can be made for each new set of fields. records can be edited

example:
film1 <- new film

film1.filmID <- 232
film1.title <- “top gun”
film1.yearReleased <- 2020

OUTPUT film1.title #which will output the film title

62
Q

does python have records?

A

no