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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
{
"metadata": {
"name": "",
"signature": "sha256:f68b9bfb9c3fa936de40e570b5ee8d7a82f02160fce8cc4fc8384697c0b86ac7"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 7: Basic Types"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example sum2.c, Page 131"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def main():\n",
" sum=0\n",
" print \"This program sums a series of integers.\"\n",
" n=int(raw_input(\"Enter integers (0 to terminate): \")) #input the integers to operate on\n",
" while(n!=0):\n",
" sum=sum+n #calculating sum till 0 encountered\n",
" n=input()\n",
" print \"The sum is: %d\" % sum #printing sum\n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"This program sums a series of integers.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter integers (0 to terminate): 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"23\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"71\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The sum is: 107\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example length.c, Page 142"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def main():\n",
" str=raw_input(\"Enter a message: \") #input string\n",
" length=len(str) #calculate length\n",
" print \"Your message was %d character(s) long\" % length #display length\n",
"if __name__=='__main__':\n",
" main()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a message: Brevity is the soul of wit.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Your message was 27 character(s) long\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|