text stringlengths 1 3.55k | source stringlengths 24 207 | emb listlengths 1.02k 1.02k |
|---|---|---|
the output of the following dictionary comprehension? names = [ " alice ", " bob ", " charlie " ] name _ lengths = { name : len ( name ) for name in names } print ( name _ lengths ) a. { " alice " : 5, " bob " : 3, " charlie " : 7 } b. { 5 : " alice ", 3 : " bob ", 7 : " charlie " } c. { " a " : 5, " b " : 3, " c " : 7... | openstax_introduction_to_python_programming_-_web | [
0.002214941196143627,
-0.018281079828739166,
0.0036915934178978205,
-0.052816133946180344,
-0.00977401714771986,
-0.010240595787763596,
-0.00039989195647649467,
-0.009613046422600746,
0.008149729110300541,
0.0524095743894577,
0.0009946066420525312,
0.03480452299118042,
-0.01837829314172268,
... |
a dictionary, employees, outputs a nested dictionary, dept _ employees, which groups employees by department. input : employees = { 1001 : { " name " : " alice ", " department " : " engineering ", " salary " : 75000 }, 1002 : { " name " : " bob ", " department " : " sales ", " salary " : 50000 }, | openstax_introduction_to_python_programming_-_web | [
-0.02509879693388939,
-0.01307360827922821,
-0.02817513793706894,
-0.046771153807640076,
0.013048753142356873,
-0.02239462547004223,
0.006454710382968187,
-0.032161761075258255,
0.010556166991591454,
0.039911217987537384,
0.02358938381075859,
0.0031969931442290545,
0.011477135121822357,
-0... |
10. 5 • nested dictionaries and dictionary comprehension 259 1003 : { " name " : " charlie ", " department " : " engineering ", " salary " : 80000 }, 1004 : { " name " : " dave ", " department " : " marketing ", " salary " : 60000 }, 1005 : { " name " : " eve ", " department " : " sales ", " salary " : 55000 } } result... | openstax_introduction_to_python_programming_-_web | [
0.021220065653324127,
-0.010107598267495632,
-0.029592009261250496,
-0.05574984848499298,
-0.016379915177822113,
-0.01250455528497696,
0.020458262413740158,
0.0017891976749524474,
-0.0070123691111803055,
0.04564114660024643,
0.005236379336565733,
0.023005813360214233,
0.01898571476340294,
... |
10. 6 chapter summary highlights from this chapter include : • a dictionary in python is a container object including key - value pairs. • the dict type implements a dictionary in python. • a dictionary cannot have duplicate keys. • a dictionary is a mutable object but keys in the dictionary must be immutable objects. ... | openstax_introduction_to_python_programming_-_web | [
-0.00650853430852294,
0.005042124539613724,
-0.023286646232008934,
-0.02913905307650566,
-0.01997874490916729,
-0.010091940872371197,
-0.0008628970826976001,
-0.01015603356063366,
0.016906550154089928,
0.08665639162063599,
-0.010211835615336895,
-0.008887783624231815,
0.026699960231781006,
... |
##ct ( key1 = value1, key2 = value2 ) # from another dictionary old _ dict = { key1 : value1, key2 : value2 } new _ dict = dict ( old _ dict ) accessing dictionary items my _ dict = { key1 : value1, key2 : value2 } # accessing item using square bracket notation my _ dict [ key1 ] # accessing item through get ( ) method... | openstax_introduction_to_python_programming_-_web | [
-0.024765130132436752,
0.002153103705495596,
0.0006495735724456608,
-0.016622282564640045,
-0.04290400445461273,
-0.015698155388236046,
-0.01992456242442131,
-0.005225184373557568,
0.012785178609192371,
0.07628894597291946,
0.0037771102506667376,
0.011048884131014347,
-0.008476456627249718,
... |
10. 6 • chapter summary 261 concept description adding a new key - value pair or updating an existing key - value pair my _ dict = { key1 : value1, key2 : value2 } # updating an item using square bracket notation my _ dict [ key1 ] = new _ value # adding a new key - value pair using square bracket notation my _ dict [ ... | openstax_introduction_to_python_programming_-_web | [
-0.0196752417832613,
0.013141527771949768,
-0.03179521858692169,
-0.00922626256942749,
-0.04023446887731552,
-0.036827217787504196,
-0.019712526351213455,
-0.03885309398174286,
0.024231746792793274,
0.10934777557849884,
-0.02890635095536709,
0.0033010488841682673,
0.0016365742776542902,
-0... |
: [ 6 ], " odd " : [ 3, 5, 1 ] } access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 10 - 6 - chapter - summary ) 10. 6 • chapter summary 263 264 10 • dictionaries access for free at openstax. org figure 11. 1 credit : modification of work " fresh, bright apples... | openstax_introduction_to_python_programming_-_web | [
0.014197932556271553,
0.0011404920369386673,
-0.004281720146536827,
-0.05109017342329025,
0.018851263448596,
-0.013687933795154095,
0.007373789791017771,
0.01879538968205452,
0.01068519614636898,
0.07011783123016357,
0.0030579010490328074,
-0.016824332997202873,
0.03964204341173172,
-0.009... |
11. 1 object - oriented programming basics learning objectives by the end of this section you should be able to • describe the paradigm of object - oriented programming ( oop ). • describe the concepts of encapsulation and abstraction as they relate to oop, and identify the value of each concept. grouping into objects ... | openstax_introduction_to_python_programming_-_web | [
0.017977623268961906,
-0.012929529882967472,
-0.0020584003068506718,
-0.043166156858205795,
0.010565274395048618,
-0.024476634338498116,
-0.001984711503610015,
-0.019305113703012466,
0.011686491779983044,
0.07956573367118835,
0.012091043405234814,
0.009514528326690197,
0.03698008880019188,
... |
changed due to inclement weather. how should a programmer change the website object to allow changes to the venue field? a. add a procedure to change the field. b. allow users direct access to the field. c. no valid way to allow changes to the field. 4. which is a benefit of encapsulation when developing complex progra... | openstax_introduction_to_python_programming_-_web | [
-0.0354183055460453,
-0.007728725206106901,
-0.008313818834722042,
-0.030059201642870903,
0.011250331997871399,
-0.0013015036238357425,
-0.006584663409739733,
-0.03542083129286766,
-0.001475260709412396,
0.08697069436311722,
0.052587684243917465,
0.008398961275815964,
0.036885377019643784,
... |
11. 2 classes and instances learning objectives by the end of this section you should be able to • create a class with instance attributes, class attributes, and the _ _ init _ _ ( ) method. • use a class definition to create class instances to represent objects. classes and instances in the previous section, a real - ... | openstax_introduction_to_python_programming_-_web | [
0.02671652100980282,
-0.002025268040597439,
-0.004393494222313166,
-0.057645391672849655,
-0.012203759513795376,
-0.004225054290145636,
-0.0008938436512835324,
0.0036582867614924908,
-0.0017883473774418235,
0.04436507448554039,
0.03200617805123329,
-0.0512256845831871,
0.04344866797327995,
... |
11. 2 • classes and instances 267 checkpoint representing a coffee order with a class access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 11 - 2 - classes - and - instances ) concepts in practice classes and instances consider the example below : class cat : def... | openstax_introduction_to_python_programming_-_web | [
0.03721274062991142,
-0.004063553176820278,
0.024944886565208435,
-0.031511906534433365,
0.0028045009821653366,
-0.018352000042796135,
-0.01869947649538517,
0.0016532507725059986,
-0.008459406904876232,
0.06930798292160034,
0.03767389431595802,
0.006735422648489475,
0.016954021528363228,
0... |
- and - instances ) concepts in practice instances and _ _ init _ _ ( ) consider the example below : 1 class rectangle : 2 def _ _ init _ _ ( self ) : 3 self. length = 1 4 self. width = 1 5 def area ( self ) : 6 return self. length * self. width 7 8 room _ 1 = rectangle ( ) 9 room _ 1. length = 10 10 room _ 1. width = ... | openstax_introduction_to_python_programming_-_web | [
0.044012732803821564,
-0.011958686634898186,
0.01377425529062748,
-0.046757958829402924,
-0.019324829801917076,
-0.021308129653334618,
0.018286675214767456,
0.004892402328550816,
-0.020265955477952957,
0.04128243774175644,
0.04186214879155159,
-0.009477466344833374,
0.013766302727162838,
0... |
11. 2 • classes and instances 269 8. suppose line 2 is changed to def _ _ init _ _ ( ) :. what would room _ 1's attributes be initialized to? a. length = 0, width = 0 b. length = 1, width = 1 c. error instance attributes vs. class attributes the attributes shown so far have been instance attributes. an instance attribu... | openstax_introduction_to_python_programming_-_web | [
0.01294324267655611,
-0.00796338077634573,
0.02139572612941265,
-0.03209543973207474,
0.006757624447345734,
-0.00255208951421082,
0.015077848918735981,
-0.025128325447440147,
-0.003461259650066495,
0.02758442424237728,
0.010825002565979958,
0.0007622954435646534,
0.016944287344813347,
-0.0... |
error 270 11 • classes access for free at openstax. org try it creating a class for an airline's flight tickets write a class, flightticket, as described below. default values follow the attributes. then create a flight ticket and assign each instance attribute with values read from input. instance attributes : • fligh... | openstax_introduction_to_python_programming_-_web | [
0.011655986309051514,
-0.011797387152910233,
0.01603085733950138,
-0.009576004929840565,
0.025045286864042282,
-0.010908346623182297,
0.01189061813056469,
-0.012888080440461636,
0.013486175797879696,
0.04986761882901192,
0.005855385679751635,
-0.01286687795072794,
0.033629417419433594,
-0.... |
11. 2 • classes and instances 271 instance attributes : • title :'' • author :'' • year : 0 • pages : 0 class attribute : • imprint : fantasy tomes method : • _ _ init _ _ ( ) : initializes the instance attributes • print _ info ( ) : prints book information ( provided in template ) given input : lord of the bracelets ... | openstax_introduction_to_python_programming_-_web | [
-0.021055350080132484,
-0.03429575636982918,
0.04970536008477211,
-0.03803360462188721,
0.00871887058019638,
0.023073773831129074,
-0.01687372848391533,
-0.06246165931224823,
0.00650023715570569,
0.02669689618051052,
0.010187467560172081,
-0.0312347374856472,
0.0237930566072464,
0.01803720... |
11. 3 instance methods learning objectives by the end of this section you should be able to • create and implement _ _ init _ _ ( ) with multiple parameters including default parameter values. • describe what information an instance method has access to and can modify. more about _ _ init _ _ ( ) in python, _ _ init _ ... | openstax_introduction_to_python_programming_-_web | [
0.03814295679330826,
0.026779774576425552,
0.014587990939617157,
-0.04670730233192444,
-0.04062521085143089,
-0.005778164137154818,
-0.009737524203956127,
0.003166966373100877,
-0.017249148339033127,
0.06382812559604645,
0.03188535198569298,
-0.021946648135781288,
0.029481543228030205,
0.0... |
_ 10 = vitals ( 5241 ) c. both 3. suppose another _ _ init _ _ ( ) definition is added after the first with the header as follows : def _ _ init _ _ ( self, p _ id, bp, tmp, hr, rr ) what is the impact on the program? a. no change. b. second _ _ init _ _ ( ) definition produces an error. c. first two _ _ init _ _ ( ) c... | openstax_introduction_to_python_programming_-_web | [
0.016799451783299446,
0.011469884775578976,
0.010111609473824501,
-0.04969531670212746,
-0.01611049473285675,
-0.02911839634180069,
-0.0007000230834819376,
-0.013373804278671741,
0.0014525223523378372,
0.03344060853123665,
0.0034527487587183714,
-0.020436955615878105,
0.03761528059840202,
... |
11. 3 • instance methods 273 self. max _ mph = max _ mph def max _ kmh ( self ) : return self. max _ mph * 1. 609344 def update _ max ( self, speed ) : self. max _ mph = speed car _ 1 = productioncar ('mclaren ','speedtail ', 2020 ) # car _ 1. max _ mph is 0. 0 car _ 1. update _ max ( 250. 0 ) # car _ 1. max _ mph is 2... | openstax_introduction_to_python_programming_-_web | [
0.0036911459174007177,
0.019494831562042236,
0.01301940530538559,
0.01086184848099947,
-0.020886501297354698,
-0.020554086193442345,
0.031474821269512177,
0.01818576268851757,
0.007146001793444157,
0.0619964525103569,
0.0033270148560404778,
0.02795897237956524,
0.013594402000308037,
-0.013... |
( 8, true, false ) 274 11 • classes access for free at openstax. org order _ 1. change ( false, true ) 4. what does order _ 1 represent at the end of the program? a. 8 oz coffee b. 8 oz coffee with milk c. 8 oz coffee with sugar 5. why is change ( ) an instance method? a. can change attributes b. has multiple parameter... | openstax_introduction_to_python_programming_-_web | [
-0.017961286008358,
0.004624158143997192,
0.007374816574156284,
0.014185570180416107,
-0.02102840319275856,
-0.015077687799930573,
-0.018887190148234367,
0.007394386455416679,
-0.023165684193372726,
0.08461067825555801,
0.007222936954349279,
0.003939935006201267,
0.011028199456632137,
0.01... |
11. 4 overloading operators learning objectives by the end of this section you should be able to • identify magic methods and describe their purpose. • develop overloaded arithmetic and comparison operators for user - defined classes. magic methods and customizing magic methods are special methods that perform actions ... | openstax_introduction_to_python_programming_-_web | [
0.006282656919211149,
-0.006062261760234833,
0.02446444146335125,
-0.03569142147898674,
-0.007523312699049711,
-0.020775318145751953,
0.0032202366273850203,
0.0070267533883452415,
-0.05241627246141434,
0.04746665805578232,
0.02193806692957878,
0.0006538262241519988,
0.031179329380393028,
-... |
add _ _ ( ) for a user - defined class, account access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 11 - 4 - overloading - operators ) arithmetic operator ( operation ) magic method + ( addition ) _ _ add _ _ ( self, other ) - ( subtraction ) _ _ sub _ _ ( self,... | openstax_introduction_to_python_programming_-_web | [
0.020143484696745872,
-0.024637088179588318,
-0.006625432521104813,
-0.035817794501781464,
0.015455509535968304,
-0.014287704601883888,
0.030266204848885536,
0.016408713534474373,
-0.0428735688328743,
0.06836928427219391,
-0.003614927176386118,
0.0014744428917765617,
0.020180918276309967,
... |
11. 4 • overloading operators 277 6. which _ _ sub _ _ ( ) definition overloads the - operator for the code below to work? class pair : def _ _ init _ _ ( self, x = 0, y = 0 ) : self. x = x self. y = y # define _ _ sub _ _ ( ) p1 = pair ( 10, 2 ) p2 = pair ( 8, 4 ) p3 = p1 - p2 print ( p3. x, p3. y ) a. def _ _ sub _ _... | openstax_introduction_to_python_programming_-_web | [
0.04089847952127457,
-0.02392825298011303,
0.015583133324980736,
-0.0264478400349617,
0.010912496596574783,
-0.014427228830754757,
0.030411113053560257,
0.013171534985303879,
-0.02368166111409664,
0.0633975937962532,
0.017634036019444466,
0.017371218651533127,
0.004054691642522812,
-0.0336... |
= name self. amount = amount def _ _ str _ _ ( self ) : return f " { self. name } : $ { self. amount } " def _ _ lt _ _ ( self, other ) : return self. amount < other. amount def _ _ gt _ _ ( self, other ) : return self. amount > other. amount def _ _ eq _ _ ( self, other ) : return self. amount = = other. amount acct _... | openstax_introduction_to_python_programming_-_web | [
0.0063028717413544655,
-0.041871655732393265,
0.02051672153174877,
-0.02175154536962509,
-0.022330638021230698,
-0.040617864578962326,
0.02786543034017086,
0.014720603823661804,
-0.017267460003495216,
0.04772784560918808,
0.03427679464221001,
0.014505245722830296,
0.014243528246879578,
-0.... |
11. 4 • overloading operators 279 a. def _ _ le _ _ ( other ) : return self. amount < = other. amount b. def _ _ le _ _ ( self, other ) : return self. amount < = other. amount c. def _ _ le _ _ ( self, other ) : return other. amount < = self. amount 9. which type of value does _ _ gt _ _ ( ) return? a. account instance... | openstax_introduction_to_python_programming_-_web | [
0.0018297353526577353,
0.015535084530711174,
-0.0015835793456062675,
0.004769818391650915,
-0.01908821240067482,
-0.026691969484090805,
0.005254085175693035,
0.005778479855507612,
0.015028394758701324,
0.0492473766207695,
0.015292896889150143,
0.028230790048837662,
0.005141823552548885,
-0... |
11. 5 using modules with classes learning objectives by the end of this section you should be able to • construct an import statement that selectively imports classes. • create an alias to import a module. importing classes from modules import statements, as discussed in the modules chapter, allow code from other files... | openstax_introduction_to_python_programming_-_web | [
0.0624752938747406,
0.005581879056990147,
-0.007503066677600145,
-0.012299377471208572,
0.004111282993108034,
0.014043048955500126,
0.015555120073258877,
0.020059073343873024,
-0.0035603370051831007,
0.07702136039733887,
0.003353109350427985,
0.024121318012475967,
0.02577964775264263,
-0.0... |
11. 5 • using modules with classes 281 using aliases aliasing allows the programmer to use an alternative name for imported items. ex : import triangle as tri allows the program to refer to the triangle module as tri. aliasing is useful to avoid name collisions but should be used carefully to avoid confusion. example 1... | openstax_introduction_to_python_programming_-_web | [
0.03466319292783737,
0.011022098362445831,
0.04655304551124573,
0.007110531907528639,
-0.012108756229281425,
-0.017682986333966255,
0.03769604116678238,
-0.024021698161959648,
-0.034900374710559845,
0.06955567747354507,
0.011110130697488785,
0.014856436289846897,
0.02349756471812725,
-0.02... |
- 5 - using - modules - with - classes ) try it missing class import statement add the missing class import statement to the top of the file. import only a class, not a full module. do not make any changes to the rest of the code. in the end, the program should run without errors. access multimedia content ( https : / ... | openstax_introduction_to_python_programming_-_web | [
0.019508622586727142,
0.010832587257027626,
-0.009749136865139008,
0.019720349460840225,
0.02545798011124134,
0.01160625834017992,
-0.009237030521035194,
0.016368931159377098,
0.006538786459714174,
0.06778603047132492,
-0.0031972855795174837,
0.006261538714170456,
0.033540867269039154,
-0.... |
11. 6 chapter summary highlights from this chapter include : • object - oriented programming involves grouping related fields and procedures into objects using data abstraction and encapsulation. • classes define a type of object with attributes and methods. • instances of a class type represent objects and are created... | openstax_introduction_to_python_programming_-_web | [
0.02335362695157528,
-0.008954903110861778,
-0.007628053892403841,
-0.07311340421438217,
0.03120964765548706,
0.003077180590480566,
0.017095312476158142,
-0.003602897748351097,
0.015987345948815346,
0.0605786107480526,
0.0014964354922994971,
0.003808406414464116,
0.010112636722624302,
-0.0... |
11. 6 • chapter summary 283 construct description class definition class classname : " " " docstring " " " _ _ init _ _ ( ) class classname : def _ _ init _ _ ( self ) : # initialize attributes attributes class classname : class _ attr _ 1 = [ value ] class _ attr _ 2 = [ value ] def _ _ init _ _ ( self ) : instance _ ... | openstax_introduction_to_python_programming_-_web | [
0.009290934540331364,
0.0014244492631405592,
0.016315491870045662,
-0.03575580567121506,
-0.010716318152844906,
-0.014779922552406788,
-0.018442301079630852,
-0.008189123123884201,
-0.0028340390417724848,
0.060139015316963196,
0.03020685724914074,
-0.02312496118247509,
0.02907141111791134,
... |
##ursion basics 12. 2 simple math recursion 12. 3 recursion with strings and lists 12. 4 more math recursion 12. 5 using recursion to solve problems 12. 6 chapter summary introduction recursion is a powerful programming technique to write solutions compactly. recursion simplifies programs by calling smaller versions of... | openstax_introduction_to_python_programming_-_web | [
0.007205592934042215,
0.023014547303318977,
0.01829463243484497,
-0.054604362696409225,
-0.0007861954509280622,
-0.040745824575424194,
-0.0013061093632131815,
0.008776908740401268,
-0.02588733844459057,
0.0932818204164505,
0.02691837027668953,
0.0070900390855968,
-0.00636788597330451,
-0.0... |
12. 1 recursion basics learning objectives by the end of this section you should be able to • describe the concept of recursion. • demonstrate how recursion uses simple solutions to build a better solution. recursion recursion is a problem solving technique that uses the solution to a simpler version of the problem to ... | openstax_introduction_to_python_programming_-_web | [
0.036498479545116425,
0.02202504873275757,
0.00711396336555481,
-0.02578139491379261,
-0.021530283614993095,
-0.0347154326736927,
-0.04521666094660759,
0.023176509886980057,
-0.007787823677062988,
0.09992937743663788,
0.022949354723095894,
0.010124393738806248,
0.006706350017338991,
-0.014... |
##ursion - basics ) concepts in practice solving three towers 4. how many total steps does it take to solve two rings? a. 1 b. 2 288 12 • recursion access for free at openstax. org c. 3 5. how many total steps does it take to solve three rings? a. 3 b. 4 c. 7 6. how many total steps does it take to solve four rings? a.... | openstax_introduction_to_python_programming_-_web | [
0.058136481791734695,
0.029752230271697044,
-0.012983215972781181,
-0.021483557298779488,
-0.014679769985377789,
0.009205414913594723,
-0.024017874151468277,
0.044560059905052185,
0.009783405810594559,
0.09938126057386398,
0.04281860217452049,
0.05712132900953293,
-0.01895773597061634,
-0.... |
12. 2 simple math recursion learning objectives by the end of this section you should be able to • identify a recursive case and a base case in a recursive algorithm. • demonstrate how to compute a recursive solution for the factorial function. calculating a factorial the factorial of a positive integer is defined as t... | openstax_introduction_to_python_programming_-_web | [
0.033449139446020126,
0.021489793434739113,
-0.004682896193116903,
-0.03883754462003708,
0.04204461723566055,
-0.053468454629182816,
0.002588477684184909,
-0.011461431160569191,
-0.04031653702259064,
0.07745959609746933,
0.022489819675683975,
-0.010340079665184021,
-0.012470172718167305,
-... |
12. 2 • simple math recursion 289 3. listing the primary numbers ( prime numbers ) greater than 3. 4. listing the fibonacci sequence of numbers. defining a recursive function recursive algorithms are written in python as functions. in a recursive function different actions are performed according to the input parameter... | openstax_introduction_to_python_programming_-_web | [
0.038126252591609955,
-0.010580782778561115,
0.02099764533340931,
-0.04318736493587494,
0.019666608422994614,
-0.036068983376026154,
-0.01042220275849104,
-0.014241335913538933,
-0.03325854241847992,
0.07157967239618301,
0.03809509798884392,
-0.022592104971408844,
-0.006504731252789497,
-0... |
the questions below, the function rec _ fact ( ) is another recursive function that calculates a factorial. what is the result of each definition of rec _ fact ( ) if n = 17 is the initial input parameter? 5. def rec _ fact ( n ) : return n * rec _ fact ( n - 1 ) a. 355687428096000 b. 0 c. no result / infinite computat... | openstax_introduction_to_python_programming_-_web | [
0.05063555762171745,
0.0035701508168131113,
0.008289281278848648,
-0.03327453136444092,
0.027778996154665947,
-0.03176811337471008,
-0.016005994752049446,
0.0025772638618946075,
-0.019967686384916306,
0.08766575902700424,
0.04471949115395546,
-0.027131834998726845,
-0.012425597757101059,
-... |
sum of the digits of 6721 is 16. hint : there are 10 base cases, which can be checked easily with the right condition. access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 12 - 2 - simple - math - recursion ) | openstax_introduction_to_python_programming_-_web | [
0.019558751955628395,
0.003255705116316676,
0.0032594408839941025,
-0.006386379711329937,
-0.00941385142505169,
-0.05257611721754074,
0.013158242218196392,
0.02728193625807762,
0.0029563873540610075,
0.08947604149580002,
0.029372191056609154,
0.006284635979682207,
0.025271562859416008,
0.0... |
12. 3 recursion with strings and lists learning objectives by the end of this section you should be able to • demonstrate the use of recursion to solve a string problem. • demonstrate the use of recursion to solve a list problem. • use the built - in count ( ) list function. recursion with strings a word that is spelle... | openstax_introduction_to_python_programming_-_web | [
0.010035245679318905,
-0.004838652443140745,
0.0007831321563571692,
-0.02134023606777191,
-0.020884955301880836,
-0.011206203140318394,
-0.018946148455142975,
0.006328510120511055,
-0.011781570501625538,
0.06474759429693222,
0.019021248444914818,
0.0022730736527591944,
-0.0011731135891750455... |
same items but in different order. the count ( ) function returns a count of the number of items in a list that match the given item, and returns 0 otherwise. ex : for list _ num = [ 1, 3, 3, 4 ], list _ num. count ( 3 ) returns 2. checkpoint checking list permutations access multimedia content ( https : / / openstax. ... | openstax_introduction_to_python_programming_-_web | [
0.03563784435391426,
-0.02062002383172512,
-0.0005223875632509589,
0.008589223958551884,
-0.02725820057094097,
-0.05493592098355293,
-0.03300686553120613,
0.01136489026248455,
0.03650965914130211,
0.08851498365402222,
0.019351042807102203,
0.01722807250916958,
-0.00637121731415391,
-0.0178... |
12. 3 • recursion with strings and lists 293 a. true b. false 8. list _ 1 = [ 1, 2, 3 ] list _ 2 = [ 1, 2, 3 ] a. true b. false try it remove duplicates write a recursive rem _ dup ( ) function that removes duplicates from a list. ex : list [ 5, 5, 2, 1, 3, 1, 6 ] should result in an output list [ 5, 2, 1, 3, 6 ]. acce... | openstax_introduction_to_python_programming_-_web | [
0.04281182587146759,
0.012105689384043217,
0.03679485246539116,
-0.011569133959710598,
-0.04253913089632988,
-0.056764792650938034,
-0.04173242673277855,
-0.0004994460614398122,
0.03421946242451668,
0.09251407533884048,
0.0035240836441516876,
0.002636345336213708,
0.011840572580695152,
-0.... |
12. 4 more math recursion learning objectives by the end of this section you should be able to • define a recursive function to generate fibonacci numbers. fibonacci sequence using recursion the fibonacci sequence is a sequence in which each number in the sequence is the sum of the previous two numbers in the sequence.... | openstax_introduction_to_python_programming_-_web | [
0.007228543050587177,
0.03190241754055023,
0.030804025009274483,
-0.03087121620774269,
-0.007585516199469566,
-0.06409281492233276,
-0.054315149784088135,
0.015546027570962906,
-0.006083199288696051,
0.08270329236984253,
0.022443857043981552,
-0.019541151821613312,
0.021635711193084717,
-0... |
##b ( n ) : # base case if n = = 0 or n = = 1 : return n # recursive case elif n > 1 : return fib ( n - 1 ) + fib ( n - 2 ) # error case else : print ( " fibonacci numbers begin at 0. " ) return # test code print ( fib ( 7 ) ) the above code's output is : 13 checkpoint recursive fibonacci function access multimedia con... | openstax_introduction_to_python_programming_-_web | [
0.0036559216678142548,
-0.01800147071480751,
0.022823451086878777,
-0.01080622524023056,
-0.00444994168356061,
-0.06672194600105286,
-0.05209910497069359,
0.0188293494284153,
-0.019023215398192406,
0.07417397201061249,
0.01186809316277504,
0.006320096552371979,
0.01495198905467987,
-0.0173... |
12. 4 • more math recursion 295 a. tree b. recursion tree c. iteration tree greatest common divisor ( gcd ) the greatest common divisor ( gcd ) of two positive integers is an integer that is a divisor for both integers. ex : the gcd of 6 and 9 is 3 because 3 x 2 = 6, and 3 x 3 = 9. the gcd is found easily using euclid'... | openstax_introduction_to_python_programming_-_web | [
0.010653739795088768,
-0.018455736339092255,
0.01543942466378212,
-0.013107744045555592,
0.019956396892666817,
-0.028939299285411835,
0.002814445411786437,
-0.013818592764437199,
0.009043823927640915,
0.0943368673324585,
0.006626558024436235,
-0.046119045466184616,
-0.009616788476705551,
-... |
positive integer y, power ( x, y ) returns x raised to y. ex : power ( 3, 4 ) returns 81. access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 12 - 4 - more - math - recursion ) try it recursive power ( with any integer power ) write a recursive rec _ pow ( ) fun... | openstax_introduction_to_python_programming_-_web | [
0.04015975818037987,
0.004387794528156519,
0.002796559827402234,
-0.003199920989573002,
-0.02693391963839531,
-0.0019723237492144108,
-0.016590304672718048,
0.025670837610960007,
-0.013628609478473663,
0.0928931012749672,
0.01482735201716423,
-0.003167145885527134,
-0.008039586246013641,
-... |
12. 5 • using recursion to solve problems 297 binary search searching a sorted list usually involves looking at each item. if the item being searched is not found, then the search can take a long time. a binary search is a recursive algorithm used to efficiently search sorted lists. in each recursive step, about half t... | openstax_introduction_to_python_programming_-_web | [
0.019350705668330193,
0.017254265025258064,
-0.001657034270465374,
-0.007470845244824886,
0.007017927709966898,
-0.020153408870100975,
-0.011305105872452259,
0.003409031080082059,
0.004368633963167667,
0.12246838212013245,
0.03600949048995972,
-0.03547680005431175,
-0.015409166924655437,
-... |
1, 48 ) ) # key exists at index 8 print ( binary _ search ( in _ list, 0, len ( in _ list ) - 1, 86 ) ) # key does not exist 298 12 • recursion access for free at openstax. org the above code's output is : 8 not found checkpoint binary search access multimedia content ( https : / / openstax. org / books / introduction ... | openstax_introduction_to_python_programming_-_web | [
0.025085167959332466,
0.018705667927861214,
-0.013080782257020473,
-0.0057556661777198315,
0.008440488018095493,
-0.01788564771413803,
-0.0561475045979023,
0.02131863497197628,
0.0028350905049592257,
0.11744724214076996,
0.03497500345110893,
-0.0020708017982542515,
0.015468977391719818,
-0... |
12. 5 • using recursion to solve problems 299 example 12. 4 solving n towers the solution to three towers is simple with recursion. in the code below, rings are numbered from the top down. the smallest ring is 1, the next ring is 2, and when solving for three rings, the bottom ring is 3. " " " solving the towers proble... | openstax_introduction_to_python_programming_-_web | [
0.03194102644920349,
0.012829497456550598,
0.011939543299376965,
-0.023830099031329155,
-0.02015516348183155,
-0.020879853516817093,
-0.04000294208526611,
0.019251149147748947,
0.014481009915471077,
0.09060325473546982,
0.02441195584833622,
0.02333381399512291,
-0.004786335397511721,
-0.03... |
t1 to tower t3 move ring 1 from tower t2 to tower t1 move ring 2 from tower t2 to tower t3 move ring 1 from tower t1 to tower t3 300 12 • recursion access for free at openstax. org concepts in practice solving three towers 5. for four rings, how many total lines will be printed by three _ towers ( )? a. 7 b. 15 6. woul... | openstax_introduction_to_python_programming_-_web | [
0.04104582220315933,
0.011041508987545967,
0.023601878434419632,
-0.013651901856064796,
-0.007309275679290295,
-0.024046042934060097,
-0.04574495181441307,
0.011648987419903278,
0.011663857847452164,
0.07026192545890808,
0.01714324951171875,
0.016317229717969894,
0.011317389085888863,
-0.0... |
solve problems using recursion. function description count ( element ) returns the number of times the element exists on the list on which the count ( ) function is called. table 12. 1 chapter 12 reference. 302 12 • recursion access for free at openstax. org figure 13. 1 credit : modification of work " don't stare... o... | openstax_introduction_to_python_programming_-_web | [
-0.006627127528190613,
0.003924964927136898,
-0.002382568782195449,
-0.03444244712591171,
0.014658785425126553,
0.007915657013654709,
-0.017440875992178917,
-0.0042923446744680405,
0.012948144227266312,
0.05477713420987129,
0.020143207162618637,
-0.001889280742034316,
-0.009980585426092148,
... |
13. 1 inheritance basics learning objectives by the end of this section you should be able to • identify is - a and has - a relationships between classes. • differentiate between a subclass and a superclass. • create a superclass, subclass, and instances of each. is - a vs has - a relationships classes are related to e... | openstax_introduction_to_python_programming_-_web | [
0.01322763692587614,
0.021838856860995293,
-0.015902932733297348,
-0.027881551533937454,
0.0024673345033079386,
0.019342608749866486,
-0.00618452625349164,
-0.032322317361831665,
-0.0005415802006609738,
0.061601750552654266,
-0.02696598879992962,
0.013544672168791294,
0.04660362750291824,
... |
##class attributes and methods checkpoint using inheritance to create subclasses access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 13 - 1 - inheritance - basics ) 304 13 • inheritance access for free at openstax. org concepts in practice creating subclasses 4.... | openstax_introduction_to_python_programming_-_web | [
0.021794190630316734,
0.013407314196228981,
0.014600936323404312,
-0.030783001333475113,
0.015138499438762665,
0.013309008441865444,
0.0027324361726641655,
0.010424873791635036,
0.012346271425485611,
0.061692360788583755,
-0.0038493287283927202,
0.01533083338290453,
0.020510854199528694,
0... |
13. 1 • inheritance basics 305 the class that inherits. this book uses superclass / subclass throughout for consistency. class inherited from class that inherits superclass subclass base class derived class parent class child class table 13. 1 try it employee and developer classes given the employee class, create a dev... | openstax_introduction_to_python_programming_-_web | [
0.007473798003047705,
0.014627235941588879,
-0.02850835584104061,
-0.05105726420879364,
0.03433562070131302,
0.0015297840582206845,
0.014391222968697548,
-0.018097393214702606,
0.02240440435707569,
0.06117790564894676,
0.02090851217508316,
0.03813544660806656,
0.03319350630044937,
0.023763... |
13. 2 attribute access learning objectives by the end of this section you should be able to • implement a subclass that accesses inherited attributes from the superclass. • write a subclass's _ _ init _ _ ( ) that inherits superclass instance attributes and creates new instance attributes. 306 13 • inheritance access f... | openstax_introduction_to_python_programming_-_web | [
0.006007583811879158,
0.033643871545791626,
-0.010353143326938152,
-0.04625115916132927,
0.033354517072439194,
0.005431056022644043,
-0.004216290544718504,
-0.022036494687199593,
-0.003949118312448263,
0.06084427610039711,
-0.002208907622843981,
0.009637759067118168,
0.04182148724794388,
-... |
_ _ ( self ) : self. feat _ 1 = 1 self. feat _ 2 = " " | openstax_introduction_to_python_programming_-_web | [
0.007611721288412809,
-0.03192097321152687,
0.007100074086338282,
0.020972315222024918,
-0.0010125006083399057,
-0.0344436876475811,
-0.0006392147042788565,
0.00823285710066557,
0.021272478625178337,
0.03572492673993111,
0.020307432860136032,
0.008338131941854954,
0.028685174882411957,
-0.... |
13. 2 • attribute access 307 def bc _ display ( self ) : print ( f " superclass : { self. feat _ 2 } " ) class subclass ( superclass ) : def dc _ display ( self ) : print ( f " subclass : { self. feat _ 2 } " ) dc _ 1 = subclass ( ) a. feat _ 2 b. feat _ 1 and feat _ 2 c. none using _ _ init _ _ ( ) to create and inher... | openstax_introduction_to_python_programming_-_web | [
-0.005637237802147865,
-0.00267607718706131,
-0.0023207245394587517,
-0.04118283838033676,
0.029105864465236664,
0.009548203088343143,
0.005440287292003632,
-0.02603643760085106,
-0.009131812490522861,
0.04899749159812927,
-0.00611294386908412,
-0.003583099227398634,
0.05139503628015518,
0... |
##p _ 1 = employee ( ) dev _ 1 = developer ( ) 4. what would be the output of dev _ 1. dev _ display ( )? a. employee 2 hired in 2023 b. proficient in ['python ','c + + ','java'] c. error 5. what would be the output of emp _ 1. dev _ display ( )? a. employee 1 hired in 2023 b. proficient in ['python ','c + + ','java'] ... | openstax_introduction_to_python_programming_-_web | [
-0.0063134594820439816,
-0.024184273555874825,
-0.0031906948424875736,
-0.027275824919342995,
-0.013098593801259995,
-0.034269701689481735,
0.012199086137115955,
-0.020319180563092232,
0.008770838379859924,
0.035857584327459335,
-0.029764419421553612,
0.013029588386416435,
0.0396374315023422... |
topped with buttercream frosting access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 13 - 2 - attribute - access ) 13. 2 • attribute access 309 | openstax_introduction_to_python_programming_-_web | [
-0.01353518757969141,
-0.016896963119506836,
0.007089730817824602,
-0.013406606391072273,
0.0011899542296305299,
-0.0018159705214202404,
0.026404866948723793,
-0.01154550164937973,
0.020659558475017548,
0.022767651826143265,
-0.01694108545780182,
0.0012994242133572698,
0.04444595053792,
-0... |
13. 3 methods learning objectives by the end of this section you should be able to • write overridden methods to change behavior of inherited methods. • use super ( ) to access superclass methods. • identify applications of polymorphism in method and function use. overriding methods sometimes a programmer wants to chan... | openstax_introduction_to_python_programming_-_web | [
-0.003927935380488634,
0.0336996465921402,
0.0013860182370990515,
-0.015526148490607738,
0.0036468838807195425,
-0.029896525666117668,
-0.007363195065408945,
-0.001706025330349803,
0.005726495757699013,
0.07249479740858078,
0.0011460373643785715,
0.04775124415755272,
0.01835138164460659,
0... |
of calc _ tax ( ) is missing a parameter. c. contracttaxde can't override calc _ tax ( ) since contracttax already has. 3. the following program doesn't override calc _ tax ( ). why? class tax : def calc _ tax ( self ) : print ('calculating tax') total = 0 # to replace with calculation return total class contracttax : ... | openstax_introduction_to_python_programming_-_web | [
0.011326630599796772,
0.02271110750734806,
-0.0058061215095222,
0.012958893552422523,
-0.006184675730764866,
-0.02240249142050743,
0.006952847354114056,
0.017529187723994255,
-0.014615852385759354,
0.07609017193317413,
0.023833787068724632,
0.033650338649749756,
0.02314089611172676,
-0.002... |
13. 3 • methods 311 6 def _ _ init _ _ ( self, ln = 1, wd = 1 ) : 7 super ( ). _ _ init _ _ ( 4 ) 8 self. length = ln 9 self. width = wd 10 11 class square ( rectangle ) : 12 def _ _ init _ _ ( self, side = 1 ) : 13 super ( ). _ _ init _ _ ( side, side ) 14 self. side = side 15 16 sq _ 1 = square ( 5 ) 17 print ( sq _ ... | openstax_introduction_to_python_programming_-_web | [
0.007554010488092899,
0.021617548540234566,
0.02942233346402645,
-0.03618999943137169,
-0.007177651394158602,
0.011779068037867546,
0.020470982417464256,
-0.0027424986474215984,
0.01387749146670103,
0.059824805706739426,
0.021484114229679108,
0.055217064917087555,
0.005871103145182133,
-0.... |
at openstax. org class plant : def display ( self ) : print ( " i'm a plant " ) class mint ( plant ) : def display ( self ) : print ( " i'm a mint " ) class lavender ( mint ) : def display ( self ) : print ( " i'm a lavender " ) mint _ 1 = mint ( ) mint _ 1. display ( ) lavender _ 1 = lavender ( ) lavender _ 1. display... | openstax_introduction_to_python_programming_-_web | [
-0.03791093826293945,
0.011008438654243946,
-0.014681454747915268,
0.006611480377614498,
-0.015148147009313107,
0.010197690688073635,
0.026478873565793037,
0.001279387972317636,
0.028613127768039703,
0.055600959807634354,
0.03146757930517197,
0.05679803341627121,
0.008966008201241493,
-0.0... |
13. 3 • methods 313 class contracttax : def _ _ init _ _ ( self, value ) : self. value = value def calc _ tax ( self ) : print ( " calculating contracts tax " ) total = 0. 15 * self. value # to replace with calculation return total my _ tax = contracttax ( value = 1000 ) # replace 1000 with any value result = my _ tax.... | openstax_introduction_to_python_programming_-_web | [
-0.0068296329118311405,
0.0199266504496336,
0.010766997933387756,
-0.022736208513379097,
0.00107422168366611,
-0.01448279432952404,
-0.004079569596797228,
0.0030348629225045443,
0.004835087805986404,
0.054319851100444794,
0.03223434463143349,
0.04135638475418091,
0.021857239305973053,
-0.0... |
= str ( x ) c. x = 3 * 4 y = x * x try it overriding methods given the pet class, create a bird class inherited from pet and a finch class inherited from bird. override the display ( ) method in bird and finch such that the program output is : pet type : bird bird type : finch access multimedia content ( https : / / op... | openstax_introduction_to_python_programming_-_web | [
0.01667105033993721,
-0.020392144098877907,
0.043489400297403336,
-0.04567353054881096,
-0.011955883353948593,
-0.03625331073999405,
0.008714995346963406,
0.01868281699717045,
0.03985298052430153,
0.044598910957574844,
0.008549983613193035,
0.01777186430990696,
0.03628361225128174,
0.01123... |
13. 3 • methods 315 try it using super ( ) given the employee class, create a developer class inherited from employee with methods _ _ init _ _ ( ) and print _ info ( ) such that : _ _ init _ _ ( ) • uses super ( ) to call employee's _ _ init _ _ ( ) to initialize e _ id and hire _ year. • assigns lang _ xp with list p... | openstax_introduction_to_python_programming_-_web | [
0.028240401297807693,
-0.012607087381184101,
-0.010706140659749508,
-0.01784464903175831,
0.00906488299369812,
-0.019473807886242867,
0.015536786988377571,
-0.030905811116099358,
0.00019501452334225178,
0.04678335785865784,
0.01615026406943798,
0.020630497485399246,
0.05976046621799469,
0.... |
13. 4 hierarchical inheritance learning objectives by the end of this section you should be able to • label relationships between classes as types of inheritance. • construct classes that form hierarchical inheritance. hierarchical inheritance basics hierarchical inheritance is a type of inheritance in which multiple c... | openstax_introduction_to_python_programming_-_web | [
0.014239666052162647,
-0.014040284790098667,
-0.0024426113814115524,
-0.028137214481830597,
0.041176483035087585,
0.012970746494829655,
-0.01779584027826786,
0.004859654698520899,
-0.003793775336816907,
0.04640205577015877,
0.015919890254735947,
0.0012389695039018989,
0.0024136374704539776,
... |
: def display ( self ) : super ( ). display ( ) print ( " part : tenor " ) class bass ( choirmember ) : def display ( self ) : super ( ). display ( ) print ( " part : bass " ) mem _ 10 = alto ( ) | openstax_introduction_to_python_programming_-_web | [
0.014872370287775993,
-0.04827311262488365,
0.0012330550234764814,
0.012031327933073044,
0.04664435237646103,
-0.03294365480542183,
0.022704796865582466,
0.006715645547956228,
-0.004346880130469799,
0.012485451065003872,
0.051380716264247894,
-0.016840552911162376,
0.020911868661642075,
-0... |
13. 4 • hierarchical inheritance 317 mem _ 13 = tenor ( ) mem _ 15 = soprano1 ( ) mem _ 10. display ( ) print ( ) mem _ 13. display ( ) print ( ) mem _ 15. display ( ) the code's output is : current choir member part : alto current choir member part : tenor current choir member part : soprano division : soprano 1 conce... | openstax_introduction_to_python_programming_-_web | [
0.004238246474415064,
-0.01704583875834942,
0.004971861373633146,
-0.029339658096432686,
0.057741254568099976,
0.01130336057394743,
-0.004254198633134365,
-0.025322813540697098,
-0.0020380483474582434,
0.03408995643258095,
0.0022884162608534098,
-0.0004098937788512558,
0.03070175088942051,
... |
_ inst have access to? a. a _ attr b. a _ attr, c _ attr c. c _ attr 5. which attributes does d _ inst have access to? a. b _ attr, d _ attr b. a _ attr, b _ attr, d _ attr c. d _ attr try it overriding methods define three classes : instrument, woodwind, and string. • instrument has instance attribute owner, with defa... | openstax_introduction_to_python_programming_-_web | [
-0.03380988538265228,
-0.0192192941904068,
0.007156603969633579,
-0.03606325760483742,
0.02404327690601349,
0.018368516117334366,
0.0008793707238510251,
-0.020581165328621864,
0.019189387559890747,
0.04072659835219383,
0.008598823100328445,
-0.004232295323163271,
0.017795495688915253,
-0.0... |
13. 5 multiple inheritance and mixin classes learning objectives by the end of this section you should be able to • construct a class that inherits from multiple superclasses. • identify the diamond problem within multiple inheritance. • construct a mixin class to add functionality to a subclass. multiple inheritance b... | openstax_introduction_to_python_programming_-_web | [
0.013911526650190353,
0.01290332991629839,
0.00048755764146335423,
-0.045757655054330826,
0.0017941842088475823,
-0.009825867600739002,
-0.008487277664244175,
-0.004861718043684959,
-0.020717067644000053,
0.06336914747953415,
0.002188743557780981,
-0.012029210105538368,
0.025493497028946877,... |
a. 2 b. 20 c. error 3. what is the value of b _ inst. a _ attr? a. 2 b. 4 c. error the diamond problem and mixin classes multiple inheritance should be implemented with care. the diamond problem occurs when a class inherits from multiple classes that share a common superclass. ex : dessert and bakedgood both inherit fr... | openstax_introduction_to_python_programming_-_web | [
-0.0016177434008568525,
0.014497230760753155,
0.0118203554302454,
-0.033653393387794495,
-0.010637861676514149,
-0.016596756875514984,
0.0063609653152525425,
0.002086944878101349,
-0.006084212567657232,
0.05537557601928711,
-0.009494285099208355,
-0.025859558954834938,
0.023378780111670494,
... |
13. 5 • multiple inheritance and mixin classes 321 class plant : def photosynth ( self ) : print ( " photosynthesizing " ) def eat ( self ) : print ( " eating " ) class rose ( plant ) : pass class pitcher ( plant ) : pass class venusflytrap ( plant ) : pass 4. which edit is appropriate? a. move eat ( ) to a different c... | openstax_introduction_to_python_programming_-_web | [
0.013250689953565598,
0.015392580069601536,
0.0032953021582216024,
-0.005050016567111015,
0.022470977157354355,
-0.006774552632123232,
-0.029409529641270638,
-0.0024182649794965982,
-0.008737877942621708,
0.061770956963300705,
0.007145446725189686,
-0.004691099748015404,
0.032515380531549454... |
" connecting to server " and is called by foodfast's process _ order ( ). the output should match : processing meal order connecting to server enjoy your foodfast order access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 13 - 5 - multiple - inheritance - and - m... | openstax_introduction_to_python_programming_-_web | [
0.0016085626557469368,
0.02098039537668228,
-0.04867393150925636,
-0.012707672081887722,
0.019542556256055832,
-0.015612428076565266,
0.0004592741606757045,
-0.007237355690449476,
0.009134478867053986,
0.072573721408844,
-0.02689577080309391,
0.03187020868062973,
0.02279416099190712,
-0.01... |
13. 6 • chapter summary 323 • polymorphism describes a single representation for multiple forms and is applied in python to define multiple methods with the same name and allow the same method to take different arguments. • hierarchical inheritance is a type of inheritance in which multiple classes inherit from a singl... | openstax_introduction_to_python_programming_-_web | [
0.018229015171527863,
0.022984519600868225,
-0.009141238406300545,
-0.03950630873441696,
0.03284863382577896,
0.016835957765579224,
-0.0029505083803087473,
-0.019332969561219215,
0.02021035924553871,
0.05812615156173706,
0.012804053723812103,
0.001713991747237742,
0.004864056129008532,
-0.... |
: def display ( self ) : print ('subclass 3 method') implement multiple inheritance class superclass1 : pass class superclass2 : pass class subclass ( superclass1, superclass2 ) : pass table 13. 2 chapter 13 reference. 13. 6 • chapter summary 325 326 13 • inheritance access for free at openstax. org figure 14. 1 credit... | openstax_introduction_to_python_programming_-_web | [
-0.005021908786147833,
-0.008657194674015045,
0.0007306188344955444,
-0.028566690161824226,
0.022021058946847916,
-0.009983857162296772,
0.008008879609405994,
0.005265046842396259,
-0.0027312850579619408,
0.1141904965043068,
-0.003502464387565851,
0.02275075949728489,
0.03798456862568855,
... |
14. 1 reading from files learning objectives by the end of this section you should be able to • understand how to open a file using the open ( ) function. • demonstrate how to read information from a file using read ( ), readline ( ), and readlines ( ). opening a file reading information from and writing information to... | openstax_introduction_to_python_programming_-_web | [
0.016274651512503624,
0.02655787207186222,
0.01924918219447136,
-0.015374643728137016,
-0.01733270287513733,
-0.011692965403199196,
-0.020135605707764626,
0.0021745353005826473,
0.018372636288404465,
0.09553049504756927,
-0.011688916943967342,
0.020135745406150818,
0.006526073440909386,
0.... |
individual lines of a file and returns a string list containing all the lines of the file in order. example 14. 1 using read ( ) and readlines ( ) a file called input. txt has the following contents : 12 328 14 • files access for free at openstax. org 55 5 91 " " " demonstrating read ( ) and readlines ( ) " " " # using... | openstax_introduction_to_python_programming_-_web | [
-0.014758016914129257,
-0.0008675921126268804,
0.040769606828689575,
-0.009371044114232063,
-0.03352562338113785,
-0.03813382610678673,
-0.026546413078904152,
0.0023974780924618244,
0.03697112947702408,
0.08586309105157852,
0.0038325455971062183,
0.009144285693764687,
0.012284533120691776,
... |
14. 1 • reading from files 329 checkpoint read ( ) and readline ( ) access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 14 - 1 - reading - from - files ) concepts in practice reading files suppose fileobj = open ( " input. txt " ) has already been executed for e... | openstax_introduction_to_python_programming_-_web | [
0.014627141878008842,
0.021386027336120605,
0.020716235041618347,
0.006692537106573582,
-0.014897188171744347,
-0.01912279985845089,
-0.030849909409880638,
-0.013600458391010761,
0.03905705362558365,
0.08755836635828018,
-0.006684173364192247,
0.014605253003537655,
0.009609433822333813,
-0... |
: 5 25 13 4 6 19 access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 14 - 1 - reading - from - files ) | openstax_introduction_to_python_programming_-_web | [
0.04222017526626587,
0.010704242624342442,
-0.013986852020025253,
0.013490335084497929,
0.01018528826534748,
0.0059302859008312225,
-0.02160322293639183,
0.008116507902741432,
0.009656493552029133,
0.09627945721149445,
0.00009369450708618388,
0.03136179596185684,
0.03552047535777092,
-0.02... |
14. 2 writing to files learning objectives by the end of this section you should be able to • understand how to open a file for writing. • explain different modes for opening a file in python. • demonstrate the use of the write ( ) function to write to a file. • understand the importance of closing a file. opening a fi... | openstax_introduction_to_python_programming_-_web | [
0.0013071028515696526,
0.018479838967323303,
0.024968048557639122,
0.001260718097910285,
-0.007961290888488293,
-0.008995463140308857,
-0.014608892612159252,
0.022613920271396637,
-0.03828608617186546,
0.09609795361757278,
-0.036902837455272675,
0.02270728349685669,
0.018889816477894783,
0... |
14. 2 • writing to files 331 parameter mode name and description example'r'read mode : open the specified file for reading. if the file does not exist, an error occurs. when no mode parameter is used, the default is to open a file in read mode. fileobj = open ( " input. txt " ) same as : fileobj = open ( " input. txt "... | openstax_introduction_to_python_programming_-_web | [
0.01713968999683857,
0.02091868221759796,
0.03430313616991043,
0.0006923717446625233,
-0.004220630973577499,
0.005127901677042246,
0.0019840411841869354,
0.0244472436606884,
-0.005935082212090492,
0.10969836264848709,
0.018504207953810692,
-0.0029084833804517984,
0.02726704068481922,
-0.01... |
txt ",'a') 3. a new file called newlog. txt must be created. which of the following lines of code works best? a. logfile = open ( " newlog. txt " ) b. logfile = open ( " newlog. txt ",'w') c. logfile = open ( " newlog. txt ",'a') using write ( ) and close ( ) the write ( ) function is used to write to an already opened... | openstax_introduction_to_python_programming_-_web | [
0.008632722310721874,
0.003684267168864608,
0.027410728856921196,
0.024822620674967766,
-0.00388237158767879,
-0.03379654139280319,
0.005149667616933584,
0.009175135754048824,
0.011158918030560017,
0.09127676486968994,
-0.0015764582203701138,
0.012611545622348785,
0.006109733134508133,
-0.... |
( infile. read ( ) ) infile. close ( ) # reopen the file in append mode to add to the file opfile = open ( " output. txt ",'a') # note the use of newline characters opfile. write ( " \ nadding to the file. " ) opfile. write ( " \ nadding another line. " ) # ensure changes are saved by closing the file opfile. close ( )... | openstax_introduction_to_python_programming_-_web | [
-0.008886104449629784,
-0.014404821209609509,
0.028545567765831947,
0.007552425377070904,
0.00909146573394537,
-0.03451794385910034,
-0.0172383151948452,
-0.015788454562425613,
0.01316496729850769,
0.06698098033666611,
-0.0178989265114069,
0.011165241710841656,
0.020592791959643364,
-0.013... |
14. 2 • writing to files 333 read the original file : writing to a new file. second line. read the modified file : writing to a new file. second line. adding to the file. adding another line. checkpoint write ( ) and close ( ) access multimedia content ( https : / / openstax. org / books / introduction - python - progr... | openstax_introduction_to_python_programming_-_web | [
0.003908241633325815,
0.015077189542353153,
0.024442795664072037,
0.0020787979010492563,
0.014126446098089218,
-0.04400812089443207,
-0.00724496366456151,
-0.010050840675830841,
0.04618183895945549,
0.08466210961341858,
-0.03100697137415409,
0.006719690747559071,
0.010290875099599361,
-0.0... |
( ). out. txt should only have the line of text that you have written. access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 14 - 2 - writing - to - files ) try it writing to an existing file add two lines of text to the file called out. log, such that out. log ha... | openstax_introduction_to_python_programming_-_web | [
0.010750138200819492,
-0.0033167805522680283,
0.001866531907580793,
-0.005236128345131874,
0.013881595805287361,
-0.02541692741215229,
-0.011493268422782421,
0.007613408844918013,
0.05545312538743019,
0.07278377562761307,
-0.007312043569982052,
0.01081324927508831,
0.008306884206831455,
-0... |
14. 3 files in different locations and working with csv files learning objectives by the end of this section you should be able to • demonstrate how to access files within a file system. • demonstrate how to process a csv file. opening a file at any location when only the filename is used as the argument to the open ( ... | openstax_introduction_to_python_programming_-_web | [
0.029166074469685555,
0.04744815453886986,
0.0089197913184762,
-0.006268891040235758,
-0.0038993796333670616,
0.010934378020465374,
-0.013397707603871822,
0.007977294735610485,
-0.017747456207871437,
0.11762266606092453,
-0.0073145367205142975,
0.03194718435406685,
-0.0025031876284629107,
... |
14. 3 • files in different locations and working with csv files 335 operating system file location open ( ) function example mac / users / student / fileobj = open ( " / users / student / output. txt " ) linux / usr / code / fileobj = open ( " / usr / code / output. txt " ) windows c : \ projects \ code \ fileobj = ope... | openstax_introduction_to_python_programming_-_web | [
0.0012185784289613366,
0.017678502947092056,
0.03437373787164688,
-0.019764233380556107,
-0.006764892488718033,
-0.019599992781877518,
0.010110028088092804,
-0.006700179073959589,
-0.018570950254797935,
0.09753036499023438,
-0.024813029915094376,
0.03966545686125755,
-0.005530360620468855,
... |
" c : / documents / out. txt ",'a') c. fileobj = open ( " c : / documents / out. txt " ) 3. / users / turtle / logs on a linux computer for writing a. fileobj = open ( " out. txt " ) b. fileobj = open ( " / users / turtle / logs / out. txt " ) c. fileobj = open ( " / users / turtle / logs / out. txt ",'w') 4. c : \ pro... | openstax_introduction_to_python_programming_-_web | [
0.025528473779559135,
0.014741072431206703,
0.04044109210371971,
-0.015280262567102909,
-0.011698816902935505,
-0.024828702211380005,
0.015852464362978935,
-0.007328039035201073,
0.025750400498509407,
0.06886961311101913,
-0.03226170316338539,
0.014418949373066425,
-0.025589944794774055,
-... |
##y dick, herman melville, 538 example 14. 3 processing a csv file " " " processing a csv file. " " " # open the csv file for reading file _ obj = open ( " books. csv " ) # rows are separated by newline \ n characters, so readlines ( ) can be used to read in all rows into a string list csv _ rows = file _ obj. readline... | openstax_introduction_to_python_programming_-_web | [
0.04774380847811699,
-0.0015027389163151383,
0.05650366470217705,
-0.018585074692964554,
-0.02706543728709221,
-0.05152624845504761,
0.012412630952894688,
-0.02776164934039116,
0.022541502490639687,
0.09755497425794601,
0.01635323464870453,
-0.0007324403850361705,
0.02542964182794094,
0.01... |
14. 3 • files in different locations and working with csv files 337 # remove \ n character row = row. strip ( " \ n " ) # split using commas cells = row. split ( ", " ) list _ csv. append ( cells ) # print result print ( list _ csv ) the code's output is : [ ['title ','author ','pages'], ['1984 ','george orwell ','268'... | openstax_introduction_to_python_programming_-_web | [
0.01711014099419117,
-0.01273563876748085,
0.059373289346694946,
-0.021098041906952858,
-0.02544557861983776,
-0.03613891452550888,
0.01813521608710289,
-0.03871378302574158,
0.02318003587424755,
0.07681087404489517,
-0.014792513102293015,
-0.007552786264568567,
-0.010093809105455875,
-0.0... |
, 538'] b. [ ['title ','author ','pages'], ['1984 ','george orwell ','268'], ['jane eyre ','charlotte bronte ','532'], ['walden ','henry david thoreau ','156'], ['moby dick ','herman melville ','538'] ] c. title, author, pages 338 14 • files access for free at openstax. org exploring further files such as word document... | openstax_introduction_to_python_programming_-_web | [
0.00038631135248579085,
-0.012488234788179398,
0.0429861880838871,
0.003877239068970084,
-0.004969608038663864,
-0.012247665785253048,
-0.006945985369384289,
-0.03626282140612602,
0.02679683454334736,
0.05700964480638504,
-0.0034487079828977585,
0.00889706239104271,
0.025982627645134926,
-... |
exam. write a program to display the average score. access multimedia content ( https : / / openstax. org / books / introduction - python - programming / pages / 14 - 3 - files - in - different - locations - and - working - with - csv - files ) 14. 4 handling exceptions learning objectives by the end of this section yo... | openstax_introduction_to_python_programming_-_web | [
-0.011048968881368637,
-0.004884640220552683,
0.021323731169104576,
-0.0012127103982493281,
0.021343395113945007,
-0.00008437778160441667,
0.008539818227291107,
-0.03459562361240387,
0.008683906868100166,
0.10326031595468521,
-0.013837707228958607,
0.02636953815817833,
0.03105085901916027,
... |
14. 4 • handling exceptions 339 example 14. 4 typo in a file a file named food _ order. txt has the following contents : 5 sandwiches 4 chips 1 pickle soft drinks the following program expects each line of the file to begin with an integer : for line in open ( " food _ order. txt " ) : space = line. index ( " " ) qty =... | openstax_introduction_to_python_programming_-_web | [
-0.03921583294868469,
-0.02594112791121006,
0.02396523207426071,
-0.013458597473800182,
0.003791352268308401,
-0.01791347563266754,
0.021302415058016777,
-0.024656549096107483,
-0.02263350412249565,
0.09895160794258118,
0.017399486154317856,
0.01897967793047428,
0.008418940007686615,
0.011... |
the exception. example 14. 5 try to open a file the following program, named try _ open. py, asks the user for a filename and counts the number of lines in the file. name = input ( " enter a filename : " ) try : file = open ( name ) lines = file. readlines ( ) count = len ( lines ) print ( name, " has ", count, " lines... | openstax_introduction_to_python_programming_-_web | [
0.0034455684944987297,
-0.0019374762196093798,
0.011885825544595718,
-0.0073919291608035564,
0.01624048873782158,
-0.003849858185276389,
-0.01752450503408909,
0.002019815146923065,
0.006743096746504307,
0.10339515656232834,
0.02402888797223568,
-0.0036807486321777105,
0.026738133281469345,
... |
14. 4 • handling exceptions 341 enter a filename : try _ open. txt file not found : try _ open. txt have a nice day! concepts in practice predicting output with exceptions for each code snippet, what is the output? 4. word = " one " try : number = int ( word ) print ( word, " equals ", number ) except valueerror : prin... | openstax_introduction_to_python_programming_-_web | [
-0.008710246533155441,
-0.021953066810965538,
0.012961439788341522,
0.002529412740841508,
-0.004718707874417305,
-0.02545349858701229,
-0.00032646197360008955,
-0.03316635265946388,
-0.0006326442817226052,
0.09080799669027328,
0.012653953395783901,
-0.00026406909455545247,
0.0131057426333427... |
the provided main block calls get _ type ( ) for each word in a file. get _ type ( ) should return either " int ", " float ", or " str ", based on the word. the output for the provided data. txt is : str : hello int : 100 str : times! float : 3. 14159 str : is str : pi. access multimedia content ( https : / / openstax.... | openstax_introduction_to_python_programming_-_web | [
0.014594316482543945,
-0.019848253577947617,
0.008829569444060326,
-0.006661841180175543,
-0.03908785060048103,
-0.0033101828303188086,
0.015204831026494503,
-0.016853418201208115,
-0.009208589792251587,
0.047467220574617386,
0.03135530278086662,
-0.012179909273982048,
-0.040054965764284134,... |
14. 4 • handling exceptions 343 ( https : / / openstax. org / r / 100countries ) from worldometer. each line of the file includes a country's name, population, land area, and population density, separated by commas. the user might incorrectly type the filename ( ex : countries. txt instead of countries. csv ). your pro... | openstax_introduction_to_python_programming_-_web | [
-0.013306064531207085,
0.008366581983864307,
0.00268776947632432,
0.016112372279167175,
-0.02334916591644287,
0.000843954854644835,
-0.01688515767455101,
-0.010056225582957268,
0.02852996625006199,
0.0822448581457138,
0.01974995620548725,
-0.0032386595848947763,
-0.0178987979888916,
0.0051... |
14. 5 raising exceptions learning objectives by the end of this section you should be able to • use a raise statement to indicate that user input is invalid. • explain the flow of execution when an exception is raised. the raise statement a program can raise an exception when an error is detected. raising an exception ... | openstax_introduction_to_python_programming_-_web | [
-0.009414629079401493,
-0.020480496808886528,
0.004018004052340984,
0.006618725135922432,
-0.0044023990631103516,
0.0027828929014503956,
0.02567434310913086,
-0.019163092598319054,
-0.01367376558482647,
0.0884397029876709,
0.0023360291961580515,
0.022855408489704132,
0.023977981880307198,
... |
pizza. py ", line 23, in < module > p2 = pizza ( " z " ) file " pizza. py ", line 12, in _ _ init _ _ raise valueerror ( f " unknown size'{ size }'" ) valueerror : unknown size'z'raising an exception in a constructor prevents an invalid object from being constructed. in the example, p1 is constructed but p2 is not cons... | openstax_introduction_to_python_programming_-_web | [
-0.003043562173843384,
-0.015173453837633133,
0.010719534941017628,
-0.031835880130529404,
-0.039720453321933746,
-0.018845131620764732,
0.00881616584956646,
0.008785479702055454,
-0.016330335289239883,
0.06575892865657806,
0.045455094426870346,
0.005664426367729902,
0.01720190979540348,
0... |
14. 5 • raising exceptions 345 c. typeerror : missing argument 2. raise pizzaerror ( " invalid size " ) a. invalid size b. pizzaerror : invalid size c. nameerror : name'pizzaerror'is not defined 3. raise valueerror a. valueerror b. nameerror : name'valueerror'is not defined c. syntaxerror : missing parenthesis in call ... | openstax_introduction_to_python_programming_-_web | [
-0.004919792991131544,
-0.00998471025377512,
0.0002642718027345836,
-0.004727414809167385,
-0.006032963283360004,
-0.005692003294825554,
0.011470029130578041,
-0.011187168769538403,
-0.023066701367497444,
0.06962205469608307,
0.00801476463675499,
0.0030407728627324104,
-0.00818978063762188,
... |
the same as " apple ". likewise, fruit [ " nana " ] raises a keyerror because " nana " does not exactly match " banana ". implement the lookup ( ) function. this function tries to find a key in the dictionary that matches the search term. a key matches if the search term is a substring of the key, ignoring case. ex : t... | openstax_introduction_to_python_programming_-_web | [
0.002275235252454877,
-0.006299965549260378,
-0.007582716643810272,
0.01914588175714016,
-0.011361759155988693,
-0.03499503433704376,
0.016094064339995384,
0.007664889097213745,
0.01925795152783394,
0.06334387511014938,
0.016662264242768288,
-0.017184749245643616,
-0.011408725753426552,
0.... |
14. 6 chapter summary highlights from this chapter include : • python can read and write data in files using the built - in open ( ) function. • due to buffering, changes to a file may not be visible until the file is closed. • newline characters need to be removed / added when reading / writing files. • comma - separa... | openstax_introduction_to_python_programming_-_web | [
-0.008341713808476925,
-0.013532016426324844,
0.023226769641041756,
0.00531136617064476,
0.014092815108597279,
-0.0001564109552418813,
0.012575355358421803,
-0.008525985293090343,
0.005395292304456234,
0.10203514248132706,
-0.01014905795454979,
0.014759802259504795,
0.020429423078894615,
-... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.