{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Python Guide V1.2.11

Iteminlistortupleorstring itemtakes on the value of

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: "x")!#!put!"x"!before!xl[3]! xl.pop(1)! !8.9!#!remove!2nd!entry! !xl!is now![1,!'TV',!'x',!5.7]! • xl.reverse()!#!reverses!order! • xl.sort()!!!!#!in!increasing!order! !xl!is now![1,!5.7,!'TV',!'x']! • xl[1]!+=!2.2!!!#!updates!entry!value! • xl[:2]!=![2,3]!#!assign!to!slice! • xl[:]! ![2,!3,!'TV',!'x']!#!a!copy! • range(5)! ![0,!1,!2,!3,!4]! • range(2,5)! ![2,!3,!4]! • range(2,15,3)! ![2,!5,!8,!11,!14]! Third element of a slice is a step size: • range(50)[::9]! ![0,!9,!18,!27,!36,!45]! • • • • for control structure • for statement followed by indented lines • for!item!in!listOrTupleOrString!:! —item!takes on the value of each entry in turn —indented lines are repeated for each item • total!=!0.0! for!number!in!range(1,101):! !!!!total!+=!1.0!/!number! • Parallel lists list1 and list2: for6e1,!e2!in!zip(list1,!list2):! !!!!print!e1,!e2! • If you need both position and value of list entry: for!pos,!value!in6enumerate(list1):! !!!!print!"Entry6at6%d6is6%g"!%!(pos,value)! ! ! List comprehension (embedded for) • To generate one list from items in another • list2!=![f(n)!for!n!in!list1]! • squares!=![n!*6n!for!n!in!range(90)]! Defining functions • def statement followed by indented lines • def!myFunc(parm1,!parm2,!…)!:! —creates a function!myFunc!with parameters —parmj!is given t...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online