College: | CUNY School of Professional Studies |
---|---|
Course-Name: | Software Application Programming I |
Course-Code: | IS 210 |
Lesson: | 03 |
The warm-up tasks this week will focus on general git repository tasks. You'll be tasked to manipulate files with git's tools prior to submitting the work through the git pull request workflow.
The following tasks will either have you interacting with existing files in the assignment repository or creating new ones on the fly. Don't forget to add your interpreter directive, utf-8 encoding, and a short docstring with any new files that you create!
Important
In these exercises, you may, on occasion, come across a task that requres you to research or use a function or method not directly covered by the course text. Since Python is such a large language it would be impossible for the author to have included descriptions of each and every available function which would largely duplicate the offical Python documentation.
A vital skill to successful programming is being comfortable searching for and using official language documentation sources like the Python String Documentation page. Throughout our coursework we will be practicing both the use of the language in practice and the search skills necessary to become functional programmers.
In the reading, we learned that python strings are immutable, meaning they cannot be changed. Here we'll test for the same property to see if it holds true for the variables that hold strings.
Open
task_01.py
After line 9, add a new line and assign a new value of
Nevermore!
to theRAVEN
variable.Note
Do not change the existing variable declaration. Add a new line instead.
>>> print RAVEN
Nevermore!
Python's order of operations respects parentheses. Create a mathematical statement in a single-line.
- Create a new file
task_02.py
- Create a variable named
WEEKS
and, in a single statement:- Calculate the remainder of
19
divided by10
- Add the result to
100
- Add that result to
2^8
(do exponentiation in Python!) - Divide all of the above by
7
- Calculate the remainder of
>>> print WEEKS
52
In this task, we'll perform a basic slice operation with a string.
- Open
task_03.py
- Use the slice syntax to slice the first
7
characters from theWILL_ROBINSON
variable and assign the result into a new variable namedKLAXON
>>> print WILL_ROBINSON
Danger Will Robinson!
>>> print KLAXON
Danger
Next, we'll try repeating a string. This particular file uses an import to take the KLAXON variable you created in Task 03.
- Open
task_04.py
- On a new line, use the string repetition operator to repeat
KLAXON
five times and save the result back intoKLAXON
Hint
While not required to achieve this objective, you could use an arithmetic assignment operator to achieve this objective.
>>> print KLAXON
Danger Danger Danger Danger Danger
The split()
string function allows us to split a string according to a
specified delimiter and returns a list of the split statements.
- Open
task_05.py
- Use the string
.split()
program to split up theTEENAGE_MUTANT_NINJAS
variable using a period + space'. '
as the delimiter. - Save the result into a new variable named
TURTLE_POWER
>>> print TURTLE_POWER
['Michaelangelo', 'Leonardo', 'Rafael', 'Donatello',
'Heroes in a half shell.']
In this task we're going to use the len()
function to tell us how many
words are found in our copy of Tolstoy's War and Peace.
- Open
task_06.py
- Add a new line and, in a single line, split the text with
split()
and uselen()
to count the number of words. - Save the resulting number in a new variable named
WORDCT
Hint
Python allows you to have multiple functions in the same line
Note
While we won't get to this much later, as you can see, opening files and reading their contents in Python can be very easy to accomplish!
>>> print WORDCT
566316
In this task, we'll use the in
operator to test whether or not a particular
string is found within another string.
- Open
task_07.py
- Use the
in
operator to test whether or not the wordgranaries
exists within theWORDS
variable - Save the result into a variable named
GRANARIES_EXIST
>>> print GRANARIES_EXIST
The strip()
commands are of great help when dealing with poorly formatted
data.
- Open
task_08.py
- Use the
strip()
function to remove whitespace fromNERVOUS_AS
and save the result back into theNERVOUS_AS
variable - In a single-line statement, use
rstrip()
andlstrip()
to remove the commas (,
), and forward slashes (/
) fromNERVOUS_AS
storing the result back into theNERVOUS_AS
variable.
Note
Depending upon what a function returns, it is possible to chain together
multiple function calls as a form of shorthand. This is possible because
these functions either return the original object or an object of the
exact same time (eg, a string) so subsequenct .function()
calls may
be strung together one after another.
>>> print NERVOUS_AS
A long-tailed cat in a room full of rockin' chairs.
One way to achieve a multi-line string is to use triple double or single quotes. This is most commonly docstrings which are a required part of every module.
- Open
task_09.py
- Add a multi-line docstring to
task_09.py
. The docstring should break across two paragraphs. - If you want to test your docstring, try the following commands
from the Python interactive command line by using
help()
.
>>> import task_09
>>> help(task_09)
Press q
to exit the help page for this module.
One of the simple, though useful, string functions available in Python are
the casing functions such as .lower()
and .upper()
.
- Open
task_10.py
- Use a string function that will change
MOVIE
to titlecase and save its result into a new variable namedENTITLED
>>> print ENTITLED
Dr. Strangelove Or: How I Learned To Stop Worrying And Love The Bomb
Learning how to escape special characters and strings is an absolute necessity for any beginning programmer.
- Create a new file called
task_11.py
- Create a new variable called
ESCAPE_STRING
with the value\n'"
Note
In this case, we want the real characters backslash + n, not the escape sequence of a newline.
>>> print ESCAPE_STRING
\n'"
In this task, we'll assign some simple numeric types. You'll need to use the import statement as shown in your course text or video to get access to the decimal and fraction types.
- Create a new file called
task_12.py
- Create a new variable named
INTVAL
and assign it a value of1
- Create a new variable named
FLOATVAL
and assign it a value of0.1
- Create a new variable named
DECVAL
and assign it a value of one-tenth - Create a new variable named
FRACVAL
and assign it a value of one-tenth
Hint
You must import both the decimal
and fractions
modules to get
access to the Decimal
and Fraction
data types.
>>> print INTVAL
1
>>> print FLOATVAL
0.1
>>> print DECVAL
0.1
>>> print FRACVAL
1/10
Testing equality can be tricky with the various mathematical types as they all store data in slightly different ways. Here's we'll take a look at a few cases from what you did in a prior step.
- Open
task_13.py
, this file imports all of the variables you set intask_12.py
- Use the equality comparison operator (
==
) to test ifDECVAL
andFRACVAL
are equal. - Save the result into a new variable named,
FRAC_DEC_EQUAL
- Similarly, use the inequality comparison operator (
!=
) to test ifDECVAL
andFLOATVAL
are inequal - Save the result into a new variable named,
DEC_FLOAT_INEQUAL
Hint
You can access task_12
data through its namespace so, for example, to
access the FLOATVAL
variable from task_12
, you'd do so through
something like task_12.FLOATVAL
. Use this way of addressing the
variables directly; don't reassign them to new variable names.
>>> FRAC_DEC_EQUAL
False
>>> DEC_FLOAT_INEQUAL
True
There are just a few more basic types with which we ought to familiarize ourselves at this point.
- Create a new file named
task_14.py
- Create a new variable named
IS_TRUE
and assign it a value ofTrue
- Create a new variable named
IS_FALSE
and assign it a value ofFalse
- Create a new variabled named
IS_NONE
and assign it a value ofNone
- In a single, one-line statement, use the logical AND operator and the
equality operator to test if
IS_TRUE
is equal to1
andIS_FALSE
is equal to0
- Store the result into a new variable named
INTEGER_EQUIV
>>> print IS_TRUE
True
>>> print IS_FALSE
False
>>> print IS_NONE
None
>>> INTEGER_EQUIV
True
The course text mentions that some types of operations are illegal between
objects of different types. For example, a string cannot be concatenated with
an integer using the concatenation operator (+
) without first converting
the integer to a string.
- Open
task_15.py
- Concatenate the variables
NOT_THE_QUESTION
andANSWER
by using the concatenation operator and thestr()
function. - Store the result into a new variable named
THANKS_FOR_THE_FISH
>>> print THANKS_FOR_THE_FISH
The answer to life, the universe, and everything? It's 42
Code must be functional and pass tests before it will be eligible for credit.
Lint tests check your code for syntactic or stylistic errors To execute lint tests against a specific file, simply open a terminal in the same directory as your code repository and type:
$ pylint filename.py
Where filename.py
is the name of the file you wish to lint test.
Unit tests check that your code performs the tested objectives. Unit tests may be executed individually by opening a terminal in the same directory as your code repository and typing:
$ nosetests tests/name_of_test.py
Where name_of_test.py
is the name of the testfile found in the tests
directory of your source code.
All tests may be run simultaneously by executing the runtests.sh
script
from the root of your assignment repository. To execute all tests, open a
terminal in the same directory as your code repository and type:
$ ./runtests.sh
Code should be submitted via Blackboard as python files.
In order to receive full credit you must complete the assignment as-instructed and without any violations (reported in the build status).