Skip to content

Commit 1c64aab

Browse files
authored
Add files via upload
1 parent e337a27 commit 1c64aab

File tree

2 files changed

+758
-0
lines changed

2 files changed

+758
-0
lines changed

Logic_functions.ipynb

Lines changed: 379 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,379 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Logic functions"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {
14+
"collapsed": true
15+
},
16+
"outputs": [],
17+
"source": [
18+
"import numpy as np"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [
28+
{
29+
"data": {
30+
"text/plain": [
31+
"'1.11.2'"
32+
]
33+
},
34+
"execution_count": 2,
35+
"metadata": {},
36+
"output_type": "execute_result"
37+
}
38+
],
39+
"source": [
40+
"np.__version__"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"## Truth value testing\n"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"Q1. Let x be an arbitrary. Return True if none of the elements of x is zero. Remind that 0 evaluates to False in python.\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 4,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [
64+
{
65+
"name": "stdout",
66+
"output_type": "stream",
67+
"text": [
68+
"True\n",
69+
"False\n"
70+
]
71+
}
72+
],
73+
"source": [
74+
"x = np.array([1,2,3])\n",
75+
"#\n",
76+
"\n",
77+
"x = np.array([1,0,3])\n",
78+
"#"
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
84+
"source": [
85+
"Q2. Let x be an arbitrary. Return True if any of the elements of x is non-zero."
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": 5,
91+
"metadata": {
92+
"collapsed": false
93+
},
94+
"outputs": [
95+
{
96+
"name": "stdout",
97+
"output_type": "stream",
98+
"text": [
99+
"True\n",
100+
"False\n"
101+
]
102+
}
103+
],
104+
"source": [
105+
"x = np.array([1,0,0])\n",
106+
"#\n",
107+
"\n",
108+
"x = np.array([0,0,0])\n",
109+
"#"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {},
115+
"source": [
116+
"## Array contents\n"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {},
122+
"source": [
123+
"Q3. Predict the result of the following code."
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": 8,
129+
"metadata": {
130+
"collapsed": false
131+
},
132+
"outputs": [],
133+
"source": [
134+
"x = np.array([1, 0, np.nan, np.inf])\n",
135+
"#print np.isfinite(x)"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"Q4. Predict the result of the following code."
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 10,
148+
"metadata": {
149+
"collapsed": false
150+
},
151+
"outputs": [],
152+
"source": [
153+
"x = np.array([1, 0, np.nan, np.inf])\n",
154+
"#print np.isinf(x)"
155+
]
156+
},
157+
{
158+
"cell_type": "markdown",
159+
"metadata": {},
160+
"source": [
161+
"Q5. Predict the result of the following code."
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": 12,
167+
"metadata": {
168+
"collapsed": true
169+
},
170+
"outputs": [],
171+
"source": [
172+
"x = np.array([1, 0, np.nan, np.inf])\n",
173+
"#print np.isnan(x)"
174+
]
175+
},
176+
{
177+
"cell_type": "markdown",
178+
"metadata": {},
179+
"source": [
180+
"## Array type testing"
181+
]
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"metadata": {},
186+
"source": [
187+
"Q6. Predict the result of the following code."
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": 15,
193+
"metadata": {
194+
"collapsed": false
195+
},
196+
"outputs": [],
197+
"source": [
198+
"x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])\n",
199+
"#print np.iscomplex(x)"
200+
]
201+
},
202+
{
203+
"cell_type": "markdown",
204+
"metadata": {},
205+
"source": [
206+
"Q7. Predict the result of the following code."
207+
]
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": 18,
212+
"metadata": {
213+
"collapsed": false
214+
},
215+
"outputs": [],
216+
"source": [
217+
"x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])\n",
218+
"#print np.isreal(x)"
219+
]
220+
},
221+
{
222+
"cell_type": "markdown",
223+
"metadata": {},
224+
"source": [
225+
"Q8. Predict the result of the following code."
226+
]
227+
},
228+
{
229+
"cell_type": "code",
230+
"execution_count": 21,
231+
"metadata": {
232+
"collapsed": false
233+
},
234+
"outputs": [],
235+
"source": [
236+
"#print np.isscalar(3)\n",
237+
"#print np.isscalar([3])\n",
238+
"#print np.isscalar(True)"
239+
]
240+
},
241+
{
242+
"cell_type": "markdown",
243+
"metadata": {},
244+
"source": [
245+
"## Logical operations"
246+
]
247+
},
248+
{
249+
"cell_type": "markdown",
250+
"metadata": {},
251+
"source": [
252+
"Q9. Predict the result of the following code."
253+
]
254+
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": 31,
258+
"metadata": {
259+
"collapsed": false
260+
},
261+
"outputs": [],
262+
"source": [
263+
"#print np.logical_and([True, False], [False, False])\n",
264+
"#print np.logical_or([True, False, True], [True, False, False])\n",
265+
"#print np.logical_xor([True, False, True], [True, False, False])\n",
266+
"#print np.logical_not([True, False, 0, 1])\n"
267+
]
268+
},
269+
{
270+
"cell_type": "markdown",
271+
"metadata": {},
272+
"source": [
273+
"## Comparison"
274+
]
275+
},
276+
{
277+
"cell_type": "markdown",
278+
"metadata": {},
279+
"source": [
280+
"Q10. Predict the result of the following code."
281+
]
282+
},
283+
{
284+
"cell_type": "code",
285+
"execution_count": 42,
286+
"metadata": {
287+
"collapsed": false
288+
},
289+
"outputs": [],
290+
"source": [
291+
"#print np.allclose([3], [2.999999])\n",
292+
"#print np.array_equal([3], [2.999999])"
293+
]
294+
},
295+
{
296+
"cell_type": "markdown",
297+
"metadata": {},
298+
"source": [
299+
"Q11. Write numpy comparison functions such that they return the results as you see."
300+
]
301+
},
302+
{
303+
"cell_type": "code",
304+
"execution_count": 51,
305+
"metadata": {
306+
"collapsed": false
307+
},
308+
"outputs": [
309+
{
310+
"name": "stdout",
311+
"output_type": "stream",
312+
"text": [
313+
"[ True False]\n",
314+
"[ True True]\n",
315+
"[False False]\n",
316+
"[False True]\n"
317+
]
318+
}
319+
],
320+
"source": [
321+
"x = np.array([4, 5])\n",
322+
"y = np.array([2, 5])\n",
323+
"#\n",
324+
"#\n",
325+
"#\n",
326+
"#"
327+
]
328+
},
329+
{
330+
"cell_type": "markdown",
331+
"metadata": {},
332+
"source": [
333+
"Q12. Predict the result of the following code."
334+
]
335+
},
336+
{
337+
"cell_type": "code",
338+
"execution_count": 50,
339+
"metadata": {
340+
"collapsed": false
341+
},
342+
"outputs": [],
343+
"source": [
344+
"#print np.equal([1, 2], [1, 2.000001])\n",
345+
"#print np.isclose([1, 2], [1, 2.000001])"
346+
]
347+
},
348+
{
349+
"cell_type": "code",
350+
"execution_count": null,
351+
"metadata": {
352+
"collapsed": true
353+
},
354+
"outputs": [],
355+
"source": []
356+
}
357+
],
358+
"metadata": {
359+
"kernelspec": {
360+
"display_name": "Python 2",
361+
"language": "python",
362+
"name": "python2"
363+
},
364+
"language_info": {
365+
"codemirror_mode": {
366+
"name": "ipython",
367+
"version": 2
368+
},
369+
"file_extension": ".py",
370+
"mimetype": "text/x-python",
371+
"name": "python",
372+
"nbconvert_exporter": "python",
373+
"pygments_lexer": "ipython2",
374+
"version": "2.7.12"
375+
}
376+
},
377+
"nbformat": 4,
378+
"nbformat_minor": 2
379+
}

0 commit comments

Comments
 (0)