Skip to content

Commit 8f78a86

Browse files
committed
added two more print related exercises
1 parent 24ddf8e commit 8f78a86

File tree

5 files changed

+296
-1
lines changed

5 files changed

+296
-1
lines changed

problem_sets/print/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
These exercises do not need to be completed in order, but they are generally ordered in increasing difficulty. The solutions provided _are not the only possible_ solution. Your's might look different, and that is okay!
44

5-
1. Using the `print()` function, print at least four lines of your favorite poem or song lyrics. Do this on multiple lines. [Exercise link](https://colab.research.google.com/github/PDXPythonPirates/python-practice-problems/blob/main/problem_sets/print/exercises/01_print_multiple_lines.ipynb). [Solution link](https://colab.research.google.com/github/PDXPythonPirates/python-practice-problems/blob/main/problem_sets/print/solutions/01_print_multiple_lines.ipynb).
5+
1. Using the `print()` function, print at least four lines of your favorite poem or song lyrics. Do this on multiple lines. [Exercise link](https://colab.research.google.com/github/PDXPythonPirates/python-practice-problems/blob/main/problem_sets/print/exercises/01_print_multiple_lines.ipynb). [Solution link](https://colab.research.google.com/github/PDXPythonPirates/python-practice-problems/blob/main/problem_sets/print/solutions/01_print_multiple_lines.ipynb).
6+
7+
2. Given three strings stored as variables, print the strings separated by a space. Then print the strings separated by commas. [Exercise link](https://colab.research.google.com/github/PDXPythonPirates/python-practice-problems/blob/main/problem_sets/print/exercises/02_print_multiple_objects.ipynb). [Solution link](https://colab.research.google.com/github/PDXPythonPirates/python-practice-problems/blob/main/problem_sets/print/solutions/02_print_multiple_objects.ipynb).
8+
9+
3. Using the `time` module's `sleep()` function, print the string `'Waiting five seconds ...'`, then wait five seconds and print `'done waiting.`' on the same line of output. [Exercise link](https://colab.research.google.com/github/PDXPythonPirates/python-practice-problems/blob/main/problem_sets/print/exercises/03_print_modify_newline.ipynb). [Solution link](https://colab.research.google.com/github/PDXPythonPirates/python-practice-problems/blob/main/problem_sets/print/solutions/03_print_modify_newline.ipynb).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Print multiple objects\n",
8+
"\n",
9+
"This exercise has two parts. \n",
10+
"\n",
11+
"1) Given three strings stored as variables, print the strings separated by a space."
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Run this cell to create string objects\n",
21+
"string_1 = 'The first string'\n",
22+
"string_2 = 'and the second'\n",
23+
"string_3 = 'and the third.'"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"# Code solution here."
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"metadata": {},
38+
"source": [
39+
"2) Print the strings separated by commas."
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": null,
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"# Code solution here."
49+
]
50+
}
51+
],
52+
"metadata": {
53+
"interpreter": {
54+
"hash": "397704579725e15f5c7cb49fe5f0341eb7531c82d19f2c29d197e8b64ab5776b"
55+
},
56+
"kernelspec": {
57+
"display_name": "Python 3.9.7 64-bit",
58+
"language": "python",
59+
"name": "python3"
60+
},
61+
"language_info": {
62+
"name": "python",
63+
"version": "3.9.7"
64+
},
65+
"orig_nbformat": 4
66+
},
67+
"nbformat": 4,
68+
"nbformat_minor": 2
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Print while modifying the newline argument\n",
8+
"\n",
9+
"Using the `time` module's `sleep()` function, print the string `'Waiting five seconds ...'`, then wait five seconds and print `'done waiting.`' on the same line of output.\n",
10+
"\n",
11+
"A little help on this one since we haven't covered importing modules at this point. Modules contain python code that falls outside of builtin functions, so they must be imported before they can be used. An example is provided in the next cell."
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from time import sleep\n",
21+
"\n",
22+
"sleep(3)\n",
23+
"print('This waited three seconds before printing.')"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"# Code solution here."
33+
]
34+
}
35+
],
36+
"metadata": {
37+
"interpreter": {
38+
"hash": "397704579725e15f5c7cb49fe5f0341eb7531c82d19f2c29d197e8b64ab5776b"
39+
},
40+
"kernelspec": {
41+
"display_name": "Python 3.9.7 64-bit",
42+
"language": "python",
43+
"name": "python3"
44+
},
45+
"language_info": {
46+
"codemirror_mode": {
47+
"name": "ipython",
48+
"version": 3
49+
},
50+
"file_extension": ".py",
51+
"mimetype": "text/x-python",
52+
"name": "python",
53+
"nbconvert_exporter": "python",
54+
"pygments_lexer": "ipython3",
55+
"version": "3.9.7"
56+
},
57+
"orig_nbformat": 4
58+
},
59+
"nbformat": 4,
60+
"nbformat_minor": 2
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Print multiple objects\n",
8+
"\n",
9+
"This exercise has two parts. \n",
10+
"\n",
11+
"1) Given three strings stored as variables, print the strings separated by a space."
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Run this cell to create string objects\n",
21+
"string_1 = 'The first string'\n",
22+
"string_2 = 'and the second'\n",
23+
"string_3 = 'and the third.'"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"The `print()` function can take multiple input objects, and the default separator between the output is a space, so we can pass each object to `print()`."
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"print(string_1, string_2, string_3)"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"2) Print the strings separated by commas."
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"metadata": {},
52+
"source": [
53+
"We can modify how the values of these objects in the output are separated by modifying the `sep` attribute."
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"print(string_1, string_2, string_3, sep=', ')"
63+
]
64+
}
65+
],
66+
"metadata": {
67+
"interpreter": {
68+
"hash": "397704579725e15f5c7cb49fe5f0341eb7531c82d19f2c29d197e8b64ab5776b"
69+
},
70+
"kernelspec": {
71+
"display_name": "Python 3.9.7 64-bit",
72+
"language": "python",
73+
"name": "python3"
74+
},
75+
"language_info": {
76+
"codemirror_mode": {
77+
"name": "ipython",
78+
"version": 3
79+
},
80+
"file_extension": ".py",
81+
"mimetype": "text/x-python",
82+
"name": "python",
83+
"nbconvert_exporter": "python",
84+
"pygments_lexer": "ipython3",
85+
"version": "3.9.7"
86+
},
87+
"orig_nbformat": 4
88+
},
89+
"nbformat": 4,
90+
"nbformat_minor": 2
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Print while modifying the newline argument\n",
8+
"\n",
9+
"Using the `time` module's `sleep()` function, print the string `'Waiting five seconds ...'`, then wait five seconds and print `'done waiting.`' on the same line of output.\n",
10+
"\n",
11+
"A little help on this one since we haven't covered importing modules at this point. Modules contain python code that falls outside of builtin functions, so they must be imported before they can be used. An example is provided in the next cell."
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from time import sleep\n",
21+
"\n",
22+
"sleep(3)\n",
23+
"print('This waited three seconds before printing.')"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"The `print()` function has a keyword argument called `end`, which dictates what will be printed _after_ printing the values of the input objects. By default `end='\\n'`, which goes to the next line. That argument can be modified to accomplish the ask here."
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"print('Waiting five seconds ...', end=' ')\n",
40+
"sleep(5)\n",
41+
"print('done waiting.')"
42+
]
43+
}
44+
],
45+
"metadata": {
46+
"interpreter": {
47+
"hash": "397704579725e15f5c7cb49fe5f0341eb7531c82d19f2c29d197e8b64ab5776b"
48+
},
49+
"kernelspec": {
50+
"display_name": "Python 3.9.7 64-bit",
51+
"language": "python",
52+
"name": "python3"
53+
},
54+
"language_info": {
55+
"codemirror_mode": {
56+
"name": "ipython",
57+
"version": 3
58+
},
59+
"file_extension": ".py",
60+
"mimetype": "text/x-python",
61+
"name": "python",
62+
"nbconvert_exporter": "python",
63+
"pygments_lexer": "ipython3",
64+
"version": "3.9.7"
65+
},
66+
"orig_nbformat": 4
67+
},
68+
"nbformat": 4,
69+
"nbformat_minor": 2
70+
}

0 commit comments

Comments
 (0)