1/34
Looks like no tags are added yet.

What prints?
big \n really big
why is a function returning “none”?
It never reaches a “return” statement!
Either there never was one OR you have an infinite loop!
random.uniform(a,b)
a and b must be integers
outputs a random float in between a and b
MUST import random
random.random()
returns a random float between 0 and 1, including 0 excluding 1
MUST import random
DON’T FORGET () for any method
random.randint(a,b)
returns a random integer in between a and b, inclusive
MUST import random
isinstance(object,type)
checks if the object is of type “type”
returns a boolean
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!!!!

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!!

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)
What math operations are you allowed to apply to strings?
* (ex. Print("a"5) prints "hello" 5 times
+(ex print("a"+"b") returns "ab"
How does indexing work?
format: stringname[index]
![<p>format: stringname[index]</p>](https://knowt-user-attachments.s3.amazonaws.com/b28c0202-79dc-4a98-8b41-84443a24ec05.png)
How does string slicing work?
format: stringname[start point:one after endpoint]
![<p>format: stringname[start point:one after endpoint]</p>](https://knowt-user-attachments.s3.amazonaws.com/d09c04d7-084b-49ab-adc9-97972882eba6.png)
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>](https://knowt-user-attachments.s3.amazonaws.com/397c930c-96d4-4a26-ae5b-92b1093f4f00.png)
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.

what does dir(object) do? ex. dir(str)
Gives you a list of all the methods associated with that object
string_name.upper()
DO NOT FORGET () FOR METHODS
returns the string in all upper case

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
string_name.lower()
makes the whole string lowercase. DO NOT FORGET () for methods!!!
“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”)

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

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

How would you capitalize just the first letter of a string called “name”?
name.capitalize()
DO NOT FORGET () FOR METHODS
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!
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

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
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:
Python compares the first characters. If they are different, then whichever string has the bigger first character is the bigger string
If they are the same, python moves onto the next character until one is different
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!
How do you open a file called “abc” in each mode?
myfile = open(‘abc’,’r’)
myfile = open(‘abc’,’w’)
myfile = open(‘abc’,’a’)
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…..
how would you print the contents of a file called ‘abc’?
myfile = open(‘abc’,’r’)
contents = myfile.read()
print(contents)
myfile.close()
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
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
how to do absolute values
abs(-6)
abs(5)
abs(-4.56)
no need to import math! works on floats and int
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!
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!
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