1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
{
"metadata": {
"name": "Chapter 6"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": "Chapter 6: Decision and control statements"
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 6.1, Page number: 114"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 6.1.py\n# To print the Fibonacci series upto 100\n\n\n# Variable declaration\nold_number = 1\ncurrent_number = 1\n\nprint ('1')\n\n# Calculation and result\nwhile (current_number < 100) :\n print (current_number)\n next_number = current_number + old_number\n\n old_number = current_number\n current_number = next_number",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1\n1\n2\n3\n5\n8\n13\n21\n34\n55\n89\n"
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 6.2, Page number: 115"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 6.2.py\n# To calculate the number of items entered\n\n\n# Variable declaration\ntotal = 0\ni = 0\n\n# Calculation\nwhile (i < 10) :\n item = 1\n\n if item == 0 :\n break\n\n total += item\n print ('Total: %d' % total)\n i = i + 1\n \n# Result\nprint ('Final total: %d' % total)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Total: 1\nTotal: 2\nTotal: 3\nTotal: 4\nTotal: 5\nTotal: 6\nTotal: 7\nTotal: 8\nTotal: 9\nTotal: 10\nFinal total: 10\n"
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 6.3, Page number: 116"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 6.3.py\n# To calculate the total number of items entered and omit the negative items\n\n\n# Variable declaration\ntotal = 0\nminus_items = 0\ni = 0\n\n# Calculation\nwhile (i < 10) :\n item = 1\n \n if item == 0 :\n break\n\n if item < 0 :\n minus_items += 1\n continue\n\n total += item\n print ('Total: %d' % total)\n i = i + 1\n \n# Result\nprint ('Final total: %d' % total)\nprint ('with %d negative items omitted' % minus_items)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Total: 1\nTotal: 2\nTotal: 3\nTotal: 4\nTotal: 5\nTotal: 6\nTotal: 7\nTotal: 8\nTotal: 9\nTotal: 10\nFinal total: 10\nwith 0 negative items omitted\n"
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 6.4, Page number: 118"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 6.4.py\n# To calculate the number of dollars owed\n\n\n# Variable declaration\nbalance_owed = 100\n\n# Calculation and result\nif balance_owed == 0 :\n print ('You owe nothing.')\nelse :\n print ('You owe %d dollars.' % balance_owed)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "You owe 100 dollars.\n"
}
],
"prompt_number": 3
}
],
"metadata": {}
}
]
}
|