CS 100 FINAL

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

min('atom', 'batman', 'superman')

'atom' : minimum operator - gives you smallest based on alphabetical order

justiceLeague = ['batman', 'wonderwoman', 'superman', 'flash', 'martian manhunter', 'hawkgirl'] justiceLeague[3]

'flash' : list indexing - first item is 0 then goes on 1, 2, 3......

s = 'high' t = 'school' s + ' ' + t

'high school'

s = 'high' t = 'school' s + t

'highschool'

sm = 'milesmorales' sm[1:9]

'ilesmora' : slicing operator : allows you to grab a portion of the string beginning with the first index number you provide and stops BEFORE the ending index number you provide - DOES NOT CHANGE ORIGINAL

justiceLeague = ['batman', 'wonderwoman', 'superman', 'flash', 'martian manhunter', 'hawkgirl'] justiceLeague[-2]

'martian manhunter' : list indexing - last item is -1 then goes on counting backwards -2, -3, -4......

(5 * 'na') + 'batman'

'nananananabatman' : n*s operator : copies string n many times - adding strings operator as well

infinitystones = ('power', 'mind', 'soul', 'time', 'reality', 'space') infinitystones[:-5]

('power',) : splicing operator - if no beginning is given default is the first item - and mind is not included because it is -5 and ending is not included it goes up to but not including -5

infinitystones = ('power', 'mind', 'soul', 'time', 'reality', 'space') infinitystones[-2:]

('reality', 'space') : splicing operator - begins at -2 and if no end is given default is the last item

y = (1, 2, 3, 4, 5) y + (9, 10, 3) print (y)

(1, 2, 3, 4, 5) : tuples cannot be changed

y = (1, 2, 3, 4, 5) z = y + (9, 10, 3) print (z)

(1, 2, 3, 4, 5, 9, 10, 3) : tuples can be added

for i in range (0,11,2) : print (i)

0 2 4 6 8 10 : range function - first number where you start second is where you go up to not including, third number what you are counting by, in this case counting by 2s

villians = ['mirror master', 'top', 'reverse flash', 'zoom', 'reverse flash', 'captain boomerang', 'reverse flash'] villians.count('reverse')

0 : count method - counts how many times a certain item appears in list - LOOKING FOR EXACT MATCHES ONLY

object constructors : a = int () a b = float () b c = str() c d = list () d e = tuple () e

0, 0.0, ' ' , [ ] , ( ) : object constructors : what makes an object a certain type, these constructors are used to indicate to python what exactly the item you are inputting is

for i in range (1,11) : print (i)

1 2 3 4 5 6 7 8 9 10 : range function - first number where you start second is where you go up to not including

max(4, 5, 6, 2, 8, 10)

10 : Maximum

len ('black widow')

11 : len operator - also counts space as a character

len('scarletwitch')

12 : len operator : length of string

marvel = [[3000, 12], 'ironman', 'starlord'] marvel[0][1]

12 : list indexing - gives you item, then next indexes item

3*5

15: Multiplication

4**2

16 : Exponent

14%3

2 : Division - gives you the remainder

min(4, 5, 6, 2, 8, 10)

2 : Minimum

villians = ['mirror master', 'top', 'reverse flash', 'zoom', 'reverse flash', 'captain boomerang', 'reverse flash'] villians.count('reverse flash')

3 : count method - counts how many times a certain item appears in list - LOOKING FOR EXACT MATCHES ONLY

myList = [[1,2], [3,4], [5,6]] myList[1:2][0][0]

3 : splicing operator : first gives you [[3,4]], [0] gives you [3,4], then [0] gives you 3 - breaking down nested list

floats

3.5 : decimal numbers

dc_cities = ['central city' , 'keystone', 'gotham city', ['metropolis', 'star city']] len (dc_cities)

4 : len operator - nested lists count as one item

14/3

4.666667 : division : gives you a float answer

abs(-4)

4: Absolute Value

14//3

4: Division - without remainder

int

5 : integer numbers

dc_cities = ['central city' , 'keystone', 'gotham city', 'metropolis', 'star city'] len (dc_cities)

5 : len operator - tells you amount of items in list

flash_speed = 356 if flash_speed < 300 : print ('run barry run')

: if statement : nothing gets printed because statement is not true so code is skipped

y = (1, 2, 3, 4) y + (5) print (y)

