-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
888 lines (649 loc) · 32.3 KB
/
index.html
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Python Notes by celikmustafa89</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="stylesheets/normalize.css" media="screen">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen">
<link rel="stylesheet" type="text/css" href="stylesheets/github-light.css" media="screen">
</head>
<body>
<section class="page-header">
<h1 class="project-name">Python Notes</h1>
<h2 class="project-tagline">Mustafa ÇELİK</h2>
<a href="https://github.com/celikmustafa89/sololearn-python-notes" class="btn">View on GitHub</a>
<a href="https://github.com/celikmustafa89/sololearn-python-notes/zipball/master" class="btn">Download .zip</a>
<a href="https://github.com/celikmustafa89/sololearn-python-notes/tarball/master" class="btn">Download .tar.gz</a>
</section>
<section class="main-content">
<h1 id="python-notes">
<a id="python-notes" class="anchor" href="#python-notes" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong><em>PYTHON NOTES</em></strong>
</h1>
<hr>
<h2 id="table-of-contents">
<a id="table-of-contents" class="anchor" href="#table-of-contents" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Table of Contents</strong>
</h2>
<p></p><div>
<ul>
<li>
<a href="#python-notes">PYTHON NOTES</a><ul>
<li><a href="#table-of-contents">Table of Contents</a></li>
<li>
<a href="#functional-programming">Functional Programming</a><ul>
<li><a href="#lambdas">Lambdas</a></li>
<li><a href="#map">Map</a></li>
<li><a href="#filter">Filter</a></li>
<li><a href="#generators-1">Generators-1</a></li>
<li><a href="#generators-2">Generators-2</a></li>
<li><a href="#generators-3">Generators-3</a></li>
<li><a href="#decorators-1">Decorators-1</a></li>
<li><a href="#decorators-2">Decorators-2</a></li>
<li><a href="#recursion-1">Recursion-1</a></li>
<li><a href="#recursion-2">Recursion-2</a></li>
<li><a href="#recursion-3">Recursion-3</a></li>
<li><a href="#sets-1">Sets-1</a></li>
<li><a href="#sets-2">Sets-2</a></li>
<li><a href="#sets-3">Sets-3</a></li>
<li><a href="#data-structures">Data Structures</a></li>
<li><a href="#itertools-1">itertools-1</a></li>
<li><a href="#itertools-2">itertools-2</a></li>
<li><a href="#itertools-3">itertools-3</a></li>
</ul>
</li>
<li>
<a href="#object-oriented-programming">Object-Oriented Programming</a><ul>
<li>
<a href="#classes-1">Classes-1</a><ul>
<li><a href="#init">__init__</a></li>
<li><a href="#methods">Methods</a></li>
</ul>
</li>
<li><a href="#classes-2">Classes-2</a></li>
<li><a href="#inheritance-1">Inheritance-1</a></li>
<li><a href="#inheritance-2">Inheritance-2</a></li>
<li><a href="#inheritance-3">Inheritance-3</a></li>
<li><a href="#inheritance-4">Inheritance-4</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<p>Source: <a href="http://www.sololearn.com/Play/Python">SoloLearn Python Tutorial</a></p>
<h2 id="functional-programming">
<a id="functional-programming" class="anchor" href="#functional-programming" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong><em>Functional Programming</em></strong>
</h2>
<h3 id="lambdas">
<a id="lambdas" class="anchor" href="#lambdas" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Lambdas</strong>
</h3>
<hr>
<p>Creating a function normally (using <strong>def</strong>) assigns it to a variable automatically. <br>
This is different from the creation of other objects - such as <strong>strings</strong> and <strong>integers</strong> - which can be created on the fly, without assigning them to a variable. <br>
The same is possible with <strong>functions</strong>, provided that they are created using <strong>lambda syntax</strong>. Functions created this way are known as <strong>anonymous</strong>. <br>
This approach is most commonly used when passing a simple function as an argument to another function. The syntax is shown in the next example and consists of the lambda keyword followed by a list of arguments, a <strong>colon</strong>, and the <strong>expression</strong> to <strong>evaluate</strong> and <strong>return</strong>.</p>
<pre><code>def my_func(f, arg):
return f(arg)
my_func(lambda x: 2*x*x, 5)</code></pre>
<blockquote>
<p><strong>Note:</strong> Lambda functions get their name from lambda calculus, which is a model of computation invented by Alonzo Church.</p>
</blockquote>
<h3 id="map">
<a id="map" class="anchor" href="#map" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Map</strong>
</h3>
<hr>
<p>The built-in functions <strong>map</strong> and <strong>filter</strong> are very useful <strong>higher-order functions</strong> that operate on <strong>lists</strong> (or similar objects called iterables). <br>
The function map takes a function and an iterable as arguments, and returns a new iterable with the function applied to each argument.</p>
<p><strong>Example</strong>:</p>
<pre><code>def add_five(x):
return x + 5
nums = [11, 22, 33, 44, 55]
result = list(map(add_five, nums))
print(result)
</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>[16, 27, 38, 49, 60]</p>
</blockquote>
<p>We could have achieved the same result more easily by using <strong>lambda syntax</strong>.</p>
<pre><code>nums = [11, 22, 33, 44, 55]
result = list(map(lambda x: x+5, nums))
print(result)</code></pre>
<blockquote>
<p><strong>Note:</strong> To convert the result into a list, we used list explicitly.</p>
</blockquote>
<h3 id="filter">
<a id="filter" class="anchor" href="#filter" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Filter</strong>
</h3>
<hr>
<p>The function <strong>filter</strong> filters an iterable by removing items that don’t match a predicate (a function that returns a Boolean). <br>
<strong>Example:</strong></p>
<pre><code>nums = [11, 22, 33, 44, 55]
res = list(filter(lambda x: x%2==0, nums))
print(res)</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>[22, 44]</p>
<p><strong>Note:</strong> Like map, the result has to be explicitly converted to a list if you want to print it.</p>
</blockquote>
<p><strong>Example:</strong> <br>
Fill in the blanks to remove all items that are greater than 4 from the list.</p>
<pre><code>nums = [1, 2, 5, 8, 3, 0, 7]
res = list(filter(lambda x: x < 5, nums))
print(res)</code></pre>
<h3 id="generators-1">
<a id="generators-1" class="anchor" href="#generators-1" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Generators-1</strong>
</h3>
<hr>
<p><strong>Generators</strong> are a type of iterable, like lists or tuples. <br>
Unlike lists, they don’t allow indexing with arbitrary indices, but they can still be iterated through with <strong>for</strong> loops. <br>
They can be created using functions and the <strong>yield</strong> statement. <br>
<strong>Example:</strong></p>
<pre><code>def countdown():
i=5
while i > 0:
yield i
i -= 1
for i in countdown():
print(i)</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>5 <br>
4 <br>
3 <br>
2 <br>
1</p>
</blockquote>
<p>The <strong>yield</strong> statement is used to define a <strong>generator</strong>, replacing the return of a function to provide a result to its caller without destroying local variables.</p>
<h3 id="generators-2">
<a id="generators-2" class="anchor" href="#generators-2" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Generators-2</strong>
</h3>
<hr>
<p>Due to the fact that they <strong>yield</strong> one item at a time, generators don’t have the memory restrictions of lists. <br>
In fact, they can be <strong>infinite</strong>!</p>
<pre><code>def infinite_sevens():
while True:
yield 7
for i in infinite_sevens():
print(i)</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>7 <br>
7 <br>
7 <br>
7 <br>
7 <br>
7 <br>
7 <br>
…</p>
</blockquote>
<p><strong>Note:</strong> In short, <strong>generators</strong> allow you to declare a <strong>function</strong> that behaves like an iterator, i.e. it can be used in a <strong>for</strong> loop.</p>
<p><strong>Example:</strong> <br>
Fill in the blanks to create a prime number generator, that yields all prime numbers in a loop. (Consider having an is_prime function already defined):</p>
<pre><code> def get_primes():
num = 2
while True:
if is_prime(num):
yield num
num += 1</code></pre>
<h3 id="generators-3">
<a id="generators-3" class="anchor" href="#generators-3" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Generators-3</strong>
</h3>
<hr>
<p>Finite generators can be converted into lists by passing them as arguments to the <strong>list</strong> function.</p>
<pre><code>def numbers(x):
for i in range(x):
if i % 2 == 0:
yield i
print(list(numbers(11)))</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>[0, 2, 4, 6, 8, 10]</p>
</blockquote>
<p><strong>Note:</strong> Using <strong>generators</strong> results in improved performance, which is the result of the lazy (on demand) generation of values, which translates to lower memory usage. Furthermore, we do not need to wait until all the elements have been generated before we start to use them.</p>
<h3 id="decorators-1">
<a id="decorators-1" class="anchor" href="#decorators-1" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Decorators-1</strong>
</h3>
<hr>
<p><strong>Decorators</strong> provide a way to modify functions using other functions. <br>
This is ideal when you need to extend the functionality of functions that you don’t want to modify. <br>
<strong>Example:</strong></p>
<pre><code>def decor(func):
def wrap():
print("============")
func()
print("============")
return wrap
def print_text():
print("Hello world!")
decorated = decor(print_text)
decorated()</code></pre>
<p>We defined a function named <strong>decor</strong> that has a single parameter <strong>func</strong>. Inside <strong>decor</strong>, we defined a nested function named <strong>wrap</strong>. The <strong>wrap</strong> function will print a string, then call <strong>func()</strong>, and print another string. The <strong>decor</strong> function returns the <strong>wrap</strong> function as its result. <br>
We could say that the variable <strong>decorated</strong> is a decorated version of <strong>print_text</strong> - it’s <strong>print_text</strong> plus something. <br>
In fact, if we wrote a useful <strong>decorator</strong> we might want to replace <strong>print_text</strong> with the decorated version altogether so we always got our “plus something” version of <strong>print_text</strong>. <br>
This is done by re-assigning the variable that contains our function:</p>
<pre><code>print_text = decor(print_text)
print_text()</code></pre>
<blockquote>
<p>Now print_text corresponds to our decorated version.</p>
</blockquote>
<h3 id="decorators-2">
<a id="decorators-2" class="anchor" href="#decorators-2" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Decorators-2</strong>
</h3>
<hr>
<p>In our previous example, we decorated our function by replacing the variable containing the function with a wrapped version.</p>
<pre><code>def print_text():
print("Hello world!")
print_text = decor(print_text)</code></pre>
<p>This pattern can be used at any time, to wrap any function. <br>
Python provides support to wrap a function in a decorator by pre-pending the function definition with a <strong>decorator</strong> name and the <strong>@ symbol</strong>. <br>
If we are defining a function we can “decorate” it with the @ symbol like:</p>
<pre><code>@decor
def print_text():
print("Hello world!")
Try It Yourself</code></pre>
<p>This will have the same result as the above code.</p>
<blockquote>
<p><strong>Note:</strong> A single function can have multiple decorators.</p>
</blockquote>
<h3 id="recursion-1">
<a id="recursion-1" class="anchor" href="#recursion-1" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Recursion-1</strong>
</h3>
<hr>
<p><strong>Recursion</strong> is a very important concept in functional programming. <br>
The fundamental part of recursion is <strong>self-reference - functions calling</strong> themselves. It is used to solve problems that can be broken up into easier sub-problems of the same type.</p>
<p>A classic example of a function that is implemented recursively is the <strong>factorial function</strong>, which finds the product of all positive integers below a specified number. <br>
For example, 5! (5 factorial) is 5 * 4 * 3 * 2 * 1 (120). To implement this recursively, notice that 5! = 5 * 4!, 4! = 4 * 3!, 3! = 3 * 2!, and so on. Generally, n! = n * (n-1)!. <br>
Furthermore, 1! = 1. This is known as the <strong>base case</strong>, as it can be calculated without performing any more factorials. <br>
Below is a recursive implementation of the factorial function.</p>
<pre><code>def factorial(x):
if x == 1:
return 1
else:
return x * factorial(x-1)
print(factorial(5))</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>120</p>
</blockquote>
<p><strong>Note:</strong> <em>The base case acts as the exit condition of the recursion.</em></p>
<h3 id="recursion-2">
<a id="recursion-2" class="anchor" href="#recursion-2" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Recursion-2</strong>
</h3>
<hr>
<p>Recursive functions can be <strong>infinite</strong>, just like infinite <strong>while</strong> loops. These often occur when you forget to implement the base case. <br>
Below is an <strong>incorrect</strong> version of the factorial function. It has <strong>no base case</strong>, so it runs until the interpreter runs out of memory and crashes.</p>
<pre><code>def factorial(x):
return x * factorial(x-1)
print(factorial(5))</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>RuntimeError: maximum recursion depth exceeded</p>
</blockquote>
<h3 id="recursion-3">
<a id="recursion-3" class="anchor" href="#recursion-3" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Recursion-3</strong>
</h3>
<hr>
<p>Recursion can also be <strong>indirect</strong>. One function can call a second, which calls the first, which calls the second, and so on. This can occur with any number of functions. <br>
<strong>Example:</strong></p>
<pre><code>def is_even(x):
if x == 0:
return True
else:
return is_odd(x-1)
def is_odd(x):
return not is_even(x)
print(is_odd(17))
print(is_even(23))</code></pre>
<p><strong>Result</strong>:</p>
<blockquote>
<p>True <br>
False</p>
</blockquote>
<h3 id="sets-1">
<a id="sets-1" class="anchor" href="#sets-1" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Sets-1</strong>
</h3>
<hr>
<p><strong>Sets</strong> are data structures, similar to <strong>lists</strong> or <strong>dictionaries</strong>. They are created using curly braces, or the <strong>set</strong> function. They share some functionality with lists, such as the use of <strong>in</strong> to check whether they contain a particular item.</p>
<pre><code>num_set = {1, 2, 3, 4, 5}
word_set = set(["spam", "eggs", "sausage"])
print(3 in num_set)
print("spam" not in word_set)</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>True <br>
False</p>
</blockquote>
<p><strong>Note:</strong> <em>To create an empty set, you must use <strong>set()</strong>, as <strong>{}</strong> creates an empty dictionary.</em></p>
<h3 id="sets-2">
<a id="sets-2" class="anchor" href="#sets-2" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Sets-2</strong>
</h3>
<hr>
<p>Sets differ from lists in several ways, but share several list operations such as <strong>len</strong>. <br>
They are unordered, which means that they can’t be indexed. <br>
They <strong>cannot contain duplicate</strong> elements. <br>
Due to the way they’re stored, it’s <strong>faster</strong> to check whether an item is part of a set, rather than part of a list. <br>
Instead of using <strong>append</strong> to add to a set, use <strong>add</strong>. <br>
The method <strong>remove</strong> removes a specific element from a set; <strong>pop</strong> removes an arbitrary element.</p>
<pre><code>nums = {1, 2, 1, 3, 1, 4, 5, 6}
print(nums)
nums.add(-7)
nums.remove(3)
print(nums)</code></pre>
<p>Result:</p>
<blockquote>
<p>{1, 2, 3, 4, 5, 6} <br>
{1, 2, 4, 5, 6, -7}</p>
</blockquote>
<p><strong>Note</strong>: <em>Basic uses of <strong>sets</strong> include membership testing and the <strong>elimination of duplicate entries</strong>.</em></p>
<h3 id="sets-3">
<a id="sets-3" class="anchor" href="#sets-3" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Sets-3</strong>
</h3>
<hr>
<p>Sets can be combined using mathematical operations. <br>
The <strong>union</strong> operator <strong>|</strong> combines two sets to form a new one containing items in either. <br>
The <strong>intersection</strong> operator <strong>&</strong> gets items only in both. <br>
The <strong>difference</strong> operator <strong>-</strong> gets items in the first set but not in the second. <br>
The <strong>symmetric</strong> <strong>difference</strong> operator <strong>^</strong> gets items in either set, but not both.</p>
<pre><code>first = {1, 2, 3, 4, 5, 6}
second = {4, 5, 6, 7, 8, 9}
print(first | second)
print(first & second)
print(first - second)
print(second - first)
print(first ^ second)</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>{1, 2, 3, 4, 5, 6, 7, 8, 9} <br>
{4, 5, 6} <br>
{1, 2, 3} <br>
{8, 9, 7} <br>
{1, 2, 3, 7, 8, 9}</p>
</blockquote>
<p>StackEdit stores your documents in your browser, which means all your documents are automatically saved locally and are accessible <strong>offline!</strong></p>
<h3 id="data-structures">
<a id="data-structures" class="anchor" href="#data-structures" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Data Structures</strong>
</h3>
<hr>
<p>As we have seen in the previous lessons, Python supports the following data structures: <strong>lists</strong>, <strong>dictionaries</strong>, <strong>tuples</strong>, <strong>sets</strong>.</p>
<p><strong>When to use a dictionary:</strong></p>
<ul>
<li>When you need a logical association between a <strong>key:value pair</strong>.</li>
<li>When you need <strong>fast lookup</strong> for your data, based on a custom <strong>key</strong>.</li>
<li>When your data is being constantly modified. Remember, dictionaries are <strong>mutable</strong>.</li>
</ul>
<p><strong>When to use the other types:</strong></p>
<ul>
<li>Use <strong>lists</strong> if you have a collection of data that does <strong>not need random access</strong>. Try to choose lists when you need a simple, iterable collection that is modified frequently.</li>
<li>Use a <strong>set</strong> if you need <strong>uniqueness</strong> for the elements. </li>
<li>Use <strong>tuples</strong> when your data cannot change. </li>
</ul>
<blockquote>
<p>Many times, a <strong>tuple</strong> is used in combination with a <strong>dictionary</strong>, for example, a <strong>tuple</strong> might represent a key, because it’s <strong>immutable</strong>.</p>
</blockquote>
<h3 id="itertools-1">
<a id="itertools-1" class="anchor" href="#itertools-1" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>itertools-1</strong>
</h3>
<hr>
<p>The module <strong>itertools</strong> is a standard library that contains several functions that are useful in functional programming. <br>
One type of function it produces is <strong>infinite</strong> iterators. <br>
The function <strong>count</strong> counts up infinitely from a value. <br>
The function <strong>cycle</strong> infinitely iterates through an iterable (for instance a list or string). <br>
The function <strong>repeat</strong> repeats an object, either infinitely or a specific number of times. <br>
<strong>Example:</strong></p>
<pre><code>from itertools import count
for i in count(3):
print(i)
if i >=11:
break</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>3 <br>
4 <br>
5 <br>
6 <br>
7 <br>
8 <br>
9 <br>
10 <br>
11</p>
</blockquote>
<h3 id="itertools-2">
<a id="itertools-2" class="anchor" href="#itertools-2" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>itertools-2</strong>
</h3>
<hr>
<p>There are many functions in <strong>itertools</strong> that operate on iterables, in a similar way to <strong>map</strong> and <strong>filter</strong>. <br>
<strong>Some examples:</strong> <br>
<strong>takewhile</strong> - takes items from an iterable while a predicate function remains true; <br>
<strong>chain</strong> - combines several iterables into one long one; <br>
<strong>accumulate</strong> - returns a running total of values in an iterable.</p>
<pre><code>from itertools import accumulate, takewhile
nums = list(accumulate(range(8)))
print(nums)
print(list(takewhile(lambda x: x<= 6, nums)))</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>[0, 1, 3, 6, 10, 15, 21, 28] <br>
[0, 1, 3, 6]</p>
</blockquote>
<h3 id="itertools-3">
<a id="itertools-3" class="anchor" href="#itertools-3" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>itertools-3</strong>
</h3>
<hr>
<p>There are also several combinatoric functions in <strong>itertool</strong>, such as <strong>product</strong> and <strong>permutation</strong>. <br>
These are used when you want to accomplish a task with all possible combinations of some items. <br>
<strong>Example:</strong></p>
<pre><code>from itertools import product, permutations
letters = ("A", "B")
print(list(product(letters, range(2))))
print(list(permutations(letters))) </code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>[(‘A’, 0), (‘A’, 1), (‘B’, 0), (‘B’, 1)] <br>
[(‘A’, ‘B’), (‘B’, ‘A’)]</p>
</blockquote>
<h2 id="object-oriented-programming">
<a id="object-oriented-programming" class="anchor" href="#object-oriented-programming" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong><em>Object-Oriented Programming</em></strong>
</h2>
<h3 id="classes-1">
<a id="classes-1" class="anchor" href="#classes-1" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Classes-1</strong>
</h3>
<hr>
<p>We have previously looked at two paradigms of programming - <strong>imperative</strong> (using statements, loops, and functions as subroutines), and <strong>functional</strong> (using pure functions, higher-order functions, and recursion).</p>
<p>Another very popular paradigm is <strong>object**</strong>-<strong>**oriented</strong> <strong>programming</strong> (<strong>OOP</strong>). <br>
Objects are created using <strong>classes</strong>, which are actually the focal point of OOP. <br>
The <strong>class</strong> describes what the object will be, but is separate from the object itself. In other words, a class can be described as an object’s blueprint, description, or definition. <br>
You can use the same class as a blueprint for creating multiple different objects. </p>
<p>Classes are created using the keyword <strong>class</strong> and an indented block, which contains class <strong>methods</strong> (which are functions). <br>
Below is an example of a simple class and its objects.</p>
<pre><code>class Cat:
def __init__(self, color, legs):
self.color = color
self.legs = legs
felix = Cat("ginger", 4)
rover = Cat("dog-colored", 4)
stumpy = Cat("brown", 3)</code></pre>
<blockquote>
<p><strong>Note:</strong> This code defines a class named <strong>Cat</strong>, which has two attributes: <strong>color</strong> and <strong>legs</strong>. <br>
Then the class is used to create 3 separate objects of that class.</p>
</blockquote>
<h4 id="init">
<a id="__init__" class="anchor" href="#__init__" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong><code>__init__</code></strong>
</h4>
<hr>
<p>The <strong>init</strong> method is the most important method in a class. <br>
This is called when an instance (object) of the class is created, using the class name as a function.</p>
<p>All methods must have <strong>self</strong> as their first parameter, although it isn’t explicitly passed, Python adds the <strong>self</strong> argument to the list for you; you do not need to include it when you call the methods. Within a method definition, <strong>self</strong> refers to the instance calling the method.</p>
<p>Instances of a class have <strong>attributes</strong>, which are pieces of data associated with them. <br>
In this example, <strong>Cat</strong> instances have attributes <strong>color</strong> and <strong>legs</strong>. These can be accessed by putting a <strong>dot</strong>, and the attribute name after an instance. <br>
In an <strong>init</strong> method, <strong>self.attribute</strong> can therefore be used to set the initial value of an instance’s attributes. <br>
<strong>Example:</strong></p>
<pre><code>class Cat:
def __init__(self, color, legs):
self.color = color
self.legs = legs
felix = Cat("ginger", 4)
print(felix.color)</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p><strong>Note:</strong> ginger</p>
</blockquote>
<p><strong>Note:</strong> In the example above, the <strong>init</strong> method takes two arguments and assigns them to the object’s attributes. The <strong>init</strong> method is called the class <strong>constructor</strong>.</p>
<h4 id="methods">
<a id="methods" class="anchor" href="#methods" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Methods</strong>
</h4>
<hr>
<p>Classes can have other <strong>methods</strong> defined to add functionality to them. <br>
Remember, that all methods must have <strong>self</strong> as their first <strong>parameter</strong>. <br>
These methods are accessed using the same <strong>dot syntax</strong> as attributes. <br>
<strong>Example:</strong></p>
<pre><code>class Dog:
def __init__(self, name, color):
self.name = name
self.color = color
def bark(self):
print("Woof!")
fido = Dog("Fido", "brown")
print(fido.name)
fido.bark()</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>Fido <br>
Woof!</p>
</blockquote>
<p>Classes can also have <strong>class attributes</strong>, created by assigning variables within the body of the class. These can be accessed either from instances of the class, or the class itself. <br>
<strong>Example:</strong></p>
<pre><code>class Dog:
legs = 4
def __init__(self, name, color):
self.name = name
self.color = color
fido = Dog("Fido", "brown")
print(fido.legs)
print(Dog.legs)</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>4 <br>
4</p>
</blockquote>
<p><strong>NOTE:</strong> Class attributes are shared by all instances of the class.</p>
<h3 id="classes-2">
<a id="classes-2" class="anchor" href="#classes-2" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Classes-2</strong>
</h3>
<hr>
<p>Trying to access an attribute of an instance that isn’t defined causes an <strong>AttributeError</strong>. This also applies when you call an <strong>undefined</strong> method.</p>
<p><strong>Example:</strong></p>
<pre><code>class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
rect = Rectangle(7, 8)
print(rect.color)</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>AttributeError: ‘Rectangle’ object has no attribute ‘color’</p>
</blockquote>
<h3 id="inheritance-1">
<a id="inheritance-1" class="anchor" href="#inheritance-1" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Inheritance-1</strong>
</h3>
<hr>
<p><strong>Inheritance</strong> provides a way to share functionality between classes. <br>
Imagine several classes, <strong>Cat</strong>, <strong>Dog</strong>, <strong>Rabbit</strong> and so on. Although they may differ in some ways (only <strong>Dog</strong> might have the method <strong>bark</strong>), they are likely to be similar in others (all having the attributes <strong>color</strong> and <strong>name</strong>). <br>
This similarity can be expressed by making them all inherit from a <strong>superclass</strong> <strong>Animal</strong>, which contains the shared functionality. <br>
To inherit a class from another class, put the superclass name in parentheses after the class name. <br>
<strong>Example:</strong></p>
<pre><code>class Animal:
def __init__(self, name, color):
self.name = name
self.color = color
class Cat(Animal):
def purr(self):
print("Purr...")
class Dog(Animal):
def bark(self):
print("Woof!")
fido = Dog("Fido", "brown")
print(fido.color)
fido.bark()</code></pre>
<p><strong>Result</strong>:</p>
<blockquote>
<p>brown <br>
Woof!</p>
</blockquote>
<h3 id="inheritance-2">
<a id="inheritance-2" class="anchor" href="#inheritance-2" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Inheritance-2</strong>
</h3>
<hr>
<p>A class that inherits from another class is called a <strong>subclass</strong>. <br>
A class that is inherited from is called a <strong>superclass</strong>. <br>
If a class inherits from another with the same attributes or methods, it overrides them.</p>
<pre><code>class Wolf:
def __init__(self, name, color):
self.name = name
self.color = color
def bark(self):
print("Grr...")
class Dog(Wolf):
def bark(self):
print("Woof")
husky = Dog("Max", "grey")
husky.bark()</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>Woof</p>
</blockquote>
<p><strong>Note:</strong> In the example above, Wolf is the superclass, Dog is the subclass.</p>
<h3 id="inheritance-3">
<a id="inheritance-3" class="anchor" href="#inheritance-3" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Inheritance-3</strong>
</h3>
<hr>
<p>Inheritance can also be <strong>indirect</strong>. One class can inherit from another, and that class can inherit from a third class. <br>
<strong>Example:</strong></p>
<pre><code>class A:
def method(self):
print("A method")
class B(A):
def another_method(self):
print("B method")
class C(B):
def third_method(self):
print("C method")
c = C()
c.method()
c.another_method()
c.third_method()</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>A method <br>
B method <br>
C method</p>
</blockquote>
<p><strong>Note:</strong> However, circular inheritance is not possible.</p>
<h3 id="inheritance-4">
<a id="inheritance-4" class="anchor" href="#inheritance-4" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a><strong>Inheritance-4</strong>
</h3>
<hr>
<p>The function <strong>super</strong> is a useful inheritance-related function that refers to the parent class. It can be used to find the method with a certain name in an object’s superclass. <br>
<strong>Example:</strong></p>
<pre><code>class A:
def spam(self):
print(1)
class B(A):
def spam(self):
print(2)
super().spam()
B().spam()</code></pre>
<p><strong>Result:</strong></p>
<blockquote>
<p>2 <br>
1 </p>
</blockquote>
<p><strong>Note:</strong> <strong><em>super().spam()</em></strong> calls the <strong>spam</strong> method of the superclass.</p>
<footer class="site-footer">
<span class="site-footer-owner"><a href="https://github.com/celikmustafa89/sololearn-python-notes">Python Notes</a> is maintained by <a href="https://github.com/celikmustafa89">celikmustafa89</a>.</span>
<span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a> using the <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a> by <a href="https://twitter.com/jasonlong">Jason Long</a>.</span>
</footer>
</section>
</body>
</html>