r/pythontips • • Oct 23 '18

Concepts that help make coding in python “click”

29 Upvotes

I’m an engineering student and have taken 2 courses in python and am now working a part time job writing python code. I know Im not a beginner since I have a decent amount of experience and can complete most of the tasks required of me, but I feel like my level of understanding and progress is at a standstill.

When I open docs for packages and libraries I am not familiar with I have a hard time fully grasping the necessary concepts within the docs to implement them myself. I feel like this leads to me writing inefficient code and also wastes a lot of time since I spend a lot of time looking for similar code examples.

I know there is no magic answer here and obviously more practice will always be beneficial, but I am wondering if there is anyone out there that might have been in the same situation that was able to reach that next level of understanding? If so, are there any concepts that once you understood helped get you to that next level of writing code and becoming more efficient at programming in python?

This could be a dumb question but figured this would be the best place to get an answer even if I do get roasted. Any and all answers would be much appreciated! Thanks


r/pythontips • • Oct 29 '16

Syntax Initializing dictionaries from a list of key-value pairs

29 Upvotes

Initializing a dictionary is typically done via:

d = {'a': 1, 'b': 2, 'c': 3}

However a dictionary can also be initialized in the same way with two-tuples:

dict([('a', 1), ('b', 2), ('c', 3)])

This allows you to use create a dictionary from from any pair of iterables.

>>> a = ['a', 'b', 'c']
>>> b = [1,2,3]
>>> dict(zip(a,b))
{'a': 1, 'b': 2, 'c': 3}

zip can be used to generate an iterable of tuples from any number of iterables.

In this sense, the .items method is the inverse of the dict initializer

>>> list(d.items())
[('a', 1), ('b', 2), ('c', 3)]
>>> dict(d.items())
{'a': 1, 'b': 2, 'c': 3}

This initialization method is especially useful for OrderedDict, where initialization order matters.

 >>> from collections import OrderedDict
 >>> OrderedDict(zip(a,b))
 OrderedDict([('a', 1), ('b', 2), ('c', 3)])

r/pythontips • • Jun 07 '16

Standard_Lib Use defaultdict to have less checks if item is in the dict

29 Upvotes

If you have a code like this:

some_dict = {}
...
if some_key not in some_dict:
    some_dict[some_key] = []
some_dict[some_key].append(some_value)

it could be rewritten like this:

some_dict = defaultdict(list)
...
some_dict[some_key].append(some_value)

if behaves like a normal dictionary, but if it's missing a key,value pair - it will automatically create one for you with default value for the type you've provided. In this example it will be an empty list, but there are more usages for this.

Sometimes I am using

defaultdict(int)

to simulate Counter in python 2.6.


r/pythontips • • Sep 01 '24

Data_Science I am sharing Data Science courses and projects on YouTube

28 Upvotes

Hello, I wanted to share that I am sharing free courses and projects on my YouTube Channel. I have more than 200 videos and I created playlists for learning Data Science. I am leaving the playlist link below, have a great day!

Data Science Full Courses & Projects -> https://youtube.com/playlist?list=PLTsu3dft3CWiow7L7WrCd27ohlra_5PGH&si=6WUpVwXeAKEs4tB6

Data Science Projects -> https://youtube.com/playlist?list=PLTsu3dft3CWg69zbIVUQtFSRx_UV80OOg&si=go3wxM_ktGIkVdcP

Python Programming Tutorials -> https://youtube.com/playlist?list=PLTsu3dft3CWgJrlcs_IO1eif7myukPPKJ&si=eFGEzKSJb7oTO1Qg


r/pythontips • • Jul 28 '24

Python3_Specific Visualize code execution step by step.

28 Upvotes

https://www.pynerds.com/visualize/

The visualizer allows you to view the execution of Python code line by line.

I am not yet fully done making it but it is operational.

What do you think about the visualizer?.


r/pythontips • • Oct 20 '23

Module Which are the free Platforms to Host Python Apps?

28 Upvotes

As the title suggest, I want to deploy my python projects. Based on your experience and it’s usage please suggest me some best platforms to host especially python app that helps backend projects.

I’m a student so please suggest FREE only!