ERROR : cannot add a single value to a tuple must be at least two in parentheses because python just assumes you want that singular value

x = 123 x [0]

ERROR : cannot index int, would need to make x a str

'THOR' > 'thor'

False : Capital letters have lower value than lowercase letters

2==3

False : Is 2 equal to 3?

2>3

False : Is 2 greater than 3

2==3 and 2<=4

False : and operator - if one is false entire thing is false

x = 'superman' 'sm' in x

False : in operator - if 'sm' together are not in 'superman' it is false

defenders = ['dare devil', 'jessica jones', 'iron fist', 'luke cage'] 'luke' in defenders

False : in operator only looks for EXACT matches so because 'luke' alone does not appear it is false

not 3==3

False : not operator - if not true it is false

2==4 or 4==3

False : or operator - if both are false it is false

ironman = ['i', 'love', 'you', 3,0,0,0]

List : a comma-separated sequence of items enclosed in square brackets, can include numbers, strings, even other lists

days = ('Mon', 'Tues', 'Wed', 'Thu', 'Fri') for day in days : print (day)

Mon Tues Wed Thu Fri : loop - for each variable name assigned to the value of the first element of the sequence the indented code is executed and this happens for each element of the sequence - can loop through tuples

not (3==3 and 3==4)

True : (and operator if one is false it is false) not operator if not false it is true

2!=3

True : Is 2 NOT equal to 3

2<=3

True : Is 2 less than or equal to 3

'atom' < 'batman'

True : a is considered less than b because a is before b in the alphabet

2==2 and 2<=4

True : and operator - if both are true it is true

avengers = ['black widow', 'captain america', 'ironman', 'hulk', 'thor', 'hawkeye'] 'black widow' in avengers

True : in operator - asks if item (x) is in the list

s = 'batman' 'bat' in s

True : in operator - is 'bat' in 'batman'

'luke' in 'luke cage'

True : in strings it goes character by character, in lists it looks at items as a whole

not 4==3

True : not operator - if not false it is true

2==2 or 2<=4

True : or operator - if both are true it is true

2==3 or 2<=4

True : or operator - if one is true it is true

'atom' < 'atoms'

True : s makes it greater , comes in alphabet letter so considered bigger

pets = ['goose', 'krypto'] pets.pop() 'krypto' print (pets)

['goose'] : pop method : grabs last item for you and deletes it

villians = ['joker', 'posion ivy', 'riddler', 'penguin'] villians[1] = 'harley quinn' print (villians)

['joker', 'harley quinn', 'riddler', 'penguin'] : indexing operator - can change lists

infinitystones = ['power', 'mind', 'soul', 'time', 'reality', 'space'] infinitystones.sort() print (infinitystones)

['mind', 'power', 'reality', 'soul', 'space', 'time'] : sort method : sorts in increasing order of value - alphabetical

infinitystones = ['power', 'mind', 'soul', 'time', 'space', 'reality'] infinitystones.remove('space') print (infinitystones)

['power', 'mind', 'soul', 'time', 'reality'] : removes item from list

xmen = ['quicksliver' , 'wolverine', 'mystique'] avengers = ['blackwidow', 'captain america'] marvel_characters = xmen + avengers print (marvel_characters)

['quicksliver', 'wolverine', 'mystique', 'blackwidow', 'captain america'] : list concatenation (adding lists) - everything in first list plus second in the order they appear in the lists

infinitystones = ['power', 'mind', 'soul', 'time', 'reality'] infinitystones.reverse() print (infinitystones)

['reality', 'time', 'soul', 'mind', 'power'] : reverse method - reverses order of items

infinitystones = [ 'soul', 'time', 'mind', 'reality', 'space'] infinitystones.append('power') print (infinitystones)

['soul', 'time', 'mind', 'reality', 'space', 'power'] : append method - adds value to the end of the list

for i in range(10) : print (i)

[0,1,2,3,4,5,6,7,8,9] : range function - if you don't say a beginning it will start at 0 and go up to but not including last number

marvel = [[3000, 12], 'ironman', 'starlord'] marvel[0]

[3000, 12] : list indexing - gives you the item not specific character

myList = [[1,2], [3,4], [5,6]] myList[1:2]

[[3,4]] : splicing operator - two brackets puts it in a list - SPLICING A LIST ALWAYS RESULTS IN. A LIST

string

a sequence of characters enclosed within single or double quotes s = 'dog, cat'

