Tutorial, Project Ideas, and Tips
Last updated
Last updated
If you can create a quadratics root solver, you'll be able to understand this article. These are some concepts I didn't learn in one day but rather a couple years as I am self-taught.I tried to make this into a video, but the video ended up being 1:45 hours long; I myself would need a lot of motivation to watch a âtutorialâ that long, and I prefer articles to get my information since I can pick out relevant details rather than trying to skip sections of a video.
I learned Python basics through CS Circles, and then proceeded to improve/test my problem solving skills. I did this by doing CCC questions which you can find (among other contest problems) at DMOJ. Other sites to improve your algorithmic problem solving skills include HackerRank and LeetCode (although this is mainly for interview preparations).While I was doing this, I was coding with the default IDLE editor! I did this for 3 months and then I found out about PyCharm which has a slight learning curve but is loads better in terms of features and at improving productivity. Nowadays, I use both PyCharm and Visual Studio Code.I personally have an entire folder dedicated to snippets of code I could use in the future, so I suggest you also do that and maybe even add some of the code snippets in this article in there so that you can read your own examples instead of Googling them or coming back to this article.
These are some tips that are not bound to programming but just life and productivity in general.Know your keyboard shortcuts Know both the program specific ones (browser, explorer, IDE of choice, etc.) and also OS specific ones (e.g. Win + R for run). Know the command lineInstead of doing a calculation by hand or opening an IDE to create and run a script, you can actually execute Python code from the command line. Aside from the common batch functions (e.g. ls, cd), knowing how to use Python from the command line will save you a lot of timeKnow how to GoogleGoogle (or your search engine of choice), is my best friend and should also be yours. It has saved me a lot of time and so it could also save you a lot of time. It canât do that if you donât use it or donât know how to use it. When you Google something, your query needs to be general enough that you can find answers, but also specific enough so that those answers are relevant.Problem Breakdown StrategyThis goes hand in hand with Googling. Suppose you have a problem/project. You need to break it down into smaller parts. You then need to analyze each of these parts and see if they are small enough for you to complete each of them. If not, either your missing some knowledge that you should Google or the part is too big and needs to be broken down again. You keep doing this recursive procedure until your project has been split into solvable parts so that you can complete them and then weave together a project. When I search and find answers through Google, I donât expect them to be 100% what I need. I usually need to remix them into what I want and thatâs what you should also expect: the bare minimum solution that takes you at least one step forward.With these tips stated, you can do a couple of different things next. You can skim the rest of the document and make notes on the snippets of code I feature (what I would do personally), read only the headings, skip to the project ideas section, or stop reading altogether as my tips are so useful.
In CS Circles, they bring abou`t the print function and some of its optional parameters but itâs easy to forget about them so here it is again.
Input function and String FormattingThe input function has an optional parameter so that it can also act as a prompt and if you are using Python 3.6+, you can make use of f-strings.
For Loops I want to make clear to you that a for loop, is not a while loop as it is in other languages. In Python, a for loop is an iteration over an iterable object.The range function has three parameters, two of them being optional. Do not write use the range function with an explicit start value of 0 because 0 is the default start value, (unless of course you are modifying the default step value of 1).In this example, I will show you exactly what I mean by ânot a while loopâ and how a for loop (specifically range) does not add to the temporary value.
If you run this code, youâll notice that the output is increasing by 1 each time even if we are adding 2 to i at every loop. This is because i is set to the next value in range and isnât actually being increased by one each time. This means that we can actually iterate over all sorts of iterable objects, like lists, without having to use range and indexing.
Here I introduced the keyword pass, this is to avoid errors in otherwise empty blocks.If you do want to keep track of the index as well as the item, you still donât have to use range, you can use the built-in function enumerate.
You can think of enumerate as turning an iterable into an iterable of pairs (index, item of iterable at index).You can also use the next function to retrieve the next value in an iterable (if there is no next item, an error will be raised).
Iâm including this because if you search âhow to read files in pythonâ on Google, you are given this which teaches it the old way and not the modern approach.
If you are curious why the r+/w+ is not the default, think about how a file cannot be opened to be written to if it is being âwrittenâ to by another program. If you just need to read a file, it being open in another program means that you wonât be interfering with the other program.
By this point if you are following along in PyCharm, you would have seen some squiggly lines, especially under âExceptionâ in the above code. These squiggly lines help you to avoid syntax errors, follow style guidelines, and bring attention to code that could be doing something you didnât want it to be doing.
So what are these iterables I keep mentioning? Yes a list is an iterable and so are tuples (which you should already know of).There are also dictionaries, sets and generators (not discussed here). Dictionaries are like hash tables in other languages, because they âhashâ the key to store information.
Data Structure Usage (Efficiency) The data structure you use is very important to writing good code.use dictionaries if order doesnât matter + each key has information (value) associated with ituse sets if order doesnât matter + no values per key (e.g. keeping track of what you have âusedâ per se)use tuples if you need ordered data but donât need to modify the data (e.g. coordinates)use lists if you need order and mutability (most flexible)You canât use sets or dictionaries or sets if you need to keep track of duplicates. Thatâs because sets and dictionaries hash the keys so that it is super fast (O(1)) to check if a key is in a dictionary. This does mean that you canât use lists, sets, and generators as keys (but you can definitely use tuples as long as lists are not nested).Dictionaries are also like JSON objects so you can actually use the json module to export them to a JSON file. Note that if youâre using sets as values, they are converted to lists in an exported json file.Miscellaneous FunctionsSometimes you will see functions like
One of the most beautiful parts of Python is list comprehensions; one liners to create lists.
You can also use list comprehensions to create dictionaries and sets
The third example is a generator. There are some use cases for it, so do your research before using them as they are an advanced topic not for this article.
There is one very important distinction between primitive variables and iterable variables. For example.
This is especially important when dealing with nested iterables with how you create nested iterables and also copy them. Try out these examples yourself.
Copying IterablesTo make a shallow copy, use .copy(). BUT, note that for any nested iterables, only the reference is copied, not the actual nested list. Thatâs why itâs called a shallow copy. To deepcopy, we can use the copy module.
Memoization is the caching of function return results in order to speed up repetitive calculations. An example would be the recursive implementation of the Fibonacci sequence.
An exercise is to make an addition function (a, b) that uses memoization.
Usually used in place of a function parameter if the calculation is short. For example, sorting.
Modules play a big part in projects you will do. Some built-in ones are os, shutil, copy, glob, and threading.os
Environmental variablesSpecify project secrets in a
file
globUsed for getting a list of files/folders
ThreadingI have already written some threading example for you to look at here. Just follow the instructions in the gist.
You need to use `pip install module_name` to install modules.Some modules that are common are requests, beautifulsoup4, PIL, and flask. If youâre working on a big project, youâll probably end up using 3rd party modules.
ClassesI did not cover classes because that is more about OOP than Python programming and the use cases for classes are very small. One thing you should know when you are learning classes is __slots__ property, so do search that up on your own.GeneratorsAgain this is an advanced topic and learning about it now will only lead to confusion, its best to learn this on your own or in a practical setting.DecoratorsI covered only the basics of decorators. There are decorators used by lots of other 3rd party libraries and different use cases (e.g. timing functions) so I suggest you do your own research on them as well.git and git workflowThis is very important when your collaborating with others or are working for a company. Git is a versioning tool used so that mistakes donât hurt you, and for letting you work on multiple features at the same time.other builtin modulesSuch as itertools, threading, multiprocessing, and more.
You can find a list of project ideas below:
Comment if you have any questions.Thanks for reading and good luck to your learning journey.
Reference : https://hackernoon.com/intermediate-python-refresher-tutorial-project-ideas-and-tips-i28s320p