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 strings and integers

A

no

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

what data types are \t and \n

A

strings

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

what must you put when using \t and \n

A

quotations

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

what does \n do

A

a new line

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

what does = do

A

assignment of variable

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

what does != mean

A

not equal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
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
17
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
18
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
19
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
20
Q

bitwise operators for:
-bitwise AND
-bitwise OR
-bitwise NOT
-bitwise XOR
-bitwise left shift
-bitwise right shift

A
  • &
  • |
  • ~
  • «
  • > >
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
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
23
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
24
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
25
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
26
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
27
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
28
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
29
Q

what does = do

A

assignment of variables

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

how do you find input for string

A

str(input(“ “))

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

how do you find input for integer

A

int(input(“ “))

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

how do you find input for float

A

float(input(“ “))

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

can integer be stored in float, why

A

-yes
-integer requires less memory than float

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

can float be stored in integer, why

A

-no
-float requires more memory than integer

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

what is the drawback of integer and float stored as string

A

they’ll be used as a string - lose their properties kinda

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

can integer and float be stored in string

A

yes

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

can a string be an integer

A

no

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

can a string be a float

A

-yes
-using float( )

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

how do you use if conditional statement

A

if ( ):
, start here

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

how do you use elif conditional statement

A

elif ( ):
, start here

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

how do you use else conditional statement

A

else:
, start here

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

how do you use nested ifs’

A

if ( ):
if ( ):

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

how do you use and conditions in conditional statements

A

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

44
Q

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

A

eg. if (0 < username <10):

45
Q

how do you use or conditions in conditional statements

A

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

46
Q

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

A
47
Q

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

A
48
Q

note: can add and & or to conditions

A
49
Q

Iteration

A

looping

50
Q

why are looped required

A

use same algorithm for diff. inputs

51
Q

types of loops

A

-for
-while

52
Q

basic structure of loops

A

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

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

54
Q

how to update counter

A

i = i+1
i +=1

55
Q

how to write while loop

A

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

56
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

57
Q

diff btw for & while loops

A

-for is repeated a known no. times
-while is repeated for unknown no. times

58
Q

what is i called in loops

A

iterating variable

59
Q

types of loop control statements

A

-break
-continue
-pass

60
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

61
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

62
Q

how to use continue loop control

A

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

63
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:
-elif variable[i] in n:
note: i changes to elements in list, list can be any data type

64
Q

index values in list

A

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

65
Q

index values of char in list

A

-first char is 0
-last char is -1

66
Q

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

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

68
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

69
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 error
-for i in range (0, lengthData):
= i starts from 0th index

70
Q

how to print i in a single line in a loop

A

print( i, end=” “)

71
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

72
Q

define array

A

-a sequence of elements of the same data type

73
Q

what the the index value of first element called

A

lower bound

74
Q

what the the index value of last element called

A

upper bound

75
Q

how do you create a blank array

A

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

76
Q

how to use blank array and string manipulation together

A

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

77
Q

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

A
78
Q

2 types of searching algorithms

A

-linear search
-binary search

79
Q

1 type of sorting algorithm

A

-bubble sort

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

81
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

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

83
Q

advantages and disadvantages of binary search

A

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

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

85
Q

advantages and disadvantages of bubble sort

A

disadvantages:
-bad for large data sets

86
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

87
Q

what variable can you use for bubble sort

A

a temporary variable for the element swapping process

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

89
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

90
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

91
Q

how does a pre test loop work

A

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

92
Q

what is a for loop also called

A

count control loop

93
Q

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

A
94
Q

what is a 2D list

A

a list made up of lists
eg.
fruits = [“apple”, “orange”, “banana”, “coconut”]
groceries = [“celery”, “carrots”, “potatoes”]
meats = [“chicken”, “fish”, “turkey”]
groceries = [fruits, groceries, meats]

95
Q

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

A

eg.
fruits = [“apple”, “orange”, “banana”, “coconut”]
vegetables = [“celery”, “carrots”, “potatoes”]
meats = [“chicken”, “fish”, “turkey”]
groceries = [fruits, vegetables, meats]
for a row:
-print(groceries [row index])
for a single element:
-print(groceries [row index] [column index])

-if index is too big, will give “list index out of range”
-each list is a row, each element is a column (like a matrix)

96
Q

how to use a 2d list without variables using a for loop to print each row or everything

A

groceries = [[“apple”, “orange”, “banana”, “coconut”],
[“celery”, “carrots”, “potatoes”],
[“chicken”, “fish”, “turkey”]]
for a row:
-for i in groceries:
print(i)
for everything in a matrix:
-for j in groceries:
for i in j:
print(i, end=” “)
print( )
for everything in a single line:
-for j in groceries:
for i in j:
print(i)

97
Q

what does end = “ “ do

A

rearranges the printed word in a single line

98
Q

what happens when you print a list

A

the list is printed with the parentheses

99
Q

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

A
100
Q

pre- condition loop in pseudocode

A

while loop

101
Q

post- condition loop in pseudocode

A

REPEAT

102
Q

what is size of array

A

number of elements

103
Q

how to randomly choose an element

A

import random
random.choice( )

104
Q
A
104
Q
A
105
Q
A