Chap 8 - programing (python) - conditions, loops, string manipulation, arrays Flashcards

(50 cards)

1
Q

how do you find input for diff data types in python

A

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

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

2 ways you can print multiple messages on print function

A

-comma btw words
-concatenation (adding) = join tgt only strings using +
(space in comma, no space in concatenation)

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

how to comment on code

A

must be added for each line

do a tic tac toe symbol, like a hash for each line

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

data types in python

A

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

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

features of data types

A

-all chars are strings, not all strings are chars
-integer be stored in float because it requires less memory than float. it cannot work the other way around
-integer & float can be stored as string but lose their properties = can’t do mathematical operations on them, string cannot be stored in integer or float

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

arithmetic operation for
-addition
-subtraction
-multiply
-divide
-power
-squared
-remainder
-rounding
-give random int

A
  • +
  • -
  • *
  • / (float), // (integer)
  • **
  • **(1/2)
  • %
    -round.(num, decimal place)
    -import random
    random ()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

difference btw = & ==

A

= - assignment of variable
== - comparison

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

logical operators for:
-greater than
-greater than or equal
-smaller than
-smaller than or equal
-equal (comparison)
-not equal (comparison)

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

Boolean operators in python

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

if, elif, else conditional statement

A

if ( ):
action
elif ( ):
action
else:
action

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

nested ifs

A

if ( ):
if ( ):

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

note: for loops in python, last value is not included
eg. 0 TO 4 = 4 runs
so always start with 0

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

for loop

A

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

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

how does range function work

A

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

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

while loop

A

i = 0
while i< 10:
action
i +=1
-initialize iterating variable at start
(manual counter update)

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

update counter

A

i = i+1
i +=1

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

how to do nested for loops

A

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

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

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

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

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

finding length of characters

A

-len function
len(“hello”)
len(poop)

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

finding substring of characters

A

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

22
Q

uppercasing all string

A

“hello”.upper( )
poop.upper( )

23
Q

lowercasing all string

A

“hello”.lower( )
poop.lower( )

24
Q

create a blank 1D array

A

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

25
note: before making 2D array - make a table look alike of it mark row = 1, column = 2
26
order of row & column in 2D array when: 1)creating array 2)printing array 3)traversing mark row = 1, column = 2
1) column, row 2, 1 2) [row] [column] 1, 2 3) [row] [column] 1, 2
27
how to create empty 2D array
array = [ [ 0 for column in range ( , )] for row in range ( , )] -can replace 0 with None or " string" to fill in the blank array
28
how to use a 2D list with variables to print row or element
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)
29
functions - returns value
def functionName ( ): return action -calling function with arguments print(functionName (value, value)) print(functionName (variable, variable))
30
code for totaling
total = 0 for i in range (0,size): mark = int)input("what is mark?)) total = total + mark
31
code for counting
count = 0 for i in range (0,size): mark = int(input('what is marl?)) count = count + 1
32
code for min, max, avg
total = 0 maxMark = 0 minMark = 100 for i in range (0,size): mark = int(input("what is mark")) if mark> maxMark: maxMark = mark if mark< minMark: minMark = mark total = total + mark avg = total/size -set initial min value as highest value -set initial max value as lowest value
33
code for linear search
for i in range (0, len(array)): if array[ i ] == number: print("number found")
34
Code for bubble sort
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
35
swap elements with a temporary variable
a = 5 b = 10 temp = 0 ------------------------ temp = a a = b b = temp
36
swap elements without a temporary variable
-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
37
code for traversing - accessing individual elements in 2D array
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
38
code for sum of reach row in 2D array
for row in range ( , ): sum = 0 for column in range( , ): sum = sum + array[row] [column] print(sum)
39
code to put highest/ lowest/ avg value from 2D array in 1D array
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
40
built in functions
1) len(_list_) - no. of element in list 2) variable.upper ( ) - all string will be uppercase 3) variable.lower ( ) - all string will be lowercase 4) pow (base, exponent) 5) round (number, decimal place) 6) import random random.choice( ) random.randint( ) / random.randstr ( ) / random.randfloat ( ) 7) import time time.sleep( )
41
rules for variables
-has to start with a character -no special char and spaces - will change program (except underscore) -case sensitive Poop & poop are 2 diff variables
42
what does "\t" do
a tab of space - need quotations cuz string
43
what does "\n" do
a new line - need quotations cuz string
44
match case statements
match (variable): case "something": action case _: action (only run when none of others match) -use like if statements
45
error in range of loops
-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
46
what happens if index is too big
index out of range error
47
break loop control
-write break within loop -stop loop -if used in nested loop, it stop the inner most loop
48
how to randomly choose an element
import random random.choice( ) random.randint(0,5) - returns random no. btw 1 & 5
49
how to add time
import time time.sleep( ) -in seconds
50
difference between for i in range ( , ) & for i in array when comparing & printing Found/ Not found
for i in range ( , ): -print Found/ Not found as many times as range for i in array: -print Found/ Not found 1 time