torsdag 18 augusti 2022

Python | Avoid IndexError by slicing

"IndexError: list index out of range" can for example be avoided by doing [0:1] instead of [0] 

Let's say you're handling iterators that sometimes are empty and do something like "...if list_ else []". 

You're doing this to avoid trying to access an index that doesn't exist when the list is empty, like this: [][0].


If you don't need the item directly and it's ok to return an iterable, an easier or at least shorter way of returning empty iterable can be to return a sliced version of it. 

[][:1] returns an empty list []

["Test"][:1] returns ["Test"]


Other working examples:[][0:0], [][1:0], [][0:2] [][100:25], [][25:100]

These all return [].




torsdag 4 augusti 2022

Decorators in Python

Decorator is a good name to describe what it does although the concept is a bit confusing at first. 

When a decorator has been made, it can potentially be used on any function you like. For example, you can extend/enhance your function to make it calculate how long it takes to run it.
All you have to do is adding "@your_decorator_name" just above the function you like to extend.
This means that additional code will be run, using your function in whatever way the decorator is defined to run it. And it uses the same arguments.

Example:


import functools

def your_decorator(base_func):
    @functools.wraps(base_func)
    # extended_func will be called instead of base func
    # since it's returned and replaces base func
    def extended_func(input_from_base_func):
        print("Printed inside the extended func")
        # This will call will_be_extended
        base_func(input_from_base_func)
        return "Returned when will_be_extended is called"
    return extended_func

@your_decorator
def will_be_extended(used_by_extension):
    # Do something
    print("Printed from base function")
    # This prints: "Sent to base func from decorator"
    print(used_by_extension)
   
# will_be_extended is called but the extended_func is returned
# automatically instead, including the text argument.
from_extended_func = will_be_extended("Sent to base func from decorator")

# This will print the returning value of extended_func,
# "Returned when will_be_extended is called"
print(from_extended_func)

 

måndag 1 augusti 2022

Python - Journey from intermediate to expert Pythonista

 As with any programming language you learn, you need a roadmap. It might not be obvious how to personalize such a path.

Things to take into consideration is for example prior knowledge and which format that works best (video or text).

The basics is similar between many languages. Most courses starts from the very basics and it can sometimes be hard to skip content in a course or tutorial.

Today there's a ton of readily available both free and paid learning resources online. There's an argument to be made that for motivated, self-learning is enough. Many employers care more about what skills and knowledge you actually have and less about which schools you've graduated from.

I started learning Python a few months back and I would like to share my views on the learning experience so far.


My general opinions regarding learning programming:

Videos

Great for explaining difficult topics.

Articles

Great for a more in-depth understanding about specific subjects.

Books: 

Great as guided tours on laid out paths. Make sure it's the right path before investing your time in a book that might not be for you. 

Podcasts:

Great when doing something else.

Apps (Android/iOS):

Great for studying when you're not at home.

Projects:

Creating projects is incredibly important. This is the way to actually learn what you've consumed and read. Doing projects is what makes you actually learn and memories.

Problem solving exercises like leetcode, codewars or checkio:

Solving engaging challenges and fun tasks is nice a complement to your studies. It also gives a confidence boost and lets you know your skill level.  You can share your code and interact with others. 

Other thoughts:

If possible, finding a mentor gives motivation a boost. Same goes with forums, slack and discord. 

Knowing how to do research and use search engines efficiently is crucial. More or less all questions you might have is already answered.

As with everything, a clear goal with what you're doing and why you learn is essential for motivation. Maybe becoming a data analyst even data scientist? 


Finding a path

As someone with a bachelor in computer science and experience with languages like Java and C++, doing basic Python tutorials wasn't ideal. I have tried many services online. Here are my recommendations:

CS50's Introduction to Programming with Python[Video format] (Free) (harvard.edu/course/cs50s-introduction-programming-python)

I took this course after watching this review by "Python Programmer": 


CS50 will be challenging for someone new to programming. With prior knowledge you can increase the playback speed.

Real Python [Basic to Master] (Paid and Free) realpython.com

I just bought a subscription. This site seems to have it all. Articles, videos, learning paths, community, slack, updates, quizzes, Q&A with experts, books (cost extra) and more.

Example of article: realpython.com/python-time-module


App [Basic to Intermediate] (15 days Free) "Sololearn" (Android/iOS) 

It is a little hard to type on the phone but the material was incredible and was made into a fun game-like experience with leaderboards and certificates.

Try to complete these courses: Python Data Structures, Intermediate Python and Python for Data Science. The SQL course is also really good. 


Books

So far my favorite books are (intermediate level) "Fluent Python" and "Python tricks the book".


Podcast

There are many but one that I've listen to a lot is realpython.com/podcasts/rpp/


Some Youtube recommendations

Corey Schafer youtube.com/c/Coreyms

Fireship youtube.com/c/Fireship

Python Engineer youtube.com/c/PythonEngineer/

Coding Tech youtube.com/c/CodingTech

Python Programmer youtube.com/c/FlickThrough

freeCodeCamp.org youtube.com/c/Freecodecamp