Python Tip for the day:
Use the "enumerate" function to iterate over a sequence and get the index of each element.
Sometimes when you're iterating over a list or other sequence in Python, you need to keep track of the index of the current element. One way to do this is to use a counter variable and increment it on each iteration, but this can be tedious and error-prone.
A better way to get the index of each element is to use the built-in "enumerate" function. The "enumerate" function takes an iterable (such as a list or tuple) as its argument and returns a sequence of (index, value) tuples, where "index" is the index of the current element and "value" is the value of the current element. Here's an example:
The output of this code would be:
Use the "enumerate" function to iterate over a sequence and get the index of each element.
Sometimes when you're iterating over a list or other sequence in Python, you need to keep track of the index of the current element. One way to do this is to use a counter variable and increment it on each iteration, but this can be tedious and error-prone.
A better way to get the index of each element is to use the built-in "enumerate" function. The "enumerate" function takes an iterable (such as a list or tuple) as its argument and returns a sequence of (index, value) tuples, where "index" is the index of the current element and "value" is the value of the current element. Here's an example:
#Iterate over a list of strings and print each string with its iIn this example, we use the "enumerate" function to iterate over a list of strings. On each iteration, the "enumerate" function returns a tuple containing the index of the current string and the string itself. We use tuple unpacking to assign these values to the variables "i" and "s", and then print out the index and string on a separate line.
strings = ['apple', 'banana', 'cherry', 'date']
for i, s in enumerate(strings):
print(f"{i}: {s}")
The output of this code would be:
appleUsing the "enumerate" function can make your code more concise and easier to read, especially when you need to keep track of the index of each element in a sequence.
1: banana
2: cherry
3: date
Number Guessing Game Using python..
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
import random
def play_guessing_game():
print("Welcome to the Guessing Game!")
# by @mr_sukkun
# join : https://t.me/Python_Codes_Pro
secret_number = random.randint(1, 100)
attempts = 0
while True:
guess = int(input("Enter your guess (between 1 and 100): "))
attempts += 1
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You've guessed the secret number {secret_number} in {attempts} attempts!")
break
play_guessing_game()
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
👍3❤1
Which of the following concepts is not a part of Python?
Anonymous Quiz
50%
Pointers
13%
Loops
27%
Dynamic typing
10%
All of the above
# Pastebin Using Python
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
import requests
def pastebin(content):
"""
Function to post content to a pastebin service and return the URL of the paste.
Args:
content (str): The content to be pasted
Returns:
str: The URL of the paste
"""
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0"}
data = {"snippet":content}
response = requests.post(f'https://bin.kv2.dev/', json=data, headers=headers)
yield response.url
print(next(pastebin("hi")))
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
❤2
❤1
What will be the output of the following Python code?
Anonymous Quiz
32%
a b c
19%
1 2 3
31%
0 a 1 b 2 c
18%
none
Python program that Create a function that gives True if a string is empty and False otherwise.
# - Try it for '' and ' 5gh'
# - Try it for '' and ' 5gh'
def checkstring(string):
if len(string)==0:
return True
else:
return False
print(checkstring("5gh"))
print(checkstring(""))
#Taskforyou
Write a Python program that remove the extra spaces from the following string:
' I am learning Python object oriented programming '
Write a Python program that remove the extra spaces from the following string:
' I am learning Python object oriented programming '
# KBC game using python
source code :
https://github.com/Noob-mukesh/Python-Projects/blob/main/KBCgame.py
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
source code :
https://github.com/Noob-mukesh/Python-Projects/blob/main/KBCgame.py
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
❤1👍1
🖥 Awesome Python Awesome: A curated list of awesome Python frameworks, libraries, software and resources.
🖥 Github: https://github.com/vinta/awesome-python
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
🖥 Github: https://github.com/vinta/awesome-python
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
#Doubt
How to create 80000 data of random names
Output: https://t.me/python_group_pro/914
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
How to create 80000 data of random names
#pip install faker
from faker import Faker
fake = Faker()
name = set()
while len(name) < 80000:
name.add(fake.name())
print(name)
Output: https://t.me/python_group_pro/914
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
❤3
Forwarded from ˹ᴍʀ sᴜᴋᴋᴜɴ˼ (Mukesh test bot)
This media is not supported in your browser
VIEW IN TELEGRAM
👩💻 Wᴇʟᴄᴏᴍᴇ ᴛᴏ @CodesSnippet! 🚀
Jᴏɪɴ ᴜs ғᴏʀ ᴅᴀɪʟʏ sɴɪᴘᴘᴇᴛs ᴏғ ᴄᴏᴅɪɴɢ 🧩 ᴋɴᴏᴡʟᴇᴅɢᴇ! 💻💡
Hᴇʀᴇ, ʏᴏᴜ'ʟʟ ғɪɴᴅ 👀 ʙɪᴛᴇ-sɪᴢᴇᴅ ᴘɪᴇᴄᴇs ᴏғ ᴄᴏᴅᴇ, 🔥 ᴘʀᴏɢʀᴀᴍᴍɪɴɢ ᴛɪᴘs, ᴀɴᴅ ᴛʀɪᴄᴋs ᴛᴏ ʟᴇᴠᴇʟ ᴜᴘ ʏᴏᴜʀ ᴄᴏᴅɪɴɢ sᴋɪʟʟs! 💪💻
Sᴛᴀʏ ᴜᴘᴅᴀᴛᴇᴅ ᴏɴ ᴛʜᴇ ʟᴀᴛᴇsᴛ ᴛʀᴇɴᴅs ɪɴ ᴛʜᴇ ᴛᴇᴄʜ ᴡᴏʀʟᴅ ᴀɴᴅ ᴇɴɢᴀɢᴇ ᴡɪᴛʜ ғᴇʟʟᴏᴡ ᴄᴏᴅᴇʀs 🤝 ɪɴ ᴏᴜʀ ᴠɪʙʀᴀɴᴛ ᴄᴏᴍᴍᴜɴɪᴛʏ! 🌐💬
Lᴇᴛ's ᴄʀᴀᴄᴋ ᴛʜᴇ ᴄᴏᴅɪɴɢ 🚪 ᴛᴏɢᴇᴛʜᴇʀ ᴀɴᴅ ʙʀɪɴɢ ʏᴏᴜʀ ᴘʀᴏJᴇᴄᴛs ᴛᴏ ʟɪғᴇ!
#CodeSnippet
#ProgrammingFun
Jᴏɪɴ ᴜs :- @CodesSnippet
Jᴏɪɴ ᴜs ғᴏʀ ᴅᴀɪʟʏ sɴɪᴘᴘᴇᴛs ᴏғ ᴄᴏᴅɪɴɢ 🧩 ᴋɴᴏᴡʟᴇᴅɢᴇ! 💻💡
Hᴇʀᴇ, ʏᴏᴜ'ʟʟ ғɪɴᴅ 👀 ʙɪᴛᴇ-sɪᴢᴇᴅ ᴘɪᴇᴄᴇs ᴏғ ᴄᴏᴅᴇ, 🔥 ᴘʀᴏɢʀᴀᴍᴍɪɴɢ ᴛɪᴘs, ᴀɴᴅ ᴛʀɪᴄᴋs ᴛᴏ ʟᴇᴠᴇʟ ᴜᴘ ʏᴏᴜʀ ᴄᴏᴅɪɴɢ sᴋɪʟʟs! 💪💻
Sᴛᴀʏ ᴜᴘᴅᴀᴛᴇᴅ ᴏɴ ᴛʜᴇ ʟᴀᴛᴇsᴛ ᴛʀᴇɴᴅs ɪɴ ᴛʜᴇ ᴛᴇᴄʜ ᴡᴏʀʟᴅ ᴀɴᴅ ᴇɴɢᴀɢᴇ ᴡɪᴛʜ ғᴇʟʟᴏᴡ ᴄᴏᴅᴇʀs 🤝 ɪɴ ᴏᴜʀ ᴠɪʙʀᴀɴᴛ ᴄᴏᴍᴍᴜɴɪᴛʏ! 🌐💬
Lᴇᴛ's ᴄʀᴀᴄᴋ ᴛʜᴇ ᴄᴏᴅɪɴɢ 🚪 ᴛᴏɢᴇᴛʜᴇʀ ᴀɴᴅ ʙʀɪɴɢ ʏᴏᴜʀ ᴘʀᴏJᴇᴄᴛs ᴛᴏ ʟɪғᴇ!
#CodeSnippet
#ProgrammingFun
Jᴏɪɴ ᴜs :- @CodesSnippet
❤1
Forwarded from Cᴏᴅᴇs Sɴɪᴘᴘᴇᴛs (s ʜ ɪ ɴ ᴇ)
Pattern Challenge..
>> Approx Time:
Jᴏɪɴ ᴜs :- @CodesSnippet
# Hollow Rectangle of '*'
def hollow_pattern(row, col):
for i in range(row):
for j in range(col):
if i == 0 or i == row-1 or j == 0 or j == col-1:
print('* ', end='')
else:
print(' ', end='')
print('')
>> Approx Time:
6.67msJᴏɪɴ ᴜs :- @CodesSnippet
_1_Introduction_To_Data_Structurepdf_lyst1084.pdf
6.1 MB
_1_Introduction_To_Data_Structurepdf_lyst1084.pdf
#Dsa basics pdf
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
#Dsa basics pdf
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
Scrap Image from google using BeautifulSoup
#CodeSnippet
#ProgrammingFun
Language:
Join :- @Python_Codes_Pro
Support group :- @python_group_pro
import requests
from bs4 import BeautifulSoup as BSP
def get_image_urls(search_query):
url = f"https://www.google.com/search?q={search_query}&tbm=isch"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
rss = requests.get(url, headers=headers)
soup = BSP(rss.content, "html.parser")
all_img = []
for img in soup.find_all('img'):
src = img['src']
if not src.endswith("gif"):
all_img.append(src)
return all_img
print(get_image_urls("boy"))
#CodeSnippet
#ProgrammingFun
Language:
PythonJoin :- @Python_Codes_Pro
Support group :- @python_group_pro
Forwarded from ˹ᴍʀ sᴜᴋᴋᴜɴ˼ (Mᴜᴋᴇsʜ)
Search hd image from unsplash using mukesh api
#CodeSnippet
#ProgrammingFun
Language:
Jᴏɪɴ ᴜs :- @CodesSnippet
import requests
ask = input("query to search image:- ")
class Unsplash:
base_url = "https://mukesh-api.vercel.app/"
"""search image from unsplash api by @mr_sukkun"""
def __init__(self, ask) -> None:
self.ask = ask
def unsplash_reply(self):
url = f"{self.base_url}unsplash?query={self.ask}"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return {"error": "Failed to fetch data"}
x = Unsplash(ask)
print(x.unsplash_reply())
#CodeSnippet
#ProgrammingFun
Language:
PythonJᴏɪɴ ᴜs :- @CodesSnippet
❤2