Today - something different. You'll learn Python (3) in 5 minutes, no need to thank... Ok, not exactly. But... Sort of. You know, when you have a little bit of experience and not much time, there is better way of learning some things (like programming languages), instead of tutorials and books. It's to take a look at the thing in action: in case of a programming language, it would be a short program, using some basic features of the language. The additionary benefit is that you can make it solve some problem you have.
Please mind this note should be quite helpful, if you already know some programming languages, but not Python in particular. If you are a beginner in programming, maybe you can use it as a simple "practical" reference, but you may have problems understanding what going on - I won't elaborate on all things (of course, if you have questions, please do comment).
Ok, so what was that problem in my case?
Well, I had been testing some feature of my Android game - to be precise, the notifications' logic. Some time after event A the app was supposed to show a notification, but only a limited number of times and in specific conditions (details are not important here). So at first I've opened Notepad and started to write things like:
...based on my app's behaviour and logcat.
After working with that for a day or so, I've realized that:
1) I have notes in 3 different editors open at the same time
2) I've been taking a note with a timestamp about 2 times a minute for a day (I check time, I write it down, I write the note, lot of work).
3) I can make things a bit easier...
And by 3) I mean:
I can write a simple script, which simply takes the input by user (followed with [ENTER]) and writes every line (with timestamp) to a file. And then if the user types r [enter] (r), it shows the notes so far. This way: everything is in one editor (which is my script), stored safely in one file (which fixes issue 1) ); and I don't have to manually check and write down the time (issue 2, and - inherently - issue 3).
And here is the script, which by the way is one of the programs I've mentioned in the beginning: it uses functions, flow control (loops, ifs), basic data structures, file I/O... Basically a first few lessons in every Python tutorial ;). Nothing advanced, nothing object-oriented for now. We don't need that for now... Maybe in the future.
# a function showing a prompt
I won't elaborate on the code - I think the Python syntax is very clean and self-explanatory, and I've commented it quite extensively. I think the most cryptic fragment is the if __name__ == "__main__": statement: it's simply the way you should call the main method in Python. You could just write main() in the last line; but when such script is imported as a module, it would be executed automatically (which most probably is not the desired effect). In our case it doesn't really matter (I would't advice importing this as a module ;) ), but let's follow the tradition.
The code is on github here - the linked version may be a bit different, because using it day-to-day, I'll probably tweak something in the future. Maybe I'll update this post too.
Next time I'll elaborate more about functions (and lambda functions). The next lessons will also be more descriptive about basic things.
Meta-notes for today: if you want such nice, monospace, syntax-colored code in Blogger, you can simply copy the contents of a file opened in VS Code (which makes a very nice Python IDE, BTW, with some plugins applied). It works out-of-the-box only for the files with syntax highlighting, so if you just create a new file and type some text, it won't. But in that case, you can use Ctrl+Shift+P -> type Copy -> choose Copy with syntax highlighting.
No comments:
Post a Comment