✨ graphical user interface (GUI) | Python Glossary ✨
📖 A visual way of interacting with a program through windows, buttons, and other on-screen elements.
🏷️ #Python
📖 A visual way of interacting with a program through windows, buttons, and other on-screen elements.
🏷️ #Python
❤2
✨ Gift Cards ✨
📖 Give the Gift of Real Python with a membership gift card. An easy way to give joy to the Pythonistas in your life.
🏷️ #Python
📖 Give the Gift of Real Python with a membership gift card. An easy way to give joy to the Pythonistas in your life.
🏷️ #Python
❤1
✨ Lovable | AI Coding Tools ✨
📖 An AI-powered full-stack platform that generates and deploys web applications from natural language descriptions.
🏷️ #Python
📖 An AI-powered full-stack platform that generates and deploys web applications from natural language descriptions.
🏷️ #Python
❤1
A bit of Python basics. Day 6 - Exchanging variable values
In Python, you can swap variables after they have already been assigned objects. Below, we first assign
You can also use the XOR (exclusive or) operator to swap variables. This is a three-step method. In the example below, we swap the values of
You can also use arithmetic operations (addition and subtraction) to swap variables without a temporary variable. However, this method is recommended for swapping numeric data types. Here's an example:
As a result of these arithmetic operations, the values of
This method of swapping variables without a temporary variable is based on the fact that when you add or subtract the value of one variable from another, you can effectively swap their values without the need for additional storage.
👉 https://t.iss.one/DataScience4
In Python, you can swap variables after they have already been assigned objects. Below, we first assign
20 to the variable x and 30 to the variable y, and then swap them: x becomes 30, and y becomes 20. This method is called tuple packing/unpacking.x, y = 20, 30
x, y = y, x
print('x is: ', x)
print('y is: ', y)
x is 30
y is 20You can also use the XOR (exclusive or) operator to swap variables. This is a three-step method. In the example below, we swap the values of
x and y.x = 20
y = 30
# step one
x ^= y
# step two
y ^= x
# step three
x ^= y
print(f'x is: {x}')
print(f'y is: {y}')
x is: 30
y is: 20You can also use arithmetic operations (addition and subtraction) to swap variables without a temporary variable. However, this method is recommended for swapping numeric data types. Here's an example:
# Use arithmetic operations
x = 5
y = 10
x = x + y
y = x - y
x = x - y
print("After swapping:")
print("x =", x)
print("y =", y)
After swapping:
x = 10
y = 5As a result of these arithmetic operations, the values of
x and y have actually been swapped. After the swap, x contains the original value of y (10), and y contains the original value of x (5).This method of swapping variables without a temporary variable is based on the fact that when you add or subtract the value of one variable from another, you can effectively swap their values without the need for additional storage.
Please open Telegram to view this post
VIEW IN TELEGRAM
Telegram
Code With Python
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Admin: @HusseinSheikho || @Hussein_Sheikho
❤4
This channels is for Programmers, Coders, Software Engineers.
0️⃣ Python
1️⃣ Data Science
2️⃣ Machine Learning
3️⃣ Data Visualization
4️⃣ Artificial Intelligence
5️⃣ Data Analysis
6️⃣ Statistics
7️⃣ Deep Learning
8️⃣ programming Languages
✅ https://t.iss.one/addlist/8_rRW2scgfRhOTc0
✅ https://t.iss.one/Codeprogrammer
Please open Telegram to view this post
VIEW IN TELEGRAM
❤2
✨ OpenCode | AI Coding Tools ✨
📖 An open-source terminal AI coding agent with support for over 75 AI models and IDE integrations.
🏷️ #Python
📖 An open-source terminal AI coding agent with support for over 75 AI models and IDE integrations.
🏷️ #Python
✨ Quiz: Dependency Management With Python Poetry ✨
📖 Test your knowledge of Python Poetry, from installation and virtual environments to lock files, dependency groups, and updates.
🏷️ #intermediate #best-practices #devops #tools
📖 Test your knowledge of Python Poetry, from installation and virtual environments to lock files, dependency groups, and updates.
🏷️ #intermediate #best-practices #devops #tools
A bit of Python basics. Day 7. Counting the number of occurrences of an element
If you need to find out how many times an element appears in an iterable collection, you can use the
Output:
Another way to do this is with a regular
Output:
Lists and other iterable data structures in Python have a built-in
Output:
👉 https://t.iss.one/DataScience4
If you need to find out how many times an element appears in an iterable collection, you can use the
Counter class from the collections module. Counter() returns a dictionary with the number of times each element appears in the sequence. Let's say we want to find out how many times the name Peter appears in the following list. We can use Counter(). See below:from collections import Counter
list1 = ['John', 'Kelly', 'Peter', 'Moses', 'Peter']
count_peter = Counter(list1).get("Peter")
print(f'The name "Peter" appears in the list '
f'{count_peter} times.')
Output:
The name "Peter" appears in the list 2 times.Another way to do this is with a regular
for loop. We create a count variable and increase it by 1 each time we find the name Peter in the sequence. This is a naive approach. See below:list1 = ['John', 'Kelly', 'Peter', 'Moses', 'Peter']
# Create a count variable
count = 0
for name in list1:
if name == 'Peter':
count +=1
print(f'The name "Peter" appears in the list'
f' {count} times.')
Output:
The name "Peter" appears in the list 2 times.Lists and other iterable data structures in Python have a built-in
count() method, which allows us to count the number of occurrences of a specific element. We can use count() to count how many times Peter appears in the list.list1 = ['John', 'Kelly', 'Peter', 'Moses', 'Peter']
print(f'The name "Peter" appears in the list '
f'{list1.count("Peter")} times.')
Output:
The name "Peter" appears in the list 2 times.Please open Telegram to view this post
VIEW IN TELEGRAM
Telegram
Code With Python
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Admin: @HusseinSheikho || @Hussein_Sheikho
👍3
⚡️ Python code that works, but does extra work 100 times over
This Python code looks normal.
It works.
It passes the tests.
But it does extra work dozens, and sometimes hundreds of times.
The most common reason is that you accidentally turn a linear algorithm into a quadratic one.
A typical scenario:
- there's a list
- inside the loop, you repeatedly do in, count, index
- everything works quickly with small data
- on real data, the application starts to "slow down for no reason"
The problem is that:
- list is O(n) for searching
- searching inside the loop = O(n²)
- Python honestly does the work you asked it to do
Pros don't think about "whether it works or not", but how many extra operations are being performed.
The correct approach:
- if you need membership checks, use set
- if you're counting elements, use dict or Counter
- if the data doesn't change, pre-calculate it once
This technique is one of the most common sources of hidden performance bugs in Python code.
This Python code looks normal.
It works.
It passes the tests.
But it does extra work dozens, and sometimes hundreds of times.
The most common reason is that you accidentally turn a linear algorithm into a quadratic one.
A typical scenario:
- there's a list
- inside the loop, you repeatedly do in, count, index
- everything works quickly with small data
- on real data, the application starts to "slow down for no reason"
The problem is that:
- list is O(n) for searching
- searching inside the loop = O(n²)
- Python honestly does the work you asked it to do
Pros don't think about "whether it works or not", but how many extra operations are being performed.
The correct approach:
- if you need membership checks, use set
- if you're counting elements, use dict or Counter
- if the data doesn't change, pre-calculate it once
This technique is one of the most common sources of hidden performance bugs in Python code.
# ❌ Bad: O(n²)
users = ["alice", "bob", "carol", "dave"]
for u in users:
if u in users: # full list traversal every time
process(u)
# ✅ Good: O(n)
users = ["alice", "bob", "carol", "dave"]
users_set = set(users)
for u in users:
if u in users_set:
process(u)
❤3
❤3
Forwarded from Machine Learning with Python
reversed() in Python - what supports it and what doesn'tThe function
reversed() is built-in in Python, but it doesn't work with all data types✓ Lists - it works
reversed([1, 2, 3]) returns an iteratorlist(reversed([1, 2, 3])) → [3, 2, 1]✓ Tuples - it also works
reversed((1, 2, 3)) can be easily iterated✗ Sets - not supported
reversed({1, 2, 3}) → TypeErrorWhy? Sets don't have a fixed order, so they can't be "reversed"
If you need to reverse a set:
list(reversed(list({1, 2, 3})))A bit of #Python basics. Day 8 - Flatten a nested list
I'll show you three (3) ways to flatten a two-dimensional list. The first method uses a
⚙️ Using a
For this method, we use a nested
⚙️ Using the itertools module:
The
You can see that the nested loop has been flattened.
⚙️ Using list comprehension
If you don't want to import
List comprehension is well suited for moderately nested lists. For deeply nested lists, it is not suitable, as the code becomes harder to read.
⚙️ Using a generator function
You can create a generator function that yields elements from the nested list, and then convert the generator into a list.
The generator method is suitable for flattening large or deeply nested lists. This is because generators are memory-efficient.
👉 https://t.iss.one/DataScience4
I'll show you three (3) ways to flatten a two-dimensional list. The first method uses a
for loop, the second uses the itertools module, and the third uses list comprehension.for loop:For this method, we use a nested
for loop. The outer loop iterates over the inner lists, and the inner loop accesses the elements in the inner lists.# In [19]:
list1 = [[1, 2, 3],[4, 5, 6]]
newlist = []
for list2 in list1:
for j in list2:
newlist.append(j)
print(newlist)[1, 2, 3, 4, 5, 6]The
itertools.chain.from_iterable() function from the itertools module can be used to flatten a nested list. This method may not be suitable for deeply nested lists.# In [20]:
import itertools
list1 = [[1, 2, 3],[4, 5, 6]]
flat_list = list(itertools.chain.from_iterable(list1))
print(flat_list)[1, 2, 3, 4, 5, 6]You can see that the nested loop has been flattened.
If you don't want to import
itertools or write a regular for loop, you can simply use list comprehension.# In [21]:
list1 = [[1, 2, 3], [4, 5, 6]]
flat_list = [i for j in list1 for i in j]
print(flat_list)[1, 2, 3, 4, 5, 6]List comprehension is well suited for moderately nested lists. For deeply nested lists, it is not suitable, as the code becomes harder to read.
You can create a generator function that yields elements from the nested list, and then convert the generator into a list.
# In [22]:
def flatten_generator(nested_list):
for sublist in nested_list:
for item in sublist:
yield item
list1 = [[1, 2, 3], [4, 5, 6]]
flat_list = list(flatten_generator(list1))
flat_list
Out[22]: [1, 2, 3, 4, 5, 6]The generator method is suitable for flattening large or deeply nested lists. This is because generators are memory-efficient.
Please open Telegram to view this post
VIEW IN TELEGRAM
Telegram
Code With Python
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Admin: @HusseinSheikho || @Hussein_Sheikho
❤4