Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
About
People
Publications
Teaching
Resources
Research Blog
Wiki Functions
Recent changes
Help
Licensing
Page
Discussion
Edit
View history
Editing
DS4UX (Spring 2016)/Day 3 lecture
(section)
From CommunityData
Jump to:
navigation
,
search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===More string functions=== ==== Formatting strings ==== Formatting strings makes it much easier to combine alphanumeric characters and other types of object (like ints, floats, and bools) and do things with them—like print! >>> x = 1 >>> y = 1.234 >>> z = True >>> w = "elevator" >>> all_together_now = "You can put ints like %d, floating point numbers like %f, boolean values like %s, and other strings like %s into a string without changing them to strings first!" % (x,y,z,w) >>> print(all_together_now) ==== Dealing with whitespace ==== >>> text = " this is a text string with lots of extra spaces " >>> text.strip() "this is a text string with lots of extra spaces" >>> text.split() ['this', 'is', 'a', 'text', 'string', 'with', 'lots', 'of', 'extra', 'spaces'] >>> " ".join(text.split()) 'this is a text string with lots of extra spaces' ==== Tuples ==== Tuples are similar to lists, but unlike lists, once they're created ("assigned") they can't be changed. Since most of our work involves reading and writing files and building and manipulating sets of data, we might not have too much cause to use tuples. But Python uses them a lot "behind the scenes", and they're useful for other types of programming, so we'll go over them briefly here. You can create a tuple just like a list... >>> my_tuple = ("John", "Terry", "Terry", "Graham", "Eric") You can find items by index... >>> my_tuple[1] 'Terry' BUT you can't edit them... >>> my_tuple[1] = "John" --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-63-2dfac7e646ea> in <module>() ----> 1 my_tuple[1] = "Michael" TypeError: 'tuple' object does not support item assignment ====Generating a list of numbers easily with <code>range()</code>==== <pre> >>> range(5) [0, 1, 2, 3, 4] >>> for i in range(5): ... print("Hi" * i) ... Hi HiHi HiHiHi HiHiHiHi</pre> The <code>range()</code> function returns a list of numbers. This is handy for when you want to generate a list of numbers on the fly instead of creating the list yourself. >>> range(5) [0, 1, 2, 3, 4] Use <code>range</code> when you want to loop over a bunch of numbers in a list, or perform an operation a certain number of times: >>> numbers = range(5) >>> for number in numbers: ... print(number * number) ... 0 1 4 9 16 We could rewrite the above example like this: >>> for number in range(5): ... print(number * number) ... 0 1 4 9 16 You can also set the start, end, and increment value (called "step") for a range. >>> for i in range(2,20,2): ... print(i) 2 4 6 8 10 12 14 16 18
Summary:
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see
CommunityData:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Tools
What links here
Related changes
Special pages
Page information