TopTgm
simple-python-programming

Simple Python | Programming

Locale: en
Subscribers:51
Category: technology
Description:
We share useful tips, code examples, problem solutions, and news from the Python world.
?The get() method on Python dicts and its "default" arg

# The get() method on dicts
# and its "default" argument

name_for_userid = {
382: "Alice",
590: "Bob",
951: "Dilbert",
}

def greeting(userid):
return "Hi %s!" % name_for_userid.get(userid, "there")

>>> greeting(382)
"Hi Alice!"

>>> greeting(333333)
"Hi there!"


When "get()" is called it checks if the given key exists in the dict.

If it does exist, the value for that key is returned.

If it does not exist then the value of the default argument is returned instead.
4/23/2024, 11:04:25 AM
?Context menu: a python library to create and deploy cross-platform native context menus.

A python library to develop and deploy custom context menus.

▪️
4/23/2024, 9:51:34 AM
With f-strings you can use the = modifier for including the code of the variable interpolation:
>>> a = 1.6
>>> print(f'We have {a=} and {int(a)=}.')
>>> We have a=1.6 and int(a)=1.


You can use classes to define decorators instead of functions:
>>> class show_time:
... def __init__(self, func):
... self.func = func
... def __call__(self, *args, **kwargs):
... start = time()
... result = self.func(*args, **kwargs)
... print(f'It took {time() - start:.4f}s')
... return result
...
>>> @show_time
... def test():
... return sum(n**2 for n in range(1000000))
...
>>> test()
It took 0.1054s
333332833333500000


The mode attribute for open is 'r' by default:
>>> with open('test.txt') as f:
... print(f.read())
>>> It works!
4/22/2024, 9:09:47 AM
My latest TILs about Python

After so long, I still find basic stuff in Python that I didn't know about. Here are some of my most recent TILs.

You can use underscores to separate digits in a number and they'll be ignored by the interpreter:
>>> 1000000  # confusing
1000000
>>> 1e6 # not confusing but creates a float
1000000.0
>>> 1_000_000 # great!
1000000


The sum method accepts a second parameter that is the initial value of the sum:
>>> base = 20
>>> sum([1, 2, 3], base)
26
4/22/2024, 2:59:47 AM
?How to sort a Python dict by value

# How to sort a Python dict by value
# (== get a representation sorted by value)

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

>>> sorted(xs.items(), key=lambda x: x[1])
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]

# Or:

>>> import operator
>>> sorted(xs.items(), key=operator.itemgetter(1))
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
4/20/2024, 1:23:56 PM
??As part of the project's continued development, the Desbordante team has posted new Python script examples on GitHub. "As the project is currently under-documented, we hope these will be useful for our potential users. . To improve the overall level of documentation, we have also published a few tutorials - see the tutorials section," the developers clarified.
4/20/2024, 8:05:24 AM
? Desbordante data profiler 2.0.0 release

?On April 16, 2024, the second major release of , which allows you to search for various primitives in tables, took place. The source code of the project is under the GNU Affero General Public License v3.0. The first stable version of Desbordante was released in December 2023.

?Desbordante can perform two categories of tasks: searching for different primitives, and performing some work using primitive search algorithms. The core of Desbordante is a console application that has an extremely simple usage scenario: a csv table is fed as input, the primitive to be searched, such as a functional dependency, an algorithm and optionally parameters, is specified, and the output is a set of found dependencies. In addition, all Desbordante components are containerized.
4/20/2024, 8:05:10 AM
?Different ways to test multiple flags at once in Python

# Different ways to test multiple
# flags at once in Python
x, y, z = 0, 1, 0

if x == 1 or y == 1 or z == 1:
print('passed')

if 1 in (x, y, z):
print('passed')

# These only test for truthiness:
if x or y or z:
print('passed')

if any((x, y, z)):
print('passed')
4/19/2024, 3:41:31 AM
Automate the Boring Stuff with Python - Practical Programming for Total Beginners by Al Sweigart is "written for office workers, students, administrators, and anyone who uses a computer to learn how to code small, practical programs to automate tasks on their computer."
|| || ||
4/18/2024, 4:04:15 AM
How to use uv: A superfast Python package installer

Crafted by the Astral team, the creators behind the renowned Ruff Python linting tool, UV sets out to revolutionize Python project management. It aspires to consolidate various command-line tools like pip and venv into a single, comprehensive solution. What sets UV apart is its foundation in Rust, a choice made to enhance its performance significantly compared to its counterparts, possibly achieving speeds beyond comparison.

Read the manual
4/16/2024, 10:53:21 AM

Related Groups

Goat.Tech Global
Goat.Tech Global
technology18.67K

Goat.Tech is First Gamified On-chain Reputation with Native Yield to earn $ETH ? and $GOAT ? and boost on-chain ReputationSocial Links: linktr.ee/goatxtechWebApp:goat.techChannel: @goatxtech?Transparency and security build trust, which is key at Goat.Tech.?Goat.Tech employs a comprehensive set of contract files and modules, each designed to uphold platform security and prevent unauthorized withdrawals or access to funds.✅All smart contracts are public and audited by top-tier Audit company Cantina/ Spearbit.?Learn more how your staked funds are completely protected at Goat.Tech: ?Shoutout to the GOAT Goat.Tech co-hosts with our partners to bring you multiple campaigns and earning opportunities.We're thrilled to introduce Lido $wstETH staking on Goat Dapp MainnetPoint tracking integration are now offering $wstETH stakers with greater benefits:?Build a high Web3 social standing in form of Trust Score?1.5x Goat.Tech Mainnet Points(will be convertible to disinflationary $GOAT token)?Earn great APR yields

Aura Finance
Aura Finance
technology660

The network of applications using blockchain technology built on Fantom Opera to bring community experiences and benefitsWebsite: aurafi.orgCEO: @chrisaurafiGood new!!! Farming works perfectly now!! No error in code but developer forgot to set the Farming contract to be the "AuraManager" which can mint $AURA.You can join Farming now!!!

Tech Mukul - Xiaomi HyperOS Community ?‍??
Tech Mukul - Xiaomi HyperOS Community ?‍??
technology2.23K

Amazing ⭐️ HyperOS updates status for Poco F4 and Redmi K50iWe are here to provide news about each an every update about HyperOS Xiaomi HyperOS updates are closed for all Xiaomi devices ?Install HyperOS "AI Surprise Wallpapers" in your Xiaomi devices Install HyperOS "AI Surprise Wallpapers" in your Xiaomi devices Official Telegram Channel = @TECH_MUKUL

Kima Network
Kima Network
technology96.25K

The Decentralized Money Transfer Protocol Revolutionizing Financial Interoperability. Incubated by @ChainGPT Announcements: t.me/KimaNetworkNews? HackaTRON Season 6 Twitter Space is LIVE TODAY! ?We're thrilled to announce that Kima is joining today's Twitter Space hosted by TRON DAO. ?As a proud Platinum Sponsor of HackaTRON Season 6, we're excited to dive into discussions on emerging blockchain technologies, the latest in the Bitcoin and crypto ecosystems, and share insights into the innovative solutions alongside esteemed projects:✅Chain GPT✅AI Tech✅GT-Protocol✅Router Protocol✅Moby Media⭐️ Highlight: Our CMO, Tomer Warschauer Nuni, will be speaking at the Bitcoin Halving celebration, showcasing how Kima is shaping the future of blockchain technology.

AmpereChain - Connect the World
AmpereChain - Connect the World
technology7.35K

AmpereChain: The fastest D-QBFT blockchain with AMPERE crypto. Pioneering EVM public chain with privacy nodes. Unmatched speed, efficiency, privacy, and TPS. #DecentralizedTechDear Community,I have received lot of DM regarding vesting time of migration.Considering queries and objection we have decided to go with voting for merging decision1. Project is registered and they have shared formation, approval certificate2. There is one way(Either go with them or reject them). Once we opt for rejection we are completely out3. Our community must have to understand they are yet in presale and asking all tokens to release without reaching certain market cap will crash project. I had discussion with him for early release and he highlighted it. Many of you have intention to exit but none of project owner will allow such crash4. I asked same questions to seed funding users few days back and they opted to stay with them5. Dom and Moh already left project with no backing to support itI have clearly mentioned positive as well as negative side with our current situation.Requesting everyone to vote for next 3 days which will help us to go forward or return back to previous steps leaving them.

The Job Overflow - Recently asked Problems
The Job Overflow - Recently asked Problems
technology6.12K

The right package for interview help to switch to Product Based Companies led by Tech Professionals. We provide OAs and interview experience, and post it at thejoboverflow.com, and notify in the TGVisit the Pinned messages for our Community Guidelines ?I don't know if there is a simpler way, but I know a way of changing profile picture. TJO profile picture is linked to gravatar. 1.So go to gravatar. Com 2.Sign in with email you registered tjo with3.change profile picture at gravatar. That's all!Thanks for showing such mad excitement! We've now got some exciting news to share. The first-ever Masterclass by AlgoUniversity is all set to take place. Join us in this Resume Crafting session by none other than Manas Sir himself!Yep, you heard it right! Manas Sir is ready for spilling all his insider secrets on how he nailed his resume that made him travel to not one, not two, but three different countries ✈️. It's an exclusive opportunity to learn from the best!

This website is not affiliated with Telegram. Visual content shown here might be copyrighted by rightful owners. No infringement intended.
DISCLAIMER: Infos without tag OFFICIAL posted on website are public, and wo are not responsible for the content on their media. Join or subscribe the info there maybe some risk with you. If you have any issueContact UsPlease!