APS106 Term Test 1

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

1/34

encourage image

There's no tags or description

Looks like no tags are added yet.

35 Terms

1
New cards
<p>What prints?</p>

What prints?

big \n really big

2
New cards

why is a function returning “none”?

It never reaches a “return” statement!

Either there never was one OR you have an infinite loop!

3
New cards

random.uniform(a,b)

  • a and b must be integers

  • outputs a random float in between a and b

  • MUST import random


4
New cards

random.random()

returns a random float between 0 and 1, including 0 excluding 1

  • MUST import random

  • DON’T FORGET () for any method


5
New cards

random.randint(a,b)

  • returns a random integer in between a and b, inclusive

  • MUST import random


6
New cards

isinstance(object,type)


checks if the object is of type “type”

  • returns a boolean


7
New cards

how to append a list called fruits_list with the string “apple”?

fruits_list.append(“apple”) NOTE: FOR LISTS, doing this changes the actual list, so no need to create a new list!!!!


<p>fruits_list.append(“apple”) NOTE: FOR LISTS, doing this changes the actual list, so no need to create a new list!!!!</p><p></p>
8
New cards

how would you remove “apple” from a list called fruits_list?

fruits_list.remove(“apple”) NOTE: FOR LISTS, doing this changes the actual list, so no need to create a new list!!!!

NOTE: this only removes the first instance of apple!!!! You would need a loop to remove all of them!!

<p>fruits_list.remove(“apple”) NOTE: FOR LISTS, doing this changes the actual list, so no need to create a new list!!!!</p><p>NOTE: this only removes the first instance of apple!!!! You would need a loop to remove all of them!!</p>
9
New cards

how would you find the length of a string called mystring (number of characters)? What type would the output be?

len(mystring) (returns an int)

10
New cards

What math operations are you allowed to apply to strings?

* (ex. Print("a"5) prints "hello" 5 times

+(ex print("a"+"b") returns "ab"

11
New cards

How does indexing work?

format: stringname[index]

<p>format: stringname[index]</p>
12
New cards

How does string slicing work?

format: stringname[start point:one after endpoint]

<p>format: stringname[start point:one after endpoint]</p>
13
New cards

How would you extract every 3d character of a string, starting from the beginning?

string[::3]

  • since we are starting at the beginning and ending at the end, we leave the start and end blank

  • our step size is 3

  • note: default is [beginning:end:1]

  • format is string[start:end:stepsize]


<p>string[::3]</p><ul><li><p>since we are starting at the beginning and ending at the end, we leave the start and end blank</p></li><li><p>our step size is 3</p></li><li><p>note: default is [beginning:end:1]</p></li><li><p>format is string[start:end:stepsize]</p></li></ul><p></p>
14
New cards

What do you have to remember when trying to change a string?

Indexing/slicing/ANY MODIFICATION that you do to a string does NOT change the original string! You need to save it as a new string variable.

<p>Indexing/slicing/ANY MODIFICATION that you do to a string does NOT change the original string! You need to save it as a new string variable.</p>
15
New cards

what does dir(object) do? ex. dir(str)

Gives you a list of all the methods associated with that object

16
New cards

string_name.upper()

  • DO NOT FORGET () FOR METHODS

  • returns the string in all upper case


<ul><li><p>DO NOT FORGET () FOR METHODS</p></li><li><p>returns the string in all upper case</p></li></ul><p></p>
17
New cards

What happens if you wrote string_name.upper instead of string_name.upper() (you left out the empty parantheses)?

It would just tell you what it does. The parantheses are what tells python that it needs to run that method

18
New cards

string_name.lower()

makes the whole string lowercase. DO NOT FORGET () for methods!!!

19
New cards

“find” and “rfind” string methods

Find: returns the FIRST index where the string you put into it can be found

  • format: string_name.find(“string you are looking for”)

Rfind: returns the LAST index where the string can be found (reverse find?)

  • format: string_name.rfind(“string you are looking for”)


<p>Find: returns the FIRST index where the string you put into it can be found</p><ul><li><p>format: string_name.find(“string you are looking for”)</p></li></ul><p>Rfind: returns the LAST index where the string can be found (reverse find?)</p><ul><li><p>format: string_name.rfind(“string you are looking for”)</p></li></ul><p></p>
20
New cards

how would you replace the “Anna” with “Benjamin” in a string called “greeting”?

greeting.replace(“Anna”,”Benjamin”)

<p>greeting.replace(“Anna”,”Benjamin”)</p>
21
New cards

Chaining methods

  • What is it?

  • How do you do it?

  • Which method gets executed first?


  • Lets you use two methods at once on a string

    • first method is applied at this returns an object

    • second method is then applied to this object

  • format: string_name.method1().method2()

  • methods are executed from left to right


<ul><li><p>Lets you use two methods at once on a string</p><ul><li><p>first method is applied at this returns an object</p></li><li><p>second method is then applied to this object </p></li></ul></li><li><p>format: string_name.method1().method2()</p></li><li><p>methods are executed from left to right</p></li></ul><p></p>
22
New cards

How would you capitalize just the first letter of a string called “name”?

name.capitalize()

  • DO NOT FORGET () FOR METHODS


23
New cards

What if the first character of a string that you string.capitalize() is not a letter?

Then this method does nothing. even if the first character is a space!

24
New cards

How does string.split(“string”) work?

It splits your string every time it encounters the string in quotes

NOTE: don’t use string.strip(char) it is confusing and unnecessary for this test

<p>It splits your string every time it encounters the string in quotes</p><p>NOTE: don’t use string.strip(char) it is confusing and unnecessary for this test</p>
25
New cards

Ascii table:

  • What has a higher order: a or z?

  • What has a higher order: a or A?

  • What has a higher order: a or Z?


z>a

a>A

a>Z


Overall:

A has an order of 65, and then every capital letter is one more up to Z at 90

a has an order of 97, and every lowercase letter is one more up to z at 122

All you need:

  • every lowercase is 32 more than its uppercase equivalent

  • every lowercase is more than every uppercase

  • as you move up the alphabet it goes up


26
New cards

How are strings compared, and what will each of the following return:

a) ‘ab’>’ZZZ’

b) ‘Zaaaaa’>’a”

