Test 1 Review (Intro to Computing)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/21

encourage image

There's no tags or description

Looks like no tags are added yet.

22 Terms

1
New cards

Common For Loop Formats

  • for i in range(stop, start, step)

  • for i in range(stop, start)

  • for i in range(stop)


2
New cards

String Iteration Using For Loops

  • Format?

  • Function


Format: for character in “string”:

Function: loops through every character in a string

3
New cards

When should you use while loops? How do you avoid creating an infinite loop?

  • When you want to a loop to run as long as a condition is True.

    • Ex: run this loop while counter < 1

  • Make sure the condition eventually becomes False to avoid an infinite loop.


4
New cards

True or False: You can usually rewrite for loops to be while loops.

True

5
New cards

If/Elif/Else

  • Indention matters! Make sure the code controlled by an if, elif, or else structure is appropriately indented under that structure!


If/Elif/Else

  • Indention matters! Make sure the code controlled by an if, elif, or else structure is appropriately indented under that structure!


6
New cards

True or False: Once a condition is met, it will not check the other elif(s) or else statements attached to it.


True. When an if statement is executed, any elif(s) or else structures attached to it will not run.

  • Ex:

    • a = 5

    • if a == 5:

      • print(“a = 5”)

    • elif b == 4:

      • print(“b = 4”)

    • else:

      • print(“if and elif didn’t run”)

Output:

  • a = 5


7
New cards

The initial condition you are checking. If met, it will run the code indented under it.

If

8
New cards

another condition that you are checking given that “If” was False

Elif

9
New cards

The code that runs given that none of the other conditions are true.

Else

10
New cards

Do these data conversions always work or only sometimes work?

  • Anything —> String

  • Anything —> Bool

  • Bool —> Anything

  • String —> Int

  • String —> Float

  • Float —> Int

  • Int —> Float


  • Anything —> String (str())

    • Always

  • Anything —> Bool (bool())

    • Always

    • ““, 0, & 0.0 —> False

    • Anything else is True

  • Bool —> Anything

    • Always

    • str(True) —> “True”, str(False) —> “False”

    • float(True) —> 1.0, float(False) —> 0.0

    • int(True) —> 1, int(False) —> 0

  • String —> Int

    • Sometimes, string must be only numbers w/ no decimal points

    • int(“1.0”) —> ValueError, int(“Dog”) —> ValueError

  • String —> Float

    • Sometimes, string must be only #s and have at most 1 decimal point

    • float(“Dog”) —> ValueError

  • Float —> Int

    • Always, rounds down to nearest int

    • int(-2.8) —> -2, int(1.4) —> 4, int(1.8) —> 1

  • Int —> Float

    • Always, adds .0 to end of the int

    • float(2) —> 2.0


11
New cards

Logic Operations: Both conditions must be true

And

  • (T & T —> T); (F & F —> F); (T & F —> F)


12
New cards

Logic Operations: Either condition must be true

Or

  • (T or T —> T); (F or T —> T); (F or F —> F)


13
New cards

“Is equal to”

==

14
New cards

“Not equal to”

!=

15
New cards

Which circumstances elicit a NameError?

using a variable that is undefined within its scope

16
New cards

Which circumstances elicit a TypeError?

using a variable in an inappropriate operation

  • aString = “hello” / print(aString + 2)

  • print(“Hello” / 2)


17
New cards

Which circumstances elicit a ZeroDivisionError?

Trying to divide by 0

  • print(len(variable/name) / 0)

  • print(0 / 0)


18
New cards

Which circumstances elicit a ValueError?

giving a method an inappropriate value

  • print(int(“Five"))


19
New cards

Describe Error Handling Format

try:

#Code to run goes here

except ValueError:

#Code to run if there is a Value Error

except ZeroDivisionError:

#Code to run if there is a ZeroDivisionError

except:

#Code to run if there are ANY other errors

else:

#Code to run if there are NO errors

finally:

#Code to run no matter what


20
New cards

Rewrite this for loop as a while loop:

  • for i in range(0, 11):

print(i)

i = 0

while i < 11:

  • print(i)

  • i += 1


21
New cards

Write the output of each line of code?

  • adding_function(1, 2)

  • print(adding_function(1, 2))

  • added_variable = adding_function(1, 2)


  • adding_function(1, 2) —> returns 3, but does nothing w/ it here

  • print(adding_function(1, 2)) —> prints 3 to the console

  • added_variable = adding_function(1, 2) —> gives added_variable the value 3


22
New cards

Important: don’t forget the difference between returning and

printing within a method!

  • What is Returning?


the value that calling the function will give