Instead of this
#Source "Python Tricks" by Dan Bader
names = ['Alice', 'Bob', 'Dilbert']
Use:>>> names = [
... 'Alice',
... 'Bob',
... 'Dilbert',
... ]
That way there’s one item per line, making it perfectly clear which one was added, removed, or modified when you view a diff in your source control system. It’s a small change but helpful to avoid silly mistakes. It also easier for your teammates to review your code changes.#Source "Python Tricks" by Dan Bader
#not_secure
#Secure
f = open('hello.txt', 'w')
f.write('hello, world')
f.close()
This implementation won’t guarantee the file is closed if there’s an exception during the f.write() call#Secure
with open('hello.txt', 'w') as f: f.write('hello, world!')
Context Managers
#not_secure
#Secure
#not_secure
f = open('hello.txt', 'w')
f.write('hello, world')
f.close()
This implementation won’t guarantee the file is closed if there’s an exception during the f.write() call#Secure
with open('hello.txt', 'w') as f:
f.write('hello, world!')
Behind the scenes context manager is implementing the following:f = open('hello.txt', 'w')
try:
f.write('hello, world')
finally:
f.close()
#Source "Python Tricks" by Dan BaderSingle Leading Underscore:
!!!Note: if you import all the names from the module, Python will not import names with a leading underscore(unless the module defines an all list that overrides this behavior)
#Source "Python Tricks" by Dan Bader
_var
is a hint to tell another programmer that a variable or method starting with a single underscore is intended for internal use. !!!Note: if you import all the names from the module, Python will not import names with a leading underscore(unless the module defines an all list that overrides this behavior)
#Source "Python Tricks" by Dan Bader
What are dunders?
In the python community Dunders are double underscores.
For example, you’d pronounce __baz as “dunder baz.”
In the python community Dunders are double underscores.
For example, you’d pronounce __baz as “dunder baz.”
PUT with PATCH
PUT is idempotent, while PATCH is not.
What does idempotent mean?
Idempotent operations produce the same result even when the operation is repeated many times. The result of the 2nd, 3rd, and 1,000th repeat of the operation will return exactly the same result as the 1st time.
common idempotent methods:
PUT is idempotent, while PATCH is not.
What does idempotent mean?
It means that any number of repeated, identical requests will leave the resource in the same state.or
Idempotent operations produce the same result even when the operation is repeated many times. The result of the 2nd, 3rd, and 1,000th repeat of the operation will return exactly the same result as the 1st time.
common idempotent methods:
GET, HEAD, PUT, DELETE, OPTIONS, TRACE
common non-idempotent methods: POST,PATCH, CONNECT
#functions
Function notes
Python adds an implicit return None statement to the end of any function. Therefore, if a function doesn’t specify a return value, it re- turns None by default. you can replace return None statements with bare
#Source "Python Tricks" by Dan Bader
Function notes
Python adds an implicit return None statement to the end of any function. Therefore, if a function doesn’t specify a return value, it re- turns None by default. you can replace return None statements with bare
#Source "Python Tricks" by Dan Bader
there are actually two dunder methods that control how objects are converted to strings in Python 3.
__str__ is one of Python’s “dunder” (double-underscore) methods and gets called when you try to convert an object into a string
Inspecting an object in a Python inter-
preter session simply prints the result of the object’s __repr__ .
Why Every Class Needs a __repr__
If you don’t add a str method, Python falls back on the result
of __repr__ when looking for __str__ .
__str__ is one of Python’s “dunder” (double-underscore) methods and gets called when you try to convert an object into a string
Inspecting an object in a Python inter-
preter session simply prints the result of the object’s __repr__ .
Why Every Class Needs a __repr__
If you don’t add a str method, Python falls back on the result
of __repr__ when looking for __str__ .
ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as 'a' or '@' or an action of some sort.
We have translated repo with huge list of things to learn into Uzbek, for Interview preparation for giants such as : Amazon, Facebook, Google, and Microsoft.
https://github.com/jwasham/coding-interview-university/blob/main/translations/README-uz.md
share with your friends )
https://github.com/jwasham/coding-interview-university/blob/main/translations/README-uz.md
share with your friends )
GitHub
coding-interview-university/translations/README-uz.md at main · jwasham/coding-interview-university
A complete computer science study plan to become a software engineer. - jwasham/coding-interview-university
#Deque
A deque (double-ended queue), from collections library, has the feature of adding and removing elements from either end(source). It is preferred over list in the cases where we need quicker append and pop operations from both the ends of container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity (source).
A deque (double-ended queue), from collections library, has the feature of adding and removing elements from either end(source). It is preferred over list in the cases where we need quicker append and pop operations from both the ends of container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity (source).
#Deque_continue
deque(maxlen=N) creates a fixed-sized queue. When new items are added and the queue is full, the oldest item is automatically removed. (Source: "Python Cookbook")
deque(maxlen=N) creates a fixed-sized queue. When new items are added and the queue is full, the oldest item is automatically removed. (Source: "Python Cookbook")
#SubArray, #SubSequence, #SubSet
Comparing SubArray SubSequence and SubSet
✅ Subarray is contiguous sequence in an array.
e.g we have an array of {1, 2, 3, 4}
subarray can be: {1,2,3}, {2,3,4}, {1,2} etc.
✅ A subsequence doesn't need to be contiguous, but maintains order
e.g subsequence can be: {1, 2, 4} {2, 4} {1, 3, 4} etc.
✅ A subset doesn’t need to maintain order and has non-contiguous behavior.
e.g subset can be: {4, 1,3} {3, 1, 2 } etc.
Comparing SubArray SubSequence and SubSet
✅ Subarray is contiguous sequence in an array.
e.g we have an array of {1, 2, 3, 4}
subarray can be: {1,2,3}, {2,3,4}, {1,2} etc.
✅ A subsequence doesn't need to be contiguous, but maintains order
e.g subsequence can be: {1, 2, 4} {2, 4} {1, 3, 4} etc.
✅ A subset doesn’t need to maintain order and has non-contiguous behavior.
e.g subset can be: {4, 1,3} {3, 1, 2 } etc.
sort() with sorted()
the sort() function will modify the list it is called on. The sorted() function will create a new list containing a sorted version of the list it is given.
the sort() function will modify the list it is called on. The sorted() function will create a new list containing a sorted version of the list it is given.