Visar inlägg med etikett Python. Visa alla inlägg
Visar inlägg med etikett Python. Visa alla inlägg

onsdag 9 november 2022

[Python] len() and time complexity

Some say "if you care about speed you shouldn't use Python". Others say "since Python isn't fast, you have to optimize it to make it viable".


The behavior of len() depends on the class definition.

Calling len() is always taking constant time for iterable data structures(string, list, tuple, etc.). That's because they have __len__() defined that way. They use a counter which is altered when the iterable in question is altered. len() is therefore just returning a counter in this scenario.

Without defining __len__() you can't use len() on it. Since it's up to the creator of the class to define what this dunder method should do and return, you can have any time complexion. You could for example traverse something every time the method is called. 

måndag 7 november 2022

Pygame for Python 3.11

As of writing this, Python 3.11 is a new release and in order to use it with Pygame you have to install Pygame in slightly different way:

pip install pygame --pre

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 

fredag 22 juli 2022

From a list, count how many numbers are within the standard deviation from the mean. (Python)

heights = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190]

height_sum = sum(heights)
mean = height_sum / len(heights)

# **2 makes negatives (heights less than the mean) become positive.
diff_list = [(x - mean) ** 2 for x in heights]
variance = sum(diff_list) / len(heights)

# We get the square root in order to get back the original units.
standard_deviation = variance **0.5

# Now we just need to find out how many heights are within the
# standard deviation from the mean.
count = 0
upper_lim = mean + standard_deviation
lower_lim = mean - standard_deviation

# Count how many heights are within the
# standard deviation from the mean.
for height in heights:
    if lower_lim < height < upper_lim:
        count += 1
       
print(count)






''' Or as a class, initiated with the heights list: '''

class Std():
   
    def __init__(self, data: list):
        self.data = data
        self.mean = self._get_mean()
        self.var = self._get_variance()
        self.std = self._get_std_from_var()
       
        self.upper_lim = self.mean + self.std
        self.lower_lim = self.mean - self.std
   
        self.in_range = lambda x: self.lower_lim < x < self.upper_lim
        self.count_within = len(list(filter(self.in_range, self.data)))

    def _get_mean(self):
        return sum(self.data) / len(self.data)

    def _get_variance(self):
        diff_list = [(x - self.mean) ** 2 for x in self.data]
        return sum(diff_list) / len(diff_list)

    def _get_std_from_var(self):
        return self.var **0.5

std = Std(heights)
print(std.count_within)

lördag 9 juli 2022

Flask - Absolute basic client input and render

The following example shows how you can handle input with Flask and update pages, in this case with the user input sent back to the client. 

I'm making this as short as possible. 

Requirements: pip install Flask 

Implementation: 
1. In your project root dir create: A file named app.py 
2. In your project root dir create: A folder named "templates" and inside a file named "index.html" 


index.html 
<html lang="en"> 
  <head> 
  <title>Flask Basic Example</title>
  </head>  
  <body>    
  <h1>Flask Post: {{ text_to_show }}</h1>    
  <form action="/" method="POST">      
  <input type="text" name="content" />     
  <input type="submit" />    
  </form>  
  </body></html>

app.py
from flask import Flask, render_template, request
app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        user_input = request.form['content']
        return render_template("index.html", text_to_show=user_input)
    return render_template("index.html")

if __name__ == "__main__":
    app.run(debug=True, port=5000)

How it's used: Run app.py and go to http://127.0.0.1:5000 in your browser. Type some input, press send and the server will update the HTML with your input in a HTML paragraph. The Internet is full of extensive guides and tutorials. Sometimes you just want a quick example.

Python Virtual Environment Setup

Working in a virtual environment is easy to setup and makes life easier thanks to the project isolation and dependency management.
Run once:
py -m pip install --user virtualenv
Run in project folder:
py -m venv env
Run in project folder:
.\env\Scripts\activate
Done! Happy coding.