Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Jul 2017 |
Last Sign in: | 210 Weeks Ago, 1 Day Ago |
Questions Answered: | 15833 |
Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Good Afternoon My Friend!
I was wondering if I could get some assistance with these Python examples. Thank you in advance for the help!
Module 4 Case introduces Python lists, tuples, and strings. Your assignment is to create mini-Python projects or programs and to run them in the Python-IDLE environment.
Review the videos and read Chapter 11-13 in the online book of "Python 2: For Beginners Only.”
Use Python to run the following .py Files with the original values and statements. Next, change some of the values to the following. Please keep in mind that statements may change accordingly once you modify the programs. The modified programs must execute correctly.
Code is adapted from MITOpenCourseWare, “A gentle introduction to programming using Python” (under the Open Education Consortium Global Network for Open Education and authorized by the creative commons license).
For Example 1, change “Hi Class!” to “Good Morning All”
For Example 2, change [3, 4, 5, 6] to [1.50, 7, 2.75, 9]
For Example 3, change (5, 6, 7, 8) to (100, 101, 102, 103)
Shaw, Z. (2012). Learn Python the hard way. Retrieved from http://learnpythonthehardway.org/book/ex32.html
Shaw, Z. (2012). Learn Python the hard way. Retrieved from http://learnpythonthehardway.org/book/ex34.html
Shaw, Z. (2012). Learn Python the hard way. Retrieved from http://learnpythonthehardway.org/book/ex40.html
Kindy, M. (2008). Chapter 11: Lists. Python 2: For Beginners Only. Edition1.0. Retrieved from http://cs118.kindy.net/p2fbo_20131230.pdf
Kindy, M. (2008). Chapter 12: Tuples. Python 2: For Beginners Only. Edition1.0. Retrieved from http://cs118.kindy.net/p2fbo_20131230.pdf
Kindy, M. (2008). Chapter 13: Strings. Python 2: For Beginners Only. Edition1.0. Retrieved from http://cs118.kindy.net/p2fbo_20131230.pdf
1 - String Examples.py File
# Lecture 4
# string_examples.py
# Strings
# Define a string
new_string = "Hi Class!"
# Remember we can iterate through it
for letter in new_string:
print letter
# We can concatenate two strings together
s1 = "Hi"
s2 = "Class"
print s1 + s2
# but remember, gluing together with a comma adds an extra space
print s1, s2
# and with a comma you can glue together different data types
print s1, 6.189, s2
# We can index the string
print "new_string[0] is", new_string[0]
# And slice it
print "new_string[0:3] is", new_string[0:3]
# We can get the length of our string using the len function
print "len(new_string) is:", len(new_string)
# And use various string methods on it
print "new_string.upper()", new_string.upper()
print "new_string.lower()", new_string.lower()
2 - List Examples.py File
# Lecture 4
# list_examples.py
# Lists are defined by brackets
new_list = [3, 4, 5, 6]
print "new_list is:", new_list
# Just like strings, we can index & slice them
print "new_list[2] is:", new_list[2]
print "new_list[0:2] is:", new_list[0:2]
# And iterate through them:
for item in new_list:
print item
# Lists, however, are mutable! So, if we want we can change the
# value of one element
new_list[2] = 100
print "new_list is:", new_list
# Or, add on a new element with append:
new_list.append(87)
print "new_list is:", new_list
# Or insert
new_list.insert(0, 200) # insert at position 0 the element 200
print "new_list is:", new_list
# Or even delete an element using remove
new_list.remove(100) # Write in the item that you want to remove from the list
print "new_list is:", new_list
# Lists are possibly the most useful data structure in Python!
# We'll see more about them in lab; check out the documentation on
# list methods for more cool things to do
3 – Tuple Examples.py File
# Lecture 4
# tuple_examples.py
# Tuples are immutable and defined by parentheses
new_tuple = (5, 6, 7, 8)
print "new_tuple is:", new_tuple
# We can index them, just like strings
print "new_tuple[2] is:", new_tuple[2]
# And iterate through them:
for item in new_tuple:
print item
# Even show how long they are
print "Tuple length is:", len(new_tuple)
# and iterate through indicies
for index in range(len(new_tuple)):
print "Index is:", index
print "Value at that index is:", new_tuple[index]
# But because they are immutable, we cannot redefine
# a single element (remember this does work with lists, though)
#new_tuple[1] = 77 # Returns an error
# We can also do something called _tuple unpacking_
(a, b, c, d) = new_tuple
print "a is:", a
print "b is:", b
print "c is:", c
print "d is:", d
# Make sure that you always have the same number of
# variables when you unpack a tuple!
# Tuples are immutable. To change a tuple, we would need
# to first unpack it, change the values, then repack it:
# Redefine b
b = 77
# Repack the tuple
new_tuple = (a, b, c, d)
print "new_tuple is now:", new_tuple
Once you have executed these programs (1 - String Example; 2 - List Examples; and 3- Tuple Examples), modify the .py files according to the instructions given to revise the code by program examples. You need to run the IDLE to execute the program changes and review the program results.
You can use the Snipping tools or screen print (ctrl + Print Screen) to show the Pythons editor’s (IDLE) code and results and demonstrate that your program executed correctly.
Create a submission file named as “ITM205-Case 4-Exercises-YourFirstNameLastName “containing executed programs (.py files) for 1 - String Example; 2 - List Examples; and 3- Tuple Examples.
Write a summary document in Microsoft Word format named “ITM205-Case 4-Summary-YourFirstNameLastName” to show what you have accomplished.
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly