-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflashcards_programming.html
More file actions
1893 lines (1718 loc) · 108 KB
/
flashcards_programming.html
File metadata and controls
1893 lines (1718 loc) · 108 KB
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Flashcards - Programming</title>
<style>
body {
background-color: #2c3e50;
color: #131314;
font-family: 'Arial', sans-serif;
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
margin: 20px;
overflow: auto;
}
.sidebar {
background-color: #34495e;
width: 250px;
padding: 20px;
box-shadow: 2px 0 5px rgba(0, 0, 0, 0.3);
display: flex;
flex-direction: column;
align-items: flex-start;
}
.sidebar button {
background-color: #e3ed4b;
color: rgb(22, 19, 19);
border: none;
padding: 10px 20px;
margin: 10px 0;
border-radius: 5px;
cursor: pointer;
font-size: 1em;
width: 100%;
/* Make all buttons the same width as the sidebar */
box-sizing: border-box;
/* Ensure padding is included in the total width */
text-align: left;
/* Align the content to the left */
}
.sidebar button:hover {
background-color: #50f43a;
}
.container {
flex: 1;
padding: 20px;
display: none;
/* Hide by default */
}
.container.active {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
.flashcard {
background-color: #dbe9a9;
border-radius: 15px;
padding: 20px;
cursor: pointer;
position: relative;
overflow: hidden;
transition: transform 0.3s, box-shadow 0.3s;
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.3);
}
.flashcard:hover {
transform: translateY(-10px);
box-shadow: 0 12px 24px rgba(19, 240, 30, 0.4);
}
.question {
font-size: 1.2em;
font-weight: bold;
margin-bottom: 10px;
}
.answer {
display: none;
font-size: 1em;
margin-top: 10px;
color: #090a0a;
}
/* Add the clicked class here */
.clicked {
background-color: #94fba4;
/* Example color: red */
color: #181616d0;
/* Example text color: white */
}
.flashcard:active .answer {
display: block;
}
</style>
<script>
function showGroup(groupId) {
var containers = document.querySelectorAll('.container');
containers.forEach(container => container.classList.remove('active'));
document.getElementById(groupId).classList.add('active');
}
function toggleAnswer(flashcard) {
const answer = flashcard.querySelector('.answer');
answer.style.display = (answer.style.display === 'block') ? 'none' : 'block';
// Toggle the 'clicked' class to change the color
flashcard.classList.toggle('clicked');
}
</script>
</head>
<body>
<div class="sidebar">
<button onclick="showGroup('group1')">Programming Concepts (142)</button>
<button onclick="showGroup('group2')">Historical Figures (28)</button>
<button onclick="showGroup('group3')">Hardware (25)</button>
<button onclick="showGroup('group4')">Software (22)</button>
<button onclick="showGroup('group5')">Data Representation (6)</button>
</div>
<!-- Programming Concepts -->
<div id="group1" class="container">
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a module in Python?</div>
<div class="answer">A module is a file containing Python code, including functions and classes, that can be
imported and used in other Python programs.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What are the two common ways to import a module in Python, and how do they differ in
usage?</div>
<div class="answer">1. <code>import math</code>: You import the entire module and use
<code>math.sqrt()</code> to access the functions.<br>2. <code>from math import sqrt</code>: You import a
specific function and can use <code>sqrt()</code> directly without the module prefix.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a loop in programming?</div>
<div class="answer">A loop is a control structure that repeatedly executes a block of code as long as a
specified condition is true. Common loops include <code>for</code> and <code>while</code> loops.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the difference between integers and floats?</div>
<div class="answer">Integers are whole numbers (e.g., 1, -5, 100), while floats are numbers with a decimal
point (e.g., 1.5, -3.14, 0.0).</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a string?</div>
<div class="answer">A string is a sequence of characters enclosed in single, double, or triple quotes.
Example: <code>'hello'</code> or <code>"hello"</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you convert a string to an integer or float?</div>
<div class="answer">You can use the <code>int()</code> function to convert a string to an integer and
<code>float()</code> to convert to a float. Example: <code>int('123')</code> or
<code>float('3.14')</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a list in Python?</div>
<div class="answer">A list in Python is a collection of items that are ordered and mutable. Lists can
contain elements of different data types, including other lists.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a set in Python?</div>
<div class="answer">A set is an unordered collection of unique elements. It is defined using curly braces,
like <code>{1, 2, 3}</code>, or the <code>set()</code> function.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a subprogram in Python?</div>
<div class="answer">A subprogram in Python is a block of code defined as a function, which performs a
specific task and can be called multiple times.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the difference between local and global scope?</div>
<div class="answer">Local scope refers to variables defined within a function, accessible only inside that
function. Global scope refers to variables defined outside any function, accessible throughout the
script.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">Why can a variable with the same name behave differently in different functions (defs)
without causing an error?</div>
<div class="answer">Variables defined inside a function have a local scope, meaning they are accessible only
within that function. This allows variables with the same name in different functions to coexist without
conflict, as they are separate entities within their respective scopes.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does it mean to "call a function" in programming?</div>
<div class="answer">To "call a function" means to execute or invoke the code within the function. This is
done by using the function's name followed by parentheses, optionally passing arguments inside the
parentheses if the function requires them. For example, <code>myFunction()</code> calls a function named
<code>myFunction</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the difference between functions and procedures in general programming terms?
</div>
<div class="answer">In general programming, functions are sub-algorithms that return a value, while
procedures are sub-algorithms that do not return any value.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How are functions and procedures handled in Python?</div>
<div class="answer">In Python, there is only the concept of a function. A function with a
<code>return</code> statement returns a value, while a function without a <code>return</code> statement
implicitly returns <code>None</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does Python return if a function does not have a <code>return</code> statement?
</div>
<div class="answer">If a function does not have a <code>return</code> statement, Python returns a special
value called <code>None</code>, which is of type <code>NoneType</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">Is <code>None</code> a string in Python?</div>
<div class="answer">No, <code>None</code> is not a string. It is a special value of type
<code>NoneType</code> used to represent the absence of a value.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">Why can’t we modify a variable in a function if that variable was not created within
the function?</div>
<div class="answer">Variables not created within a function are considered out of the function's local
scope. To modify such a variable, you would need to explicitly declare it as global or pass it as a
parameter to the function.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What happens if you try to call a function defined within another function, from the
main program?</div>
<div class="answer">You will get a NameError because the inner function is not visible or accessible outside
the scope of the function it is defined in.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does function scope allow us to implement in programming?</div>
<div class="answer">Function scope allows us to implement the concept of function visibility, meaning
functions can be either public (accessible everywhere) or private (accessible only within a specific
scope).</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the scope of a variable in Python?</div>
<div class="answer">The scope of a variable in Python refers to the region of the code where the variable is
accessible. Variables defined inside a function are only accessible within that function.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">Why might a function defined within another function be inaccessible from the main
program or other parts of the code?</div>
<div class="answer">A function defined inside another function has a limited scope, meaning it is only
accessible within the outer function where it is defined. Attempting to call it from outside this scope
will result in a NameError.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the purpose of using <code>time()</code> in the code?</div>
<div class="answer">The <code>time()</code> function is used to measure the execution time of the functions
<code>factorial</code> and <code>summation</code>. By taking timestamps before and after the function
calls, the code calculates how long each function takes to run.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How can you format integers, floats, and strings using format specifiers?</div>
<div class="answer">You can format output using format specifiers: <code>%d</code> for integers,
<code>%.f</code> for floats, and <code>%s</code> for strings. For example, to print a float with 10
decimal places, use <code>%.10f</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a class in object-oriented programming?</div>
<div class="answer">A class is a blueprint for creating objects, characterized by attributes and methods
that define the behavior of the objects.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a subclass in Python?</div>
<div class="answer">A subclass is a class that inherits from another class (the parent class). It can
override or extend the functionality of the parent class.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is polymorphism in Python?</div>
<div class="answer">Polymorphism allows methods in different classes to have the same name but behave
differently based on the object calling them. It provides flexibility in code design.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is encapsulation in Python?</div>
<div class="answer">Encapsulation is the concept of bundling data and methods within a class and restricting
access to some components using private and public attributes and methods.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is inheritance in Python?</div>
<div class="answer">Inheritance is a feature that allows a class (child class) to inherit attributes and
methods from another class (parent class), promoting code reuse.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What are getters and setters in Python?</div>
<div class="answer">Getters and setters are methods used to access and update the values of private
attributes in a class, promoting encapsulation.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is polymorphism in OOP?</div>
<div class="answer">Polymorphism allows a method in a child class to have the same name as a method in the
parent class but behave differently, depending on the class of the object that calls it.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is encapsulation?</div>
<div class="answer">Encapsulation is the principle of restricting access to certain details of an object and
exposing only necessary parts, often using getters and setters.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is inheritance in object-oriented programming?</div>
<div class="answer">Inheritance is a way to create a new class using details from an existing class,
inheriting attributes and methods.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What are methods in a class?</div>
<div class="answer">Methods are functions defined within a class that describe the behavior of an object.
For example, <code>Talk()</code>, <code>Sleep()</code>, or <code>Move()</code> in a <code>Human</code>
class.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a constructor in a class?</div>
<div class="answer">A constructor is a special method called <code>__init__</code> in Python that
initializes the attributes of an object when it is created.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the purpose of the <code>self</code> parameter?</div>
<div class="answer"><code>self</code> refers to the instance of the class and allows access to the object's
attributes and methods.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What are attributes in a class?</div>
<div class="answer">Attributes are variables that hold data specific to an object. They are defined within
the class, usually in the <code>__init__</code> constructor.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the <code>__init__</code> function?</div>
<div class="answer"><code>__init__</code> is a constructor method in Python classes that initializes the
object's attributes when an instance is created.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the <code>__str__</code> function?</div>
<div class="answer"><code>__str__</code> is a method that returns a human-readable string representation of
an object, used when <code>print()</code> or <code>str()</code> is called on an object.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does <code>if __name__ == "__main__":</code> mean?</div>
<div class="answer">This line checks if the script is being run directly (not imported as a module) and
executes the code block if true. It's used to run code only when the file is executed as the main
program.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is an exception in Python?</div>
<div class="answer">An exception is an error that occurs during the execution of a program, disrupting its
normal flow. Exceptions can be handled using <code>try</code> and <code>except</code> blocks.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How does the code handle incorrect input or division errors?</div>
<div class="answer">The code uses a <code>try-except</code> block to catch errors. If an exception occurs
(like invalid input or division by zero), it prints "ERROR: Incorrect values."
<img src="C:\Users\sonia\OneDrive\Escritorio\try_except.jpg" alt="Try-Except Block"
style="width: 100%; height: auto; margin-top: 10px;">
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What are the purposes of <code>else</code> and <code>finally</code> in exception
handling?</div>
<div class="answer">The <code>else</code> statement runs if no exceptions are thrown, similar to how it
works with <code>if</code> conditions. The <code>finally</code> statement executes code that must run
regardless of whether an exception occurred, such as closing files or cleaning up resources.
<img src="C:\Users\sonia\OneDrive\Escritorio\finally.jpg" alt="Else-Finally Block"
style="width: 100%; height: auto; margin-top: 10px;">
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the purpose of the <code>finally</code> block in the exception handling flow,
and when is it executed according to the diagram?</div>
<div class="answer">The <code>finally</code> block is used to execute code that must run regardless of
whether an exception was raised or not. It is executed after the <code>try</code> block and any
<code>except</code> or <code>else</code> blocks, ensuring cleanup actions are always performed, such as
closing a file or releasing resources.
<img src="C:\Users\sonia\OneDrive\Escritorio\flowchart.jpg" alt="Else-Finally Block"
style="width: 100%; height: auto; margin-top: 10px;">
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the purpose of the <code>assert</code> statement in Python?</div>
<div class="answer">
The <code>assert</code> statement is used for debugging purposes. It tests a condition, and if the
condition is false, it raises an <code>AssertionError</code> with an optional error message. Example:
<code>assert x > 0, "x must be greater than 0"</code>.
<br><br>
<img src="C:\Users\sonia\OneDrive\Escritorio\assert.jpg" alt="Assert statement"
style="max-width: 100%; height: auto;">
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the difference between Traversal and Search in ordered data structures like
strings, tuples, and lists?</div>
<div class="answer">In ordered data structures, <strong>Traversal</strong> involves reading all elements in
the sequence without exception, ensuring every element is processed. In contrast,
<strong>Search</strong> allows stopping as soon as the desired element is found, without the need to
read all elements.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How can you check for blank spaces in a string using a Search algorithm?</div>
<div class="answer">You can use a loop to search for blank spaces in a string. The loop continues until it
finds a blank space or reaches the end of the string.
<img src="C:\Users\sonia\OneDrive\Escritorio\search.jpg" alt="Search block"
style="width: 100%; height: auto; margin-top: 10px;">
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How can you use traversal to count the number of vowels in a string?</div>
<div class="answer">You can use a <code>for</code> loop with <code>range(len(s))</code> to traverse each
character of the string using its index. Check each character to see if it is a vowel and increment a
counter accordingly.
<img src="C:\Users\sonia\OneDrive\Escritorio\1traversal.jpg" alt="Traversal block"
style="width: 100%; height: auto; margin-top: 10px;">
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a more Pythonian way to traverse a string and count vowels?</div>
<div class="answer">A more Pythonian way is to use a <code>for</code> loop that iterates directly over the
characters of the string, without using an index. This simplifies the code and makes it more readable
while still counting each vowel efficiently.
<img src="C:\Users\sonia\OneDrive\Escritorio\2traversal.jpg" alt="Traversal pythonian block"
style="width: 100%; height: auto; margin-top: 10px;">
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you create a list in Python?</div>
<div class="answer">You can create a list by placing comma-separated values between square brackets.
Example: <code>my_list = [1, 2, 3, 'hello']</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you iterate over elements in a list?</div>
<div class="answer">You can use a <code>for</code> loop to iterate over each element in a list. Example:
<code>for item in my_list: print(item)</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you access elements in a list?</div>
<div class="answer">You can access elements in a list using their index, which starts at 0. Example:
<code>my_list[0]</code> returns the first element.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you update an element in a list?</div>
<div class="answer">You can update an element in a list by assigning a new value to a specific index.
Example: <code>my_list[1] = 'new_value'</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you add an element to a list?</div>
<div class="answer">You can add an element to the end of a list using the <code>append()</code> method.
Example: <code>my_list.append('new_element')</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you insert an element at a specific position in a list?</div>
<div class="answer">You can use the <code>insert()</code> method to add an element at a specific index.
Example: <code>my_list.insert(2, 'new_element')</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you remove an element from a list by value?</div>
<div class="answer">You can use the <code>remove()</code> method to delete an element by value. Example:
<code>my_list.remove('value_to_remove')</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you remove an element from a list by index?</div>
<div class="answer">You can use the <code>pop()</code> method to remove an element by index and return it.
Example: <code>my_list.pop(1)</code> removes the element at index 1.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you clear all elements from a list?</div>
<div class="answer">You can use the <code>clear()</code> method to remove all elements from a list. Example:
<code>my_list.clear()</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you get the length of a list?</div>
<div class="answer">You can use the <code>len()</code> function to get the number of elements in a list.
Example: <code>len(my_list)</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you check if an element is in a list?</div>
<div class="answer">You can use the <code>in</code> keyword to check for an element's existence. Example:
<code>'item' in my_list</code> returns <code>True</code> if 'item' is in the list.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you concatenate two lists?</div>
<div class="answer">You can concatenate two lists using the <code>+</code> operator. Example:
<code>new_list = list1 + list2</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you repeat elements in a list?</div>
<div class="answer">You can repeat elements in a list using the <code>*</code> operator. Example:
<code>repeated_list = my_list * 3</code> creates a list with elements repeated three times.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you slice a list?</div>
<div class="answer">You can slice a list to get a subset of elements using the syntax
<code>my_list[start:end]</code>. Example: <code>my_list[1:4]</code> returns elements at index 1, 2, and
3.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you remove an element from a set?</div>
<div class="answer">You can use the <code>remove()</code> or <code>discard()</code> method to remove an
element. <code>remove()</code> raises an error if the element is not found, while <code>discard()</code>
does not.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the ASCII code and why is it important?</div>
<div class="answer">
The ASCII code is a Latin-based character encoding standard created in 1963 by the American Committee on Standards. It maps 256 integers (representable in a byte) to characters. For example:
<ul>
<li>"a" has a code of 97 and "A" has a code of 65.</li>
<li>The digit "0" has a code of 48, and digits continue up to "9" with 57.</li>
</ul>
ASCII organizes lowercase and uppercase letters sequentially:
<ul>
<li>Lowercase: "a" = 97, "b" = 98, ... "z" = 122.</li>
<li>Uppercase: "A" = 65, "B" = 66, ... "Z" = 90.</li>
</ul>
<p>On the keyboard, you can use ALT + Num to insert a character from the ASCII table (e.g., ALT + 169 = ©).</p>
<img src="C:\Users\sonia\OneDrive\Escritorio\asci.jpg" alt="ASCII Code" style="width:300px; height:auto;">
<p>In Python:</p>
<ul>
<li><code>print(chr(65))</code> → 'A'</li>
<li><code>print(ord("A"))</code> → 65</li>
</ul>
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What are the main differences between <code>while</code> and <code>for</code> loops in Python?</div>
<div class="answer">
<ul>
<li><strong>Condition Type:</strong> A <code>while</code> loop is based on a condition, while a <code>for</code> loop iterates over a sequence or range.</li>
<li><strong>Number of Repetitions:</strong> A <code>while</code> loop depends on a condition and may repeat 0 or more times, while a <code>for</code> loop typically iterates a known number of times.</li>
<li><strong>Counter:</strong> In a <code>while</code> loop, the counter must be explicitly initialized and updated. In a <code>for</code> loop, the counter is managed by the loop itself.</li>
<li><strong>Equivalence:</strong> A <code>for</code> loop can always be rewritten as a <code>while</code> loop, but not all <code>while</code> loops can be rewritten as <code>for</code> loops.</li>
</ul>
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How can you compute the sum of numbers from 1 to a given limit in Python using both a <code>for</code> loop and a <code>while</code> loop?</div>
<div class="answer">
<p>1. Using a <code>for</code> loop:</p>
<pre>
sum_all = 0
limit = int(input("Enter a number to
compute the sum: "))
while (limit < 0):
print("Error: The number must be
greater than zero")
limit = int(input("Enter a number
to compute the sum: "))
for number in range(limit + 1):
sum_all += number
print(sum_all)
</pre>
<p>2. Using a <code>while</code> loop:</p>
<pre>
number = 1
sum_all = 0
limit = int(input("Enter a number to
compute the sum: "))
while (limit < 0):
print("Error: The number must be
greater than zero")
limit = int(input("Enter a number
to compute the sum: "))
while (number <= limit):
sum_all += number
number += 1
print("The summation of", limit, "is:", sum_all)
</pre>
<p>Both approaches will calculate the sum of all numbers from 1 to the specified limit, but they use different loop structures.</p>
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What are the flowcharts for <code>while</code> and <code>for</code> loops?</div>
<div class="answer"><p>Flowchart for While loop: </p>
<img src="C:\Users\sonia\OneDrive\Escritorio\flowchart_while.png" alt="While flowchart" style="width:200px; height:auto;">
<p>Flowchart for For loop: </p>
<img src="C:\Users\sonia\OneDrive\Escritorio\flowchart_for.jpg" alt="For flowchart" style="width:200px; height:auto;">
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What are some basic data structures in Python?</div>
<div class="answer">
<ul>
<li><strong>String (str):</strong> A sequence of individual characters. You can use operators to index or extract substrings.</li>
<li><strong>Tuple:</strong> An immutable collection of ordered elements, a generalization of a string.</
</div>
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What happens in computer memory when an object is created?</div>
<div class="answer">An object is created, initialized, and stored in computer memory. It has an identifier (memory address), and a variable name serves as a pointer to this address.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is immutability in programming?</div>
<div class="answer">Immutability means that an object’s value cannot change after it is created. For example, if an integer with a value of 10 exists in memory, assigning another variable to this value doesn’t create a new object; it reuses the same memory address. A string is immutable.
<img src="C:\Users\sonia\OneDrive\Escritorio\string.jpg" alt="Immutability" style="width:200px; height:auto;">
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">Why might we need mutable objects?</div>
<div class="answer">Mutability allows us to change an object's value without creating a new object, conserving memory and making the code easier to manage. Mutable objects, like lists, allow modifications without losing their memory address.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a mutable object?</div>
<div class="answer">A mutable object can have its value changed after creation. Examples include lists and dictionaries. Unlike immutable objects, mutating a mutable object keeps its original memory address.
<img src="C:\Users\sonia\OneDrive\Escritorio\mutable.jpg" alt="Mutability" style="width:300px; height:auto;">
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the difference between mutable and immutable objects in terms of memory?</div>
<div class="answer">Immutable objects retain their initial memory address without change, but any modification creates a new object. Mutable objects allow modifications within the same memory address, keeping the link to the original object.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you access a specific character in a string?</div>
<div class="answer">You use the operator <code>[]</code> with the character's index. The index starts from 0, so <code>string[i]</code> accesses the character at position i.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does the <code>len()</code> function do when used with a string?</div>
<div class="answer">The <code>len()</code> function returns the length of the string, which is the total number of characters it contains.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does the slicing operator <code>[start:stop:step]</code> do in strings?</div>
<div class="answer">The slicing operator returns a substring starting from the character at the 'start' index (included) to the 'stop' index (not included), taking characters at intervals of 'step'. The default step is 1.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a tuple in Python?</div>
<div class="answer">A tuple is an ordered sequence of elements, similar to a string but capable of containing elements of different types. Tuples are written as comma-separated values inside parentheses, and they are immutable.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you access elements in a tuple?</div>
<div class="answer">You use the operator <code>[]</code> with the index of the element. Indexing in tuples works the same way as in strings, starting from 0.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">Can you change the values of a tuple after it's created?</div>
<div class="answer">No, tuples are immutable. Once created, you cannot change, add, or remove elements from a tuple.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How can you concatenate two or more tuples?</div>
<div class="answer">You can concatenate tuples using the <code>+</code> operator. For example, <code>tuple1 + tuple2</code> creates a new tuple with elements from both.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the utility of tuples in programming?</div>
<div class="answer">Tuples are useful for grouping related values, swapping the values of two variables, and returning multiple values from a function.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">Can tuples be nested?</div>
<div class="answer">Yes, tuples can be nested. You can have tuples within tuples to create more complex data structures.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you define a list in Python?</div>
<div class="answer">A list is defined using square brackets, and its elements are separated by commas. Example: <code>my_list = [1, 2, 3, 4]</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">Can lists in Python contain elements of different data types?</div>
<div class="answer">Yes, lists in Python are heterogeneous, meaning they can contain elements of different data types.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you access an element in a list using an index?</div>
<div class="answer">You use the operator <code>[]</code> with the index of the element. Indexing starts from 0, so <code>my_list[0]</code> returns the first element.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you access the last item in a list using negative indexing?</div>
<div class="answer">You use the index <code>-1</code> to access the last item. For example, <code>my_list[-1]</code> gives the last element.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you change the value of an item in a list?</div>
<div class="answer">Lists are mutable, so you can change the value of an item using the index. Example: <code>my_list[2] = 10</code> changes the third item to 10.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does the <code>len()</code> function return when used with a list?</div>
<div class="answer">The <code>len()</code> function returns the number of elements in the list.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How can you add an element to the end of a list?</div>
<div class="answer">You can use the <code>append()</code> method to add an element to the end of the list. Example: <code>my_list.append(5)</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How can you concatenate two lists?</div>
<div class="answer">You can use the <code>+</code> operator to concatenate two lists, creating a new list. Example: <code>list1 + list2</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you remove an item from a list by value?</div>
<div class="answer">You use the <code>remove()</code> method. It removes the first occurrence of the specified value. Example: <code>my_list.remove(3)</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">Why should you not modify a list while iterating over it?</div>
<div class="answer">Modifying a list while iterating can cause unexpected behavior because Python uses an internal counter to track elements. Removing elements can skip items or cause errors.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How can you create a copy of a list to avoid modifying the original list?</div>
<div class="answer">You can use slicing <code>[:]</code> or the <code>copy()</code> method to create a copy of the list. Example: <code>list_copy = my_list[:]</code> or <code>list_copy = my_list.copy()</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the significance of using <code>[:]`</code> when copying lists?</div>
<div class="answer">Using <code>[:]</code> creates a shallow copy of the list, ensuring changes to the new list do not affect the original list.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How can you check if a number is prime?</div>
<div class="answer">
Here's a simple algorithm to check if a number <code>N</code> is prime:
<pre>
N = int(input("Enter a number: "))
prime = True
if N < 2:
prime = False
else:
i = 2
while i * i <= N:
if N % i == 0:
prime = False
break
i += 1
if prime:
print("Prime")
else:
print("NOT Prime")
</pre>
This code checks divisibility of <code>N</code> starting from 2 up to the square root of <code>N</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How can you extract hashtags from a tweet?</div>
<div class="answer">
Use this code to extract hashtags from a string:
<pre>
s = input('Enter a string: ')
i = 0
while i < len(s):
pos = i
if s[i] == '#':
while i < len(s) and s[i] != " ":
i += 1
print(s[pos:i])
i += 1
</pre>
This program iterates over the input string, detecting and printing each hashtag.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How does the Sieve of Eratosthenes algorithm work to find prime numbers?</div>
<div class="answer">
Here's a Python implementation of the Sieve of Eratosthenes:
<pre>
max = int(input("Enter the upper limit: "))
P = []
NP = []
for nb in range(2, max + 1):
if nb not in NP:
P.append(nb)
for multiple in range(nb * nb, max + 1, nb):
NP.append(multiple)
print(P)
</pre>
This algorithm efficiently finds all prime numbers up to <code>maxi</code> by marking non-prime numbers.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a ValueError in Python?</div>
<div class="answer">A ValueError occurs when a function receives an argument of the right data type but with
an inappropriate value. Example: trying to convert a non-numeric string to an integer, like
<code>int('abc')</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is an IndexError in Python?</div>
<div class="answer">An IndexError occurs when you try to access an element from a list, tuple, or string
using an index that is out of range. Example: trying to access <code>my_list[5]</code> when
<code>my_list</code> only has 3 elements.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a TypeError in Python?</div>
<div class="answer">A TypeError occurs when an operation or function is applied to an object of an
inappropriate type. Example: trying to concatenate a string and an integer, like
<code>'hello' + 5</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a KeyError in Python?</div>
<div class="answer">A KeyError occurs when you try to access a dictionary key that does not exist. Example:
accessing <code>my_dict['missing_key']</code> when 'missing_key' is not in <code>my_dict</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is an AttributeError in Python?</div>
<div class="answer">An AttributeError occurs when you try to access or call an attribute or method that an
object does not have. Example: trying to use <code>my_string.append('a')</code> when strings do not have
an <code>append()</code> method.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a NameError in Python?</div>
<div class="answer">A NameError occurs when you try to use a variable or function name that has not been
defined. Example: using <code>print(x)</code> when <code>x</code> has not been declared.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a ZeroDivisionError in Python?</div>
<div class="answer">A ZeroDivisionError occurs when you attempt to divide a number by zero. Example:
<code>10 / 0</code> will raise a ZeroDivisionError.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a FileNotFoundError in Python?</div>
<div class="answer">A FileNotFoundError occurs when you try to open a file that does not exist. Example:
<code>open('non_existent_file.txt')</code> will raise this error if the file is not found.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is an ImportError in Python?</div>
<div class="answer">An ImportError occurs when an import statement fails to find the module or package you
are trying to import. Example: <code>import non_existent_module</code> will raise an ImportError if the
module is not available.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is a SyntaxError in Python?</div>
<div class="answer">A SyntaxError occurs when the Python parser encounters a syntax error, meaning the code
structure is incorrect. Example: <code>print 'Hello'</code> without parentheses will raise a SyntaxError
in Python 3.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does an <code>IOError</code> indicate in Python?</div>
<div class="answer">An <code>IOError</code> occurs when the input/output systems detect an error, such as trying to open a file that does not exist or issues with reading/writing to a file.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the purpose of the <code>assert</code> statement in Python?</div>
<div class="answer">The <code>assert</code> statement is used for debugging. It tests a condition and raises
an <code>AssertionError</code> if the condition is false. Example:
<code>assert x > 0, "x must be positive"</code>.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">Why is the <code>assert</code> statement useful in defensive programming?</div>
<div class="answer">
The <code>assert</code> statement is used in defensive programming to ensure that a program stops execution if a condition is not met. It is commonly used to check:
<ul>
<li>Input parameters to functions to validate data.</li>
<li>Outputs of a function to prevent the spread of incorrect values.</li>
</ul>
This helps make it easier to locate the source of an error.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you open a file in Python?</div>
<div class="answer">You can open a file using the <code>open()</code> function. It requires the file name
and mode (optional). Example: <code>file = open('example.txt', 'r')</code> opens a file in read mode.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does the <code>'r'</code> mode do in file handling?</div>
<div class="answer">The <code>'r'</code> mode opens a file for reading. If the file does not exist, it
raises a <code>FileNotFoundError</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does the <code>'w'</code> mode do in file handling?</div>
<div class="answer">The <code>'w'</code> mode opens a file for writing. If the file exists, it truncates
(overwrites) the file. If the file does not exist, it creates a new file.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does the <code>'a'</code> mode do in file handling?</div>
<div class="answer">The <code>'a'</code> mode opens a file for appending. New data is written at the end of
the file. If the file does not exist, it creates a new file.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What is the benefit of using a <code>with</code> statement for file handling?</div>
<div class="answer">Using a <code>with</code> statement ensures that the file is properly closed after its
block of code is executed, even if an error occurs. Example:
<code>with open('file.txt', 'r') as file:</code>
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you write data to a file in Python?</div>
<div class="answer">You can write data to a file using the <code>write()</code> method. Example:
<code>file.write("Hello, World!")</code> writes "Hello, World!" to the file.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">How do you read data from a file in Python?</div>
<div class="answer">You can read data from a file using the <code>read()</code>, <code>readline()</code>, or
<code>readlines()</code> method. Example: <code>content = file.read()</code> reads the entire file
content.
</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What does the <code>'b'</code> mode do when opening a file?</div>
<div class="answer">The <code>'b'</code> mode opens a file in binary mode, which is used for non-text files
like images or executable files. Example: <code>file = open('image.jpg', 'rb')</code>.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">
<div class="question">What happens if you try to open a file that does not exist using the <code>'w'</code>
mode?</div>
<div class="answer">If you open a file with the <code>'w'</code> mode and the file does not exist, Python
will create a new file.</div>
</div>
<div class="flashcard" onclick="toggleAnswer(this)">