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

operation for identity true

A

is
-Identity (eg, 2 obj, 1 content) - refer back to same memory

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

operation for identity false

A

is not
-Identity (eg, 2 obj, 1 content) - refer back to same memory

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

operation for membership true

A

in
-Membership - check if value exists in a sequence

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

operation for membership false

A

not in
-Membership - check if value exists in a sequence

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

which takes place first: maths operations or assignment of variable

A

maths operations

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

what does = & == do

A

= - assignment of variables
== - comparison

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
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
28
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
29
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
30
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
31
Q

how do you use if conditional statement

A

if ( ):
, start here
-indentation

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

how do you use elif conditional statement

A

elif ( ):
, start here
-indentation

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

how do you use else conditional statement

A

else:
, start here
-indentation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
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
35
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
36
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
37
Q

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

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

Iteration

A

looping

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

why are looped required

A

use same algorithm for diff. inputs

41
Q

types of loops

A

-for
-while

42
Q

basic structure of loops

A

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

43
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)

44
Q

how to update counter

A

i = i+1
i +=1

45
Q

how to write while loop

A

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

46
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

47
Q

diff btw for & while loops

A

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

48
Q

types of loop control statements

A

-break
-continue
-pass

49
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

50
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

51
Q

how to use continue loop control

A

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

52
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

53
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

54
Q

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

A
55
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)

56
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

57
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

58
Q

how to print i in a single line in a loop

A

print( i, end=” “)

59
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

60
Q

define array

A

-a sequence of elements of the same data type

61
Q

what the the index value of first element called

A

lower bound

62
Q

what the the index value of last element called

A

upper bound

63
Q

how do you create a blank 1D array

A

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

64
Q

how to use blank array and string manipulation together

A

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

65
Q

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

A
66
Q

2 types of searching algorithms

A

-linear search
-binary search

67
Q

1 type of sorting algorithm

A

-bubble sort

68
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)

69
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

70
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)

71
Q

advantages and disadvantages of binary search

A

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

72
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)

73
Q

advantages and disadvantages of bubble sort

A

disadvantages:
-bad for large data sets

74
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

75
Q

what variable can you use for bubble sort

A

a temporary variable for the element swapping process

76
Q

how to make the bubble sort more efficient

A

-remove the already sorted elements
- eg. for j in range (0, len(variable) -1):
(outer loop)

77
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

78
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

79
Q

how does a pre test loop work

A

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

80
Q

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

A
81
Q

what is a 2D list

A

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

82
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)

83
Q

define traversing

A

accessing individual elements in 2D array

84
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

85
Q

what does end = “ “ do

A

rearranges the printed word in a single line

86
Q

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

A
87
Q

pre- condition loop in pseudocode

A

while loop

88
Q

post- condition loop in pseudocode

A

REPEAT

89
Q

what is size of array

A

number of elements

90
Q

how to randomly choose an element

A

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

91
Q

how to add time

A

import time
time.sleep( )
-in seconds

92
Q

what happens if index is too big

A

index out of range error

93
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

94
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

95
Q

what can you do with traversing

A

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

95
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

96
Q

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

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

98
Q

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

A