r/pythontips • • Feb 28 '23

Python3_Specific __init__ and __call__ In Python - How They Differ And What They Do

28 Upvotes

You may have encountered the methods in Python that are prefixed and suffixed with double underscores, those methods are called "Dunder Methods". These methods are also called "magic methods".

Dunder methods are used to overload specific methods in order to make their behaviour unique to that class.

We will look at two dunder methods(__init__ and __call__) that are commonly used in Python classes.

The __init__ method is also called the constructor method which is used to initialize the objects of the specific class whereas the __call__ method allows us to call the object of the class like a function.

The __init__ method created without passing parameters is called the default __init__ constructor.

When we call a function using (), in actuality, the __call__ method is implemented in the function.

Here is the complete guide to use the __init__ and __call__ with examples👇👇

__init__ and __call__ In Python - How They Differ And What They Do


r/pythontips • • Feb 05 '23

Module Tip - Use Python Builtin Binary Search (Bisect) on Sorted List

28 Upvotes

Hello all,

As you might know, the most efficient way to search a sorted array in Python is using Binary Search which is logarithmic time as opposed to linear time in the brute force method. Doing operations on sorted lists is common practice in many coding interview questions.

If you are not asked to implement binary search from scratch I recommend everyone get familiar with how to use the bisect_left() method, which many people may not know about. This is essentially a built-in binary search implementation in Python as opposed to coding it from scratch. So if you need binary search and forgot how to implement it or do not have time to write it up, consider using this.

It is very simple to use and involves one line of import code (no need to pip install) and one line of implementation.

```

from bisect import bisect_left

index = bisect_left(array, goal)

```

This will allow you to find where the leftmost index of an item would be inserted in a sorted array. This can potentially save you time and keep your code cleaner.

If you have any more questions or thoughts on this method let me know below. I also have a youtube video that better shows the implementation:

https://youtu.be/iwVHbZa9NH8

***Note there is also the bisect_right method which is very similar but instead finds the rightmost index, however, it is much less commonly used.


r/pythontips • • Jul 15 '22

Data_Science what are the tip a beginner takes to solve python coding problems?

28 Upvotes

Hi,

I'm switching my profile from construction line to IT line and have started preparing with python language but it seem to be difficulty in solving the basic problems. can anybody please, give some suggestions or tips how to work on this. How can I improve my coding?

Looking for some good suggestions:

Thanks


r/pythontips • • Jun 06 '22

Standard_Lib Reduce Python code runtime

28 Upvotes

Hi, I have created a stock screener by fetching realtime data from yfinance and loading them to pandas and making some calculations.

Now the issue is, this screener runs about 400 seconds to scan 190 stocks, with that speed i would likely miss lot of scalping oppurtunities.

Can you suggest how can i speed up the code or check which process takes longest time, i used cprofile but it has tons of entries making impossible to understand.

My code is loading data from yfinance, creating 10 new columns with calculations using python ta & np where function


r/pythontips • • May 18 '22

Module Python community

28 Upvotes

Hi, I am about to start learning python and I know how learning new stuff can be hard on the long run. It’s why I think, like other in my situation, being alone in my room doesn’t help. So I was wondering if there is special discords to help each other stay focused or subreddit I don’t know :)


r/pythontips • • Apr 24 '22

Module How to use MicroPython on Docker!

28 Upvotes

You don't need a physical microcontroller to code in MicroPython.

Docker is perfect for when you want to run tests on your code, or if you want to learn MicroPython without buying hardware.

Here's how to do it: https://bhave.sh/micropython-docker/


r/pythontips • • Mar 14 '22

Module DIY WiFi RGB Floor Lamp using MicroPython

28 Upvotes

Learn the hardware setup for safely connecting an ESP32 to two Neopixel strips, and embedding that inside an IKEA lamp. Then we proceed to code basic RGB light control and HSV light control in MicroPython, followed by implementing an awesome "Niagara Falls" rainbow animation! Finally we learn to use WebREPL to upload and run code remotely over WiFi so that the project can be powered independently of your computer.

https://www.instructables.com/DIY-WiFi-RGB-Floor-Lamp-Ikea-Hack/


r/pythontips • • Jan 21 '22

Module How to send someone my PyGame without them having to download PyGame?

28 Upvotes

Hi! I’m currently working on a PyGame project and I had asked my professor if he could download PyGame on his computer so that my program can run on his computer. Unfortunately, he told me I need to figure out how to send my PyGame program in a way that the PyGame library is already loaded on the project itself so he doesn’t have to download anything extra before submission. Any tips on how to do this?


r/pythontips • • Nov 19 '21

Python3_Specific Course recommend

28 Upvotes

Hello, i just want to recommend Udemy course 100 days of code Python. Im in half and there is tons of informations. From total beginer to "professional". You will leran basics, API, git, Django etc... I think its a good package to start. Teacher Anglea is sympatic, funny and her voice is not boring. Now is all courses on Udemy for 9.99euro, if someone dont know how to start, this is my tip.


r/pythontips • • Oct 24 '21

Syntax pls explain 3rd line of this simple python code?

28 Upvotes

if __name__ == '__main__':
n = int(input())
arr = map(int, input().split()) #3rd line


r/pythontips • • Oct 18 '21

Long_video Automated Testing in Python with pytest, tox, and GitHub Actions

28 Upvotes

https://www.youtube.com/watch?v=DhUpxWjOhME

Showcase to your future employer your knowledge of python projects best practices by using:

  • mypy for static type checking

  • pytest for unit tests

  • flake8 for linting

  • Github actions for continuous integrations (CI/CD)

More curated python content on https://tutobase.com/t/python

What other tools do you use?


r/pythontips • • Aug 31 '21

Data_Science 15 Python Snippets to Optimize your Data Science Pipeline

28 Upvotes

In this post I will share 15 snippets of code to help with different aspects of your data analysis pipeline. Visit here: https://www.thinkdataanalytics.com/python-snippets-data-science-pipeline/


r/pythontips • • Jun 14 '21

Standard_Lib This is how you can create different types of charts using Python

28 Upvotes

So I created a small tutorial on how you can create different charts and graphs using Matlabplot and Plotly:

https://geekyhumans.com/draw-various-types-of-charts-and-graphs-using-python/


r/pythontips • • May 16 '21

Algorithms Learn Python Free Tutorials

28 Upvotes

Why Do I Learn Python?

Python is an easy language with simple syntax. Python is becoming popular day by day as it is the major backbone of new technologies like data science, machine learning, and artificial intelligence. So several educational websites are offering the best Python courses.

https://techarge.in/getting-started-with-python/


r/pythontips • • May 02 '21

Python3_Specific Convert Pandas DataFrame to NumPy Array in Python

28 Upvotes

Learn how to easily convert Pandas DataFrame to NumPy array in Python.

https://pyshark.com/convert-pandas-dataframe-to-numpy-array-in-python/


r/pythontips • • Sep 29 '20

Module How to guarantee your program is safe

28 Upvotes

Ther's out there any certification that a program is safe without having it belonging from windows or other known developers societys? I recently created a little program and a friend of mine told me that norton test was successfully passed but windows defender told him that my application could harm is pc. The only 2 thing i can thin about windows defender to point out as dangerous are the creation of a db inside AppLocal/Local/My-program-name and accessing the web to load and collect the input data to a mysql database. Are those libraries untrusted? I build the exe with pyinstaller


r/pythontips • • Jul 28 '20

Standard_Lib Introduction to sqlite for python

28 Upvotes

“Do You Know Python Has A Built-In Database?” by Christopher Tao https://link.medium.com/w6aAXqFMt8


r/pythontips • • Jun 21 '20

Long_video Gnubesoft: A Fun Introduction to Internet Security - Part 1

28 Upvotes

Security is an important aspect of programming that a lot of people neglect. 99% of the hacks out there are just a matter of someone being lazy. In these next 2 videos I want to cover responsible security, while also showing how you can have some fun with Game Theory!

https://www.youtube.com/watch?v=35OH6w1GbY8

This is part of my Intermediate Python Series, which builds a cross-platform desktop app that acts as a private P2P network. You don't have to watch the whole Intermedia Python series to follow this, but it will probably help to watch the Websockets videos since we're using that in this example.