Chap 8 - programing python - print, if/elif/else, (nested) loops, len, string manipulation, arrays Flashcards

1
Q

name 6 data types

A

-integer
-string
-boolean
-float
-character (char)
-date

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

how do you use the print function

A

print(“ “)

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

2 ways you can print messages on print function

A

-comma
-concatenation (adding)

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

meaning of concatenation

A

join together

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

how do you use comma when printing

A

use comma between words

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

how do you use concatenation when printing

A

use ( + ) to add the strings

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

what is the difference between using comma and concatenation

A

space in comma, no space in concatenation

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

can you add strings

A

yes

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

can you add integers

A

no

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

what must you put when using \t and \n

A

quotations because they’re strings

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

what does \t do

A

a tab of space

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

what does \n do

A

a new line

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

what does = do

A

assignment of variable

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

what does != mean

A

not equal

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

3 rules for variables

A

-has to start with a char
-case sensitive
-no special char and spaces - willc hange program (except underscore)

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

arithmetic operation for
-addition
-subtraction
-multiply
-divide
-remainder
-power
-squared

A
  • +
  • -
  • *
  • / (float), // (integer)
  • %
  • **
  • **(1/2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

relational operation for:
-greater than
-greater than or equal
-smaller than
-smaller than or equal
-equal (Equality (eg, 2 obj, same 2 content))
-not equal (Equality (eg, 2 obj, same 2 content))

A
  • >
  • > =
  • <
  • <=
  • ==
  • !=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

logical operators

A
  • and
  • or
  • not
    eg. if not hungry:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

note: explain each thing when explaining assignment

A

eg. c = a + b
- c: variable
- +: addition of a and b
- =: assignment of variable

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

equality vs identity vs memebership

A

equality - checks if obj has same contents (2 obj, same 2 content)
identity - check if obj are the same by refer back to same memory (2 obj, 1 content)
membership - check if value exists in a sequence

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

what does = & == do

A

= - assignment of variables
== - comparison

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

how do you find input for diff data types

A

str(input(“ “))
int(input(“ “))
float(input(“ “))

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

1)can integer be stored in float, why
2)can float be stored in integer, why

A

1)yes, integer requires less memory than float
2)no, float requires more memory than integer

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

what is the drawback of integer and float stored as string

A

they’ll be used as a string - lose their properties (can’t do mathematical operations on them)

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

1)can integer and float be stored in string
2)can a string be an integer/ float

A

1)yes
2)no

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

how do you use if, elif, else conditional statement

A

if ( ):
, start here
-indentation
elif ( ):
, start here
-indentation
else:
, start here
-indentation

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

how do you use nested ifs

A

if ( ):
if ( ):
-indentation

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

how do you use and & or conditions in conditional statements

A

eg. if ( age> 10 and age< 20):
eg. if ( age> 10 or age< 20):

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

how do you use conditional statement to get something in between value

A

eg. if (0 < username <10):

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

nota: % - remainder, // - integer, / - float (for division)

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

note: all chars are strings, not all strings are chars

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

Iteration

A

looping

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

why are looped required

A

use same algorithm for diff. inputs

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

types of loops

A

-for
-while

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

basic structure of loops

A

-initializer- variable which controls if loop is done or not
-action
-stopping condition
-updating counter/ no. loops

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

how to write for loop

A

-for i in range (__,__,_if you want__):
-for i in (variable/ list/ str)
-third one changes the steps between the loop
-don’t need to write iterating variable at start
(i changed to elements in variable/ list/ str)
(auto counter update)

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

how to update counter

A

i = i+1
i +=1

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

how to write while loop

A

i = 0
while (____):
i +=1
(manuel counter update)

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

how does range function work

A

eg, (0, 5) - no.s - 0, 1, 2, 3, 4, - no 5
(0, 20, 2) - no.s 0-19, with 2 step btw (eg, 0, 2, 4…)
(variable/ list/ str) = no.s in that variable, list

40
Q

diff btw for & while loops

A

-for - knows how many times to run = count controlled
-while - checks iterating value before running = precondition