c) ‘ab’>’abc’

a) True

b) False)

c) False

How they are compared:

  1. Python compares the first characters. If they are different, then whichever string has the bigger first character is the bigger string

  2. If they are the same, python moves onto the next character until one is different

  3. The only case when length matters is something like ab vs abc: first and second characters are equal, so abc must be bigger because it has that extra c!


27
New cards

How do you open a file called “abc” in each mode?

myfile = open(‘abc’,’r’)

myfile = open(‘abc’,’w’)

myfile = open(‘abc’,’a’)

28
New cards

All steps to write “hello” in a not blank file called “abc” without deleting everything

myfile = open(‘abc’,’a’)

myfile.write("\nhello”)

myfile.close()

IF YOU FORGET THOSE BRACKETS AGAIN…..

29
New cards

how would you print the contents of a file called ‘abc’?

myfile = open(‘abc’,’r’)

contents = myfile.read()

print(contents)

myfile.close()

30
New cards

What’s wrong with this “code”: (NOTE: ignore the periods knownt wont let me tab or add a bunch of spaces that’s just the indent)

sky = blue

If sky == blue:

…….print(“hi”)

Else:

……print(“dang it”)

  • if/elif/else HAVE to be lowercase! this is easy to catch when coding, NOT on a handwritten test. LOWERCASE!!!!

  • also i did it properly here but remember == gives you a boolean which you want, = doesn’t make sense here


31
New cards

You want to check if a string is in another string. How do you do that?

if “smallstring” in “bigstring”:

NOTE: do NOT say if “smallstring” is in “bigstring” that is WRONG no “is”!

You can do

if “smallstring” not in “bigstring

though that’s fine

32
New cards

how to do absolute values

abs(-6)

abs(5)

abs(-4.56)

no need to import math! works on floats and int

33
New cards

you are trying to write to a file using myfile.write(). what object types does this method accept?

ONLY strings. You cannot do myfile.write(1.34) or myfile.write(4). You NEED to do myfile.write(str(1.34)) and then it works!

34
New cards

string = “1,2,3”

a) what does string.find(“,”) return

b) what does string[3:].find(“,”) return (and why)

a) returns the index of the FIRST comma, which is 1

b) first, only looks at the string from index 3 onwards. basically this is “,3”. Of this, it returns the index of the first comma, which is zero!

35
New cards

you have two commas in a string and you need to find the index of both of them. how do you do that, and what does each function return?

string.find(“,”) returns the index of the first comma, starting from the left

string.rfind(“,”) returns the index of the last comma, STILL STARTING FROM THE LEFT