Computer Science Help

profilejustatechy

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)

 

 

 

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.

    • 9 years ago
    • 20
    Answer(1)

    Purchase the answer to view it

    blurred-text
    • attachment
      final_answer.zip
    Bids(1)