Python Codes
Open in Telegram
This channel will serve you all the codes and programs which are related to Python. We post the codes from the beginner level to advanced level.
Show more7 090
Subscribers
No data24 hours
No data7 days
No data30 days
Posts Archive
7 090
π Welcome to @datastructuresandalgoofficial π
To get job in top tech companies like ππ€π€ππ‘π, πΌπ’ππ―π€π£, ππππ§π€π¨π€ππ©, ππππππ€π€π , ππ©π you must know πΏππ©π ππ©π§πͺππ©πͺπ§ππ¨ ππ£π πΌπ‘ππ€π§ππ©ππ’π¨. And for excelling in tech field you should know πΏππ©π ππ©π§πͺππ©πͺπ§ππ¨ ππ£π πΌπ‘ππ€π§ππ©ππ’π¨.
In this Channel you will get all materials related Data Structures and Algorithms(DSA).
ππ€π§ π‘πππ§π£ππ£π πΏππ©π ππ©π§πͺππ©πͺπ§ππ¨ ππ£π πΌπ‘ππ€π§ππ©ππ’π¨(πΏππΌ) ππ€π§ ππ§ππ ππ€ππ£ π€πͺπ§ π©ππ‘πππ§ππ’ ππππ£π£ππ‘:
https://t.me/datastructuresandalgoofficial
So what are you waiting for?
Join right nowπ
https://t.me/datastructuresandalgoofficial
7 090
Join our other channels
And get the Best Content
For Basic Electronics : @BasicElectronics
For Python Learning: @Python_Codes
For Python Books : @PythonEbookz
For Knowledge: @TerrificKnowledge
For learning Basics: @BasicsToKnow
For Placements : @Aptitude_Tricky
What's Special Today:
@whatstodays_special
Udemy Channel: @udemy_channel
7 090
Sorts one list based on another list containing the desired indexes.
Use zip() and sorted() to combine and sort the two lists, based on the values of indexes.
Use a list comprehension to get the first element of each pair from the result.
Use the reverse parameter in sorted() to sort the dictionary in reverse order, based on the third argument.
CODE:
def sort_by_indexes(lst, indexes, reverse=False):
return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
x[0], reverse=reverse)]
Example:
a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']
b = [3, 2, 6, 4, 1, 5]
sort_by_indexes(a, b)
Output: ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']
sort_by_indexes(a, b, True)
Output: ['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']
Share and Support
@Python_Codes7 090
Learn from Technical Ravi
Educational Stock Marketing Channel
Can take calls for free and accurate results
Join Now
https://t.me/TechnicalTraderRAVI
7 090
FROM NOW, DONT WORRY WE GOT MR. RAVI HAVING MORE THAN 5 YEARS OF EXPERIENCE, TO SOLVE ALL YOUR PROBLEMS REGARDING STOCK MARKET ... ! π€
HE HAS HIS OWN FREE EDUCATIONAL CHANNEL WHERE HE PROVIDES FREE IMPORTANT UPDATES AND TIPS FOR INTRADAY TRADING AND INVESTMENT ADVICE ...! π΄πΊ
MOST OF THE PEOPLE ARE EARNING REGULARLY FROM HIS CHANNEL. π€©
JOIN THE BELOW LINK NOW AND LET YOUR MONEY WORK WHILE YOU SLEEP π
Join Now
https://t.me/TechnicalTraderRAVI
7 090
Converts a string to kebab case.
πUse re.sub() to replace any - or _ with a space, using the regexp r"(_|-)+".
πUse re.sub() to match all words in the string, str.lower() to lowercase them.
πFinally, use str.join() to combine all word using - as the separator.
CODE:
from re import sub
def kebab(s):
return '-'.join(
sub(r"(\s|_|-)+"," ",
sub(r"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+",
lambda mo: ' ' + mo.group(0).lower(), s)).split())
Examples
kebab('camelCase') # 'camel-case'
kebab('some text') # 'some-text'
kebab('some-mixed_string With spaces_underscores-and-hyphens')
# 'some-mixed-string-with-spaces-underscores-and-hyphens'
kebab('AllThe-small Things') # 'all-the-small-things'
Share and Support
@Python_Codes7 090
bifurcate_by
Splits values into two groups, based on the result of the given filtering function.
πUse a list comprehension to add elements to groups, based on the value returned by fn for each element.
πIf fn returns a truthy value for any element, add it to the first group, otherwise add it to the second group.
CODE:
def bifurcate_by(lst, fn):
return [
[x for x in lst if fn(x)],
[x for x in lst if not fn(x)]
]
Examples
Input:
bifurcate_by(['beep', 'boop', 'foo', 'bar'], lambda x: x[0] == 'b')
Output:
[ ['beep', 'boop', 'bar'], ['foo'] ]
Share and Support
@Python_Codes
7 090
What are named tuples in Python?
These are part of the collections module and act very similar to regular tuples
The main difference being that values stored in a named tuple can be accessed using field names instead of indexes.
For example, a point in the two-dimensional plane can be represented using two coordinates. In a regular tuple, these values would be accessed by index ([0] and [1]), but if we define a named tuple, Point, we can access them using x and y instead (although we can still use indexes, too, if we want):
Example:
from collections import namedtuple
# Regular tuple
p = (2, 4) # p[0] = 2, p[1] = 4
# Named tuple
Point = namedtuple('Point', 'x y')
q = Point(3, 5) # q.x = 3, q.y = 5
Example 2:
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y', 'z'], defaults = [1]);
a = Point(1, 1, 0); # a.x = 1, a.y = 1, a.z = 0
# Default value used for `z`
b = Point(2, 2); # b.x = 2, b.y = 2, b.z = 1 (default)
Share and Support
@Python_Codes7 090
Get all your investment related queries solved at one single place.
Tired of making loss in stock market ?π§
Join this channel for highly accurate tips from experience traders !π¦Έ
Services provided :
> Intraday calls in stocks and index
> Delivery calls for 10% in 15 days
> Stocks recommendation according to your needs.
Everything totally FREE as channel is PUBLICLY OPENED ! π
JOIN : π₯
https://t.me/TechnicalTraderRAVI
7 090
Technical Trader RAVI
FREE BANKNIFTY CALLS
FREE DELIVERY CALLS ( stocks )
FREE SWING TRADING CALLS ( stocks )
FREE TIPS 1 PER DAY
Want more ?? Join our paid services to get more such calls ... Inbox me ...!
PURELY EDUCATIONAL PURPOSE
NOT SEBI REGISTERED
https://t.me/TechnicalTraderRAVI
7 090
merge
Python, List
Merges two or more lists into a list of lists, combining elements from each of the input lists based on their positions..ππ» Use max() combined with a list comprehension to get the length of the longest list in the arguments. .ππ» Use range() in combination with the max_length variable to loop as many times as there are elements in the longest list. .ππ»If a list is shorter than max_length, use fill_value for the remaining items (defaults to None). .ππ»zip() and itertools.zip_longest() provide similar functionality to this snippet. Code:
def merge(*args, fill_value = None):
max_length = max([len(lst) for lst in args])
result = []
for i in range(max_length):
result.append([
args[k][i] if i < len(args[k]) else fill_value for k in range(len(args))
])
return result
Example:
merge(['a', 'b'], [1, 2], [True, False])
Output: [['a', 1, True], ['b', 2, False]]
Share and Support
@Python_Codes7 090
ππ» WELCOME ππ»
TO
INDIA'S BIGGEST
ONLINE BETTING I'd AVAILABLE HEREπ :
https://t.me/Bookiecircle_39
π€ BOOKIECIRCLE π€
π BINARY (SHARE MARKET)
β½ Scorrer
π ALL Cricket
πΎ Tennis
π΄ Horse Racing
π Grey Hound Racing
π° SLOT GAME
π£ Elections
π IPL fancy
π° LIVE CASINO
β οΈ TEEN PATTI
π’ WARLI MARKET
Payment Mode Google Pay/Phone Pay/Paytm
5% Deposit Bonus each time
π₯24*7 Instant withdrawals π₯
π₯within 5 minutes π₯
Contact Number
Minimum Amount: 100πΈ
Maximum Amount : unlimited
β½ππΈπ₯
π€Όββπ€Όββπ₯ππ°
https://t.me/Bookiecircle_39
7 090
What are named tuples in Python?
Python's named tuples are a very simple yet interesting feature that can make a developer's life easier. They are part of the collections module and act very similar to regular tuples, the main difference being that values stored in a named tuple can be accessed using field names instead of indexes.
For example, a point in the two-dimensional plane can be represented using two coordinates. In a regular tuple, these values would be accessed by index ([0] and [1]), but if we define a named tuple, Point, we can access them using x and y instead (although we can still use indexes, too, if we want):
Example:
from collections import namedtuple
# Regular tuple
p = (2, 4) # p[0] = 2, p[1] = 4
# Named tuple
Point = namedtuple('Point', 'x y')
q = Point(3, 5) # q.x = 3, q.y = 5
Apart from the increased readability of your code, named tuples provide a few other quality of life improvements. First and foremost, they allow for default values to be specified via the defaults iterable argument. Secondly, they have the ability to automatically rename duplicate or invalid fields via the rename boolean argument. And, finally, they even provide a convenient option to specify field names as a list or comma/space-separated string.
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y', 'z'], defaults = [1]);
a = Point(1, 1, 0); # a.x = 1, a.y = 1, a.z = 0
# Default value used for
z
b = Point(2, 2); # b.x = 2, b.y = 2, b.z = 1 (default)
Where's the catch? you might ask. Well, it seems like there's none! The obvious parallel to dictionaries in terms of syntax doesn't seem to go any further, as named tuple instances do not have per-instance dictionaries, meaning they require as much memory as regular tuples.
Share and Support
@Python_Codes