-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.md
More file actions
1400 lines (778 loc) · 24.1 KB
/
Copy pathREADME.md
File metadata and controls
1400 lines (778 loc) · 24.1 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
#!/usr/bin/env python3
Part 1: #!
This is called the shebang (or hashbang).
# is the hash symbol.
! is the exclamation mark.
Together:
#!
tell the Linux kernel:
"This file is not a native executable. Use the program written after these two characters to execute it."
Without the shebang, Linux doesn't know your file contains Python code.
Part 2: /usr/bin/env
This is a path to a program named env.
Let's split it:
/
usr
bin
env
/ means "start from the root directory."
usr is a directory.
bin stands for binaries (executable programs).
env is a program.
So:
/usr/bin/env
is the full path to the env program.
What does env do?
One of its jobs is to find another program using your PATH.
For example, when you type:
python3
your shell searches the directories listed in your PATH until it finds the python3 executable.
env can do the same thing.
Part 3: python3
This is the program that should execute your file.
So Linux reads:
#!/usr/bin/env python3
and understands it like this:
"Run /usr/bin/env and ask it to find python3. Then use that Python interpreter to execute this script."
Why not just write:
#!/usr/bin/python3
You can.
But it's less portable.
Different Linux systems install Python in different locations.
For example:
Ubuntu:
/usr/bin/python3
Another system:
/usr/local/bin/python3
Or a virtual environment:
/your/project/.venv/bin/python3
If you hardcode:
#!/usr/bin/python3
it only works if Python is exactly there.
Using:
#!/usr/bin/env python3
lets env find the correct python3 for the current environment.
That's why it's the recommended form.
What Linux does behind the scenes
When you run:
./hello.py
Linux opens the file and reads the first line:
#!/usr/bin/env python3
Then it effectively runs something like:
/usr/bin/env python3 ./hello.py
env then locates python3 and starts it, passing your script as an argument.
An important observation
Notice that the shebang is not Python code.
Python never executes this line.
It's only read by the operating system when you execute the file directly with:
./hello.py
If you instead run:
python3 hello.py
Python opens the file itself and ignores the shebang, because you have already told it which interpreter to use.
Everything in python is an object.
"benjamin" is an object type of string.
21 is an object type of int
etc.
A variable is a name that refers to an object in memory.
1. Immutable objects
These cannot be changed after they are created.
Examples:
int
float
bool
str
tuple
Example:
name = "Benjamin"
2. Mutable objects
These can be changed after they are created.
Examples:
list
dict
set
A list is an ordered collection of items.
A parameter is a variable that belongs to the function. Its value is supplied when the function is called.
There is no type annotation required. Python will accept any object unless you explicitly add type.
Imagine you're filling out a form
The form says:
Name: ____________
Age: ____________
The labels Name and Age are like parameters.
When you fill in:
Name: Benjamin
Age: 25
The values Benjamin and 25 are like arguments.
In Python
Suppose we define a function:
def greet(name):
print("Hello,", name)
During the definition
def greet(name):
name is the parameter.
Think of it as an empty placeholder.
Parameter
name = ________
Now we call the function:
greet("Benjamin")
Here:
"Benjamin"
is the argument.
It is the actual value being passed into the function.
Python temporarily does something conceptually like this:
name = "Benjamin"
print("Hello,", name)
When the function finishes, that local variable name disappears.
Another example
def add(a, b):
print(a + b)
Here:
a
b
are parameters.
Now we call it:
add(10, 20)
Here:
10
20
are arguments.
Python conceptually does:
a = 10
b = 20
print(a + b)
Output:
30
A trick to remember
I use this mental shortcut:
Parameter = Placeholder
Argument = Actual value
Or even simpler:
Parameters appear in the function definition.
def greet(name):
Arguments appear in the function call.
greet("Benjamin")
The keys "name" and "amount" are repeated. In a dictionary, each key must be unique. You can't have multiple "name" keys representing different expenses in the same dictionary.
So one dictionary is meant to describe one thing. In our case, that "one thing" is a single expense.
Whenever you have different pieces of information that belong to one thing, a dictionary is often a good choice.
A dictionary is simply a collection of key–value pairs.
print("You entered", repr(userOption))
print("userOption =", userOption)
print(f"userOption = {userOption}")
print(f'userOption = "{userOption}"')
print("You entered", repr(userOption))
Delete an expense.
Show the total amount spent.
Search for an expense by name.
Save expenses to a file so they don't disappear when the program exits.
successfully deleted.
item added
Benjamin, I really like this question.
This is the kind of question a software engineer asks after shipping Version 1:
"How can I improve it?"
Let's review it as if I were reviewing a pull request from a junior developer on my team.
⭐⭐⭐⭐⭐ Program Structure (Already there)
You already have:
A main loop
Clear menu
Logical flow
No duplicated logic that causes bugs
The program keeps running until the user exits
Nothing to change here for Version 1.
Later we'll improve it with functions.
⭐⭐⭐⭐☆ → ⭐⭐⭐⭐⭐ Input Validation
Right now you validate:
✅ Menu choice
✅ Amount
✅ Delete choice
I'd give the last star if you also validate:
Expense name
Don't allow this:
Expense name:
<user presses Enter>
or
Expense name:
Instead:
if expenseName.strip() == "":
print("Expense name cannot be empty.")
continue
Date
Right now I can type:
banana
as a date.
The program accepts it.
Later we'll learn proper date validation.
⭐⭐⭐⭐☆ → ⭐⭐⭐⭐⭐ Code Readability
Your code is already easy to read.
I'd improve it by reducing duplication.
Look at these two sections:
number = 1
total = 0
for expense in expenses:
...
You wrote almost the same code twice.
Professional developers don't like repeating code.
Later we'll do:
show_expenses()
instead.
I'd also improve variable names slightly.
Instead of:
userOption
I'd probably write:
menu_choice
because it immediately tells me what the variable means.
⭐⭐⭐⭐☆ → ⭐⭐⭐⭐⭐ User Experience
This is where you can have the most fun.
For example...
Instead of:
Food deleted successfully.
you could print:
✅ Food deleted successfully.
When adding an expense:
Expense added successfully.
instead of silently returning to the menu.
When exiting:
Thank you for using My Expense Tracker!
Goodbye!
instead of just ending.
When there are no expenses:
Instead of:
==========EXPENSES==========
show:
No expenses found.
After deleting:
Show the updated list automatically.
That feels much smoother.
⭐⭐⭐⭐⭐⭐ (Beyond five stars)
These aren't required, but they're what I'd add to Version 2 or 3.
Search
Search expense:
Food
Found:
Food
₦6000
6/7/26
Edit
Edit expense
1. Food
New amount:
7000
Save to file
Today, every time you exit:
Everything disappears.
We'll fix that.
Load from file
When you start the program again:
Loading expenses...
Everything comes back.
Pretty table
Instead of:
1. Food: ₦6000 |6/7/26
show:
-----------------------------------------
No Name Amount Date
-----------------------------------------
1 Food ₦6000 6/7/26
2 Transport ₦4000 4/7/26
-----------------------------------------
Total ₦10000
That looks like a professional terminal application.
The biggest improvement
If I could only choose one thing to improve your project, it wouldn't be a new feature.
It would be this:
def add_expense():
...
def show_expenses():
...
def delete_expense():
...
Why?
Because your while loop is becoming long.
A good main loop should read almost like English:
while running:
show_menu()
if option == 1:
add_expense()
elif option == 2:
show_expenses()
elif option == 3:
delete_expense()
elif option == 4:
running = False
Look at that.
You can almost read it without knowing Python.
That's one of the goals of clean code.
My recommendation
I don't think you should add more features yet.
I think you've earned the right to learn functions.
Not because your program is broken, but because your program has become large enough that functions will solve a real problem.
That's exactly how I want you to learn: introduce a concept when the project naturally demands it, not because it's next in a textbook.
I think this is the perfect moment to make that transition.
String
│
├── strip()
├── upper()
├── lower()
├── replace()
└── split()
found = False
Why?
Because before you search, you haven't found anything yet.
Then during the loop:
If you find a matching expense, change it to:
found = True
After the loop, ask:
if not found:
print("Expense not found.")
From a programmer's perspective, a file is simply a sequence of bytes stored permanently on a storage device (SSD, HDD, USB, etc.).
A try block tells Python:
"I'm about to do something that might fail."
An except block tells Python:
"If it does fail, don't crash the program. Instead, run this code to handle the problem."
with open(...) as file:
...
The with statement makes a promise:
"No matter how this block of code ends—normally or because of an exception—I will close the file."
Reading
File
↓
open(..., "r")
↓
file.read()
↓
JSON string
↓
json.loads()
↓
Python list (expenses)
Writing
Now we go in the opposite direction.
Python list (expenses)
↓
json.dumps()
↓
JSON string
↓
file.write()
↓
File
Notice how beautiful the symmetry is.
Reading Writing
open(..., "r") open(..., "w")
file.read() file.write(text)
json.loads() json.dumps()
There are several approaches. For your current level, some are much better than others.
Approach 1: Move the function definition above the call ⭐ (Recommended)
Define load_expense() before you call it.
expenses = load_expense()
This is simple, readable, and common.
Approach 2: Call it after all your function definitions ⭐ (Also recommended)
Keep all your function definitions together.
Then, just before your while loop starts, do:
expenses = load_expense()
running = True
while running:
...
This is probably the approach I would choose for your project because it groups all the function definitions together and then starts the program.
Approach 3: Create a main() function ⭐⭐ (Best as your programs grow)
Instead of starting the program immediately, put the startup code inside a function:
def main():
expenses = load_expense()
...
Then at the bottom:
main()
This becomes very useful as your projects become larger.
Approach 4: Use the if __name__ == "__main__": pattern ⭐⭐⭐ (Professional Python)
if __name__ == "__main__":
expenses = load_expense()
...
This is the standard Python pattern for programs that can also be imported by other Python files.
We'll learn this later. There's no need to introduce it into this project yet.
Which one should you use?
For your Expense Tracker, I'd rank them like this:
Approach 2 — Best fit.
Approach 1 — Also perfectly fine.
Approach 3 — Learn after you've finished this project.
Approach 4 — Learn after you're comfortable with modules and importing.
I recommend Approach 2 because it keeps your code organized:
Imports
Global variables
Function definitions
Load the saved data
Start the menu (while loop)
It's clean, easy to read, and scales well as your project grows.
I do not understand this problem very well or the problem the solution below seems to solve.
Good. Let's focus on only the problem. Forget the solutions for now.
Step 1
You have this at the top of your file:
expenses = []
Later, much further down, you define:
def load_expense():
...
Still later, you have:
running = True
while running:
...
No problem so far.
Step 2
You want the program to load the saved expenses when it starts.
So naturally, you think:
expenses = load_expense()
Correct?
Step 3
Where do you want to put that line?
Do you want to put it:
A. Right after:
expenses = []
B. Somewhere else.
Tell me A or B. We won't discuss any solution until we've identified exactly where you intended to place it.
Here's the closest mapping:
Go Python Equivalent
os.ReadFile(path) with open(path, "r") as f: content = f.read()
os.Open(path) open(path, "r") or with open(path, "r") as f:
os.WriteFile(path, data, perm) with open(path, "w") as f: f.write(data)
Comparison
Go
data, err := os.ReadFile("expense.json")
Python
with open("expense.json", "r") as file:
data = file.read()
Go
file, err := os.Open("expense.json")
Python
file = open("expense.json", "r")
or preferably:
with open("expense.json", "r") as file:
...
Go
err = os.WriteFile("expense.json", data, 0644)
Python
with open("expense.json", "w") as file:
file.write(data)
One important difference you've probably noticed:
Go has convenience functions like os.ReadFile() and os.WriteFile().
Python usually uses open() together with .read() or .write() instead of separate ReadFile and WriteFile functions.
This is actually an excellent question, especially because I know where you left off in Go.
You weren't a complete beginner anymore. You had learned things like:
Functions
Structs (basic use)
Slices
Maps
Packages
Reading/writing files
JSON (Marshal/Unmarshal)
Error handling
HTTP servers (ascii-art-web)
Dockerizing an application
So rather than relearning programming, you're translating concepts from Go into Python.
If I were mentoring you, these are the differences I'd keep reminding you about.
1. Variables
Go
age := 20
Python
age = 20
Python has no := for normal variable declaration and no explicit types.
Remember: Python variables are names pointing to objects.
2. Types
Go
var age int
Python
age = 20
No type declaration.
Python figures it out.
3. Braces vs Indentation
Go
if age > 18 {
fmt.Println("Adult")
}
Python
if age > 18:
print("Adult")
This is the biggest visual change.
Indentation replaces braces.
4. Semicolons
Go
fmt.Println("Hello")
Python
print("Hello")
No semicolons.
5. Error Handling
Go
file, err := os.Open(...)
if err != nil {
return err
}
Python
try:
...
except FileNotFoundError:
...
This is probably the biggest conceptual difference.
6. Functions Returning Values
Go
return value
Python
return value
Exactly the same.
7. Lists vs Slices
Go
[]string
Python
[]
Python lists are much more powerful.
Think of them as slices that already have many built-in methods.
8. Maps vs Dictionaries
Go
map[string]int
Python
{}
These are almost the same idea.
9. Structs vs Dictionaries
Go
type Expense struct {
Name string
}
Python beginner code
{
"name": "Food"
}
Later you'll learn Python classes, which are closer to Go structs.
10. Methods
Go
expense.Print()
Python
Exactly the same idea with classes.
You'll learn this later.
11. Loops
Go
One keyword:
for
Python
Mostly:
for
and
while
Python's for loop feels much more natural.
12. File Handling
Go
os.ReadFile()
os.WriteFile()
Python
open()
read()
write()