t.bk(10)

back method - moves turtle back 10 units

t.circle(10)

circle method - draws a circle RADIUS 10

t.circle(30,120)

circle method - draws a circle of radius 30 but only goes to 120 degrees - gives you almost a semi circle

t.circle(30, 180, 10)

circle method - draws a circle of radius 30 goes to 180 degrees and uses only 10 lines while drawing - making it look choppy and less smooth

t.circle(-30)

circle method - draws a circle radius 30 IN OPPOSITE DIRECTION

temp = 40 if temp > 86 : print ('hot') if temp > 70 : print ('warm') if temp < 50 : print ('cold') print('end')

cold end : elif statement - with elif it will keep going until some condition is met and whichever is met first it will run that code, if none are right it will go straight to the end

t.color('pink')

color method - changes color to pink

" " " Dance off bro you and me " " "

comment

#i can do this all day

comment

' ' ' Wakanda forever ' ' '

comment

t.down()

down method - puts pen back down

t. forward (100)

forward method - moves forward 100 units

s = 'Manifesto' s[3]

i : indexing operator - first item is index 0 then counts 1,2, 3.......

print ('i am inevitable', end = 'i am ironman')

i am inevitablei am ironman : end function - lets you print two items on the same line and ends it

to start using turtle

import turte s = turtle.Screen( ) t = turtle.Turtle ( )

x = 10 type(x)

int : type operator - tells you the type of item

s = 'flash' s[1]

l : indexing operator - first item is index 0 then counts 1,2, 3.......

s = 'Spiderman' s[-1]

n : negative indexing operator - last item has an index of -1 then counts backwards from letters, -2, -3, -4.......

t. right (90)

right method - turns right 90 degrees

gotham_crime = 3 if gotham_crime > 0 : print ('send out the batsignal') print('end')

send out the batsignal end : if function - if statement is true do the code, if it is not skip it

temp = 40 if temp > 86 : print ('hot') if temp > 70 : print ('warm') else : print ('somewhere between 40 and 50') print('end')

somewhere between 40 and 50 end : else statement - else can be added with the if and elif and so if all elif and if statements are false, else code will run

print ('start') s = input ("how old are you: ") num = int(s) print ("you will be ", num + 1) print ('end')

start how old are you : 12 you will be 13 end : input function - input always comes out as a string NO MATTER WHAT EVEN IF A NUMBER but you can change str to int NEED TO IF YOU WANT TO DO MATH

print ('start') s = input ("who is the most powerful avenger? : ") print ("you typed", s) print ('if you choose anyone but Wanda you are wrong')

start who is the most powerful avenger? : wanda you typed wanda if you choose anyone but Wanda you are wrong : input function - requests and reads input from user then continues

x = '10' type(x)

str : type operator - if you use quotes you can make an int into a str

flash_speed = 356 if flash_speed < 300 : print ('run barry run') else : print ('super sonic bunch baby')

super sonic bunch baby : else statement - if if statement is not true python will do the else statement

draw a blue triangle width of 10 in turtle

t.color('blue') t.width(10) t.forward(100) t.right(120) t.forward(100) t.right(120) t.forward(100) t.right(120) t.forward(100)

sidekicks = ('kid flash', 'robin', 'speedy', 'superboy')

tuple : a comma-separated list of items enclosed in parentheses : same as list but CANNOT BE CHANGED

t.up()

up method - picks pen up

for item in 'wintersolider' : print (item)

w i n t e r s o l i d e r : loop function - python will loop through each item of the string, list, or tuple you give it and do whatever function you tell it for each one - CANNOT LOOP THROUGH A NUMBER

s = 'Wonderwoman' s[-5]

w : negative indexing operator - last item has an index of -1 then counts backwards from letters, -2, -3, -4.......

t.width(30)

width method - changes width of line to 30 units

print ('words', end = '!!!') print ('more')

words!!!more - end function - prints on same line because you are rewriting normal behavior


Ensembles d'études connexes

Chapter 2: Homeostasis and Adaptive Responses to Stressors

View Set

Anatomy Lab: Muscles of mastication

View Set

PowerBI Certification Exam Prep 2

View Set

Chapter 52, Nursing Management: Sexually Transmitted Infections

View Set

Comm 4720 - Valuation and Restructuring (Smith Capstone)

View Set

Ricci Chapter 4: Common Reproductive Issues

View Set