The multiplication operator on a ordered collection (sequence), like strings, lists, tuples, byte sequences or byte arrays means that the items/objects inside are repeated. It does not mean creation of copies (shallow or deep ones) of the items/objects.
This can be tested with the help of the id() function which returns the memory address of an object. Two objects with overlapping lifetime can never have the same id.
An exception to this is small intergers and other immutables, which Python keeps a small cache of hence giving them the same ID.
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.
Steering wheels in pygame and arcade go under the name "joysticks". I was able to get my Logitech G27 Racing Wheel to work with pygame but not with Arcade. I made a project using Arcade and used the joystick handling from pygame.
"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]
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:
importfunctools
defyour_decorator(base_func):
@functools.wraps(base_func)
# extended_func will be called instead of base func
# since it's returned and replaces base func
defextended_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"
returnextended_func
@your_decorator
defwill_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,
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:
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.
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".
I've made an art viewer program in Unity. I'm now working on database update automatization in Python. That is because the files with art information provided are updated frequently.
OpenArtViewer uses the National Gallery of Art Open Data API and data from Metropolitan Museum of Art. The included SQLite database has stored info about the artworks in public domain (100K+) including image links to the artworks on the NGA and MM servers.
From the GUI you can search type of art and by title, year, artist. To the left there's a scroll bar with the results as thumbnails. These can be clicked to get more information and a higher resolution version of the artwork in the big panel to the right. A high resolution image can then be downloaded.
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.