Editing DS4UX (Spring 2016)/Day 3 lecture

From CommunityData
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.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 84: Line 84:
  -1
  -1


===Strings===


 
==== Strings are a lot like lists====
== New concepts for Week 3 exercises and challenges ==
>>> my_string = "Hello World"
 
>>> my_string[0]
===More string functions===
'H'
>>> my_string[:5]
'Hello'
>>> my_string[6:]
'World'
>>> my_string = my_string[:6] + "Jessica"
>>> my_string
'Hello Jessica'
>>> 'H' in my_string
True


==== Formatting strings ====
==== 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
  >>> x = 1
  >>> y = 1.234
  >>> y = 1.234
Line 98: Line 106:
  >>> w = "elevator"
  >>> 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)
  >>> 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 ====
==== Dealing with whitespace ====
Line 109: Line 116:
  'this is a text string with lots of extra spaces'
  'this is a text string with lots of extra spaces'


 
== New concepts for Week 3 exercises and challenges ==
==== 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>====
====Generating a list of numbers easily with <code>range()</code>====


Line 136: Line 123:
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
>>> for i in range(5):
>>> for i in range(5):
...    print("Hi" * i)
...    print "Hi" * i
...
...


Line 171: Line 158:
  9
  9
  16
  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


=== Using break statements to halt execution ===
=== Using break statements to halt execution ===
Line 200: Line 174:
  >>> for i in range(100):
  >>> for i in range(100):
  ...    my_input = input("Please type something> ")
  ...    my_input = input("Please type something> ")
  ...    if my_input == "Quit":
  ...    if input == "Quit":
  ...        print("Goodbye!")
  ...        print("Goodbye!")
  ...        break
  ...        break
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)