Python Basics Notes @pythonRe.pdf
2.4 MB
Python Basics Notes ππ
https://t.iss.one/pythonRe π
#Python #Coding #Programming #LearnPython #Tech #DevCommunity
https://t.iss.one/pythonRe π
#Python #Coding #Programming #LearnPython #Tech #DevCommunity
β€3π₯2
The Python library itertools contains many useful functions. πβ¨
One of them is compress(), which returns an iterator over the elements from data, for which the corresponding element in selectors is equal to True. ππ»
Here's an example: ππ
#Python #Programming #Itertools #Coding #Tech #DataScience
One of them is compress(), which returns an iterator over the elements from data, for which the corresponding element in selectors is equal to True. ππ»
Here's an example: ππ
#Python #Programming #Itertools #Coding #Tech #DataScience
π₯2
Cheat sheet on the basics of Python: ππ
basic syntax and language rules π
scalar types β basic data types (int, float, bool, str, NoneType) π’
datetime β working with date and time π β°
data structures β Python data structures (list, tuple, dict, set) π
list β mutable lists for storing data collections π
tuple β immutable sequences of values π
dict (hash map) β storing data in a key-value format π
set β unique elements without order π
slicing β obtaining parts of sequences through indices and step βοΈ
module/library β connecting modules and libraries π
help functions β using help() and dir() to explore the Python API π
#Python #Coding #DataScience #Programming #Tech #DevCommunity
basic syntax and language rules π
scalar types β basic data types (int, float, bool, str, NoneType) π’
datetime β working with date and time π β°
data structures β Python data structures (list, tuple, dict, set) π
list β mutable lists for storing data collections π
tuple β immutable sequences of values π
dict (hash map) β storing data in a key-value format π
set β unique elements without order π
slicing β obtaining parts of sequences through indices and step βοΈ
module/library β connecting modules and libraries π
help functions β using help() and dir() to explore the Python API π
#Python #Coding #DataScience #Programming #Tech #DevCommunity
β€5π₯3π2
Do you know that Python can shift sequences without slicing and creating new lists? π€
When you need to cyclically shift data, many use slicing:
But
A negative value rotates the queue in the other direction. β¬ οΈ
This is useful for ring buffers, task schedulers, cyclical queues, and round-robin algorithms. π
π₯
#Python #Programming #Deque #CodingTips #Tech #DevCommunity
When you need to cyclically shift data, many use slicing:
data = data[-1:] + data[:-1]
But
deque.rotate() does this at the level of the data structure and usually works more efficiently for cyclical operations. πq.rotate(1)
A negative value rotates the queue in the other direction. β¬ οΈ
q.rotate(-2)
This is useful for ring buffers, task schedulers, cyclical queues, and round-robin algorithms. π
workers.rotate(-1)
π₯
deque.rotate() allows you to implement cyclical data structures without manual index logic and without creating new lists. π‘#Python #Programming #Deque #CodingTips #Tech #DevCommunity
β€7
How to check for the presence of subclasses in Python? ππ§
Here's how you can do it:
This function uses the
#Python #Programming #Subclasses #Coding #Dev #Tech
Here's how you can do it:
import inspect
def has_subclasses(cls):
return any(issubclass(sub, cls) for sub in inspect.getmembers(sys.modules[cls.__module__], inspect.isclass))
This function uses the
inspect module to find all subclasses of the given class. π οΈ#Python #Programming #Subclasses #Coding #Dev #Tech
β€5π1
π Reminder about Python map()!
map() β a built-in function that applies the specified function to each element of an iterable object (list, tuple, set, etc.).
The picture shows the basic syntax, an example of use with lambda, and a typical case β data transformation without a manual for loop.
Save it to quickly remember the syntax!
ππ»πΊοΈ #Python #Coding #Programming #LearnToCode #DevTips #Tech
map() β a built-in function that applies the specified function to each element of an iterable object (list, tuple, set, etc.).
The picture shows the basic syntax, an example of use with lambda, and a typical case β data transformation without a manual for loop.
Save it to quickly remember the syntax!
ππ»πΊοΈ #Python #Coding #Programming #LearnToCode #DevTips #Tech
β€7π1
Why is enumerate() used in Python? π€π
It allows you to simultaneously obtain the value of an element and its index when iterating through a list. πβ¨
This is more convenient and more readable than manually working with a counter. β π
#Python #Coding #Programming #Dev #Tech #Code
β¨ Join Best TG Channels
https://t.iss.one/addlist/0f6vfFbEMdAwODBk
βοΈ Join Our WhatsApp Channel
https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
It allows you to simultaneously obtain the value of an element and its index when iterating through a list. πβ¨
This is more convenient and more readable than manually working with a counter. β π
for i, item in enumerate(items):
print(i, item)
#Python #Coding #Programming #Dev #Tech #Code
β¨ Join Best TG Channels
https://t.iss.one/addlist/0f6vfFbEMdAwODBk
βοΈ Join Our WhatsApp Channel
https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
β€4π1π1
Deep copying of objects with the copy module ππ¦
The link trap in Python ππ³οΈ
When you assign a list to another variable (A = B) or make a regular slice (A = B[:]), Python doesn't physically copy the data. It simply creates a new reference to the same objects in memory. If the list contains other mutable objects (lists, dictionaries, custom classes), standard copying methods will only create a shallow copy. The copy module allows you to control this process.
β Breaking the links: The deepcopy function recursively traverses the entire data structure and creates honest, independent duplicates for each nested element. This ensures that changes in the copy will not harm the original data. ππ
β Safe state: The use of deep copying is critical when implementing design patterns (for example, Snapshot/Memento), creating game state backups, or when you pass complex configurations to functions that may modify them accidentally. π‘οΈπΎ
β A sensible balance: It's worth remembering that deepcopy works slower and consumes more memory than shallow copying, as it spends resources on creating new objects and checking for cyclic references. Use it specifically when there are nested mutable containers within the structure. βοΈπ§
#Python #Programming #DeepCopy #Coding #Tech #Dev
β¨ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk
βοΈ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
import copy
# Original list with nested structure
original = [[1, 2, 3], [4, 5, 6]]
# 1. Shallow copy
shallow = copy.copy(original)
shallow[0][0] = 'X'
# Oh no! Both lists have changed, because the nested list wasn't copied, but passed by reference
print(f"Original after shallow: {original}") # [['X', 2, 3], [4, 5, 6]]
# Restore the data
original = [[1, 2, 3], [4, 5, 6]]
# 2. Deep copy
deep = copy.deepcopy(original)
deep[0][0] = 'X'
# Everything is fine! Only deep has changed, the original remains untouched
print(f"Original after deep: {original}") # [[1, 2, 3], [4, 5, 6]]
The link trap in Python ππ³οΈ
When you assign a list to another variable (A = B) or make a regular slice (A = B[:]), Python doesn't physically copy the data. It simply creates a new reference to the same objects in memory. If the list contains other mutable objects (lists, dictionaries, custom classes), standard copying methods will only create a shallow copy. The copy module allows you to control this process.
β Breaking the links: The deepcopy function recursively traverses the entire data structure and creates honest, independent duplicates for each nested element. This ensures that changes in the copy will not harm the original data. ππ
β Safe state: The use of deep copying is critical when implementing design patterns (for example, Snapshot/Memento), creating game state backups, or when you pass complex configurations to functions that may modify them accidentally. π‘οΈπΎ
β A sensible balance: It's worth remembering that deepcopy works slower and consumes more memory than shallow copying, as it spends resources on creating new objects and checking for cyclic references. Use it specifically when there are nested mutable containers within the structure. βοΈπ§
#Python #Programming #DeepCopy #Coding #Tech #Dev
β¨ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk
βοΈ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
Telegram
AI PYTHON π
Youβve been invited to add the folder βAI PYTHON πβ, which includes 14 chats.
β€3