41
Q

types of loop control statements

A

-break
-continue
-pass

42
Q

how to use break loop control

A

-write break within loop
-stop loop
-if used in nested loop, it stop the inner most loop

43
Q

how to use pass loop control

A

-write pass within loop
-acts as a placeholder and doesn’t print the i for that loop

44
Q

how to use continue loop control

A

-write continue within loop
-skips current iteration and jumps to next loop

45
Q

how to write loop/if/elif with a list as its range

A

n= [“A”, “B”]
-for i in n:
-while i in n:
-if variable[i] in n:
note: i changes to elements in list, list can be any data type

46
Q

index values in list

A

-all char has an index value
-first element = 0 - positive
-last element = -1 - negative
-space also has an index value

47
Q

note: space is considered a char so has an index value

A
48
Q

how to use len function

A

-finds no. elements in a list - list can be of any data types
n = [1,2,3]
length = len(n)
print(length)

49
Q

how to extract a certain char from a list

A

num = [1,2,3,4]
- print(num[ 0 ]) = 1
-print(num[ 0: ]) = 1, 2, 3, 4
-print(num[ 0:2]) = 1,2 (index after : not included)
-print(num[:2]) = 1,2 (index after : not included)
-print(num[-1]) = 4
-print(num[i:i+ n ]) = value with i index & n no.s after it

50
Q

diff btw in range of loops -

A

-for i in range (10, lengthData+10):
= i starts from 10th index - if range i beyond elements in list = index out of range error
-for i in range (0, lengthData):
= i starts from 0th index

51
Q

how to print i in a single line in a loop

A

print( i, end=” “)

52
Q

how to use match case statements

A

match (variable):
case “something”:
action
case _:
action (only run when none of others match)
-use like if statements

53
Q

define array

A

-a sequence of elements of the same data type

54
Q

what the the index value of first element called

A

lower bound

55
Q

what the the index value of last element called

A

upper bound

56
Q

how do you create a blank 1D array

A

-reserving memory in ram
-variable = [ None ] * (number of times)

57
Q

how to use blank array and string manipulation together

A

-variable [index number] = “ “
-for i in range ( , ):
variable [i] = ______
-while i < ___:
variable [i] = ______

58
Q

note: there are index values for each element in an array

A
59
Q

2 types of searching algorithms

A

-linear search
-binary search

60
Q

1 type of sorting algorithm

A

-bubble sort

61
Q

how does linear search work & its time

A

-compare target value with elements from left to right until they match (can be sorted or unsorted list)
-time: 0(n)

62
Q

advantages and disadvantages of linear search

A

advantages:
-fast for small to medium data sets
-works for sorted & unsorted sets
-good for data structures with no random access
disadvantages:
-slow for large data sets

63
Q

how does binary search work & its time

A

-check if target value if equal, bigger or smaller than the element in the middle
-eg, it’s smaller so all the elements smaller than the mid element and the mid element are left out in the next search
-continue until target value is found
-time: 0 (log n)

64
Q

advantages and disadvantages of binary search

A

advantages:
-quick
-good for large data sets
disadvantages:
-needs to be sorted

65
Q

how does bubble sort work & its time

A

-start with 1 chosen element
-compares adjacent elements to the chosen element and swaps them if needed until it is no longer needed
-time: 0 (n^2)

66
Q

advantages and disadvantages of bubble sort

A

disadvantages:
-bad for large data sets

67
Q

how to do nested for loops

A

-2 counters for inner and out loops
-outer loop only loops after inner loop is finished
eg. for j in range (0,3):
for i in range (0,2):
action

68
Q

what variable can you use for bubble sort

A

a temporary variable for the element swapping process

69
Q

Code for bubble sort

A

n = len(array)
for i in range(n - 1):
for j in range(n - i - 1):
if array[ j ] > array[ j + 1 ]:
temp = array[ j ]
array[ j ] = array[ j + 1]
array[ j + 1] = temp

70
Q

code for linear search

A

for i in range (len(array)):
if array[ i ] == number:
print(“number found”)

71
Q

code for sum of reach row in 2D array

A

for row in range ( , ):
sum = 0
for column in range( , ):
sum += array[row] [column]
print(sum)

72
Q

code to put highest/ lowest/ avg value from 2D array in 1D array

A

highest_stores = [0 for i in range( )]
salary = [[0 for c in range( )] for r in range( )]
for r in range ( , ):
highest_salary = salary[r] [c]
for c in range ( , ):
if salary[r] [c] > highest_salary:
highest_salary = salary[r] [c]
highest_stores[r] = highest_salary
print(“Highest salary in”, r, “is”, highest_salary[r])
-declare the first element in each row as highest salary & compare it with others
-if others are bigger, highest salary is replaced by it

73
Q

how to swap elements without a temporary variable

A

-ages [i], ages [i+1] = ages [i+1], ages [i]
1 2 2 1
Left to right:
1 goes to 2
2 goes to 1

74
Q

How does a post test loop work (not used in python)

A

-initialize loop counter
-execute action
-update counter
-check condition
-if condition met, stop
else repeat

75
Q

how does a pre test loop work

A

-initialize loop counter
-check condition
if condition met:
-execute action
-update counter

76
Q

note: arrays cannot be implemented on python so just think of them like list with 1 data type

A
77
Q

what is a 2D list

A

a list made up of lists
eg.
meats = [“chicken”, “fish”, “turkey”]
groceries = [fruits, groceries, meats]

78
Q

how to use a 2D list with variables to print row or element

A

for a row:
-print(groceries [row])
for a single element:
-print(groceries [row] [column])

-each list is a row, each element is in a column (like a table)

79
Q

define traversing

A

accessing individual elements in 2D array

80
Q

how to traverse

A

groceries = [[“apple”, “orange”, “banana”, “coconut”],
[“celery”, “carrots”, “potatoes”],
[“chicken”, “fish”, “turkey”]]
-for j in groceries:
for i in j:
print(i)
-for row in range ( , ):
for column in range ( , ):
print(groceries [row] [column])
-finish inner loop before changing outer loop
row, column:
0, 0
0, 1
1, 0
1, 1

81
Q

what does end = “ “ do

A

rearranges the printed word in a single line

82
Q

note: for loop doesn’t check condition, only runs it

A
83
Q

pre- condition loop in pseudocode

A

while loop

84
Q

post- condition loop in pseudocode

A

REPEAT

85
Q

what is size of array

A

number of elements

86
Q

how to randomly choose an element

A

import random
random.choice( )
random.randint( ) / random.randstr ( ) / random.randfloat ( )

87
Q

how to add time

A

import time
time.sleep( )
-in seconds

88
Q

what happens if index is too big

A

index out of range error

89
Q

how to create empty 2D array

A

array = [ [ 0 for column in range ( , )] for row in range ( , )]
-can replace 0 with None or “ string” to fill in the blank array

90
Q

order of row & column in 2D array when:
1)creating array
2)printing array
3)traversing
mark row = 1, column = 2

A

1) column, row 2, 1
2) row, column 1, 2
3) row, column 1, 2

91
Q

what can you do with traversing

A

-print each elements
-add data into 2D array eg. input or adding

92
Q

difference between for i in range ( , ) & for i in array when comparing & printing Found/ Not found

A

for i in range ( , ):
-print Found/ Not found as many times as range
for i in array:
-print Found/ Not found 1 time

93
Q

note:
before making 2D array - make a table look alike of it
mark row = 1, column = 2

A
94
Q

difference btw 1D & 2D array in terms of memory

A

1D - data is stored randomly in memory cell = more comparisons to find them
2D - data is stored consecutively in memory cell = less comparisons to find them

95
Q

help for arrays:
https://docs.google.com/document/d/1b5Tt-2W5yVyTZYWSwUQk3DXtFnfM_McnkaAZX1FV1QQ/edit?tab=t.0#heading=h.isd4beaopm41

A
96
Q

note:
for loop - limited running
while - unlimited running

A