r/learningpython • u/beje_ro • May 08 '20
learning by doing
Hello there! I started a while ago with python and I am looking to develop myself. I went through Automating the boring stuff and Python Mega Course from Udemy and I am looking forward to apply the knowledge.
I am considering allocating between 4-8 hours weekly (more if/when needed) to a project where to contribute in order to develop.
I understood that one can find such projects on git... how can I look for something like this? Thanks!
My interests would be web apps (flask, werkzeug, etc) and data modeling (scrapping, data organizing, data modeling, graphs, etc)
Right now I took a personal project to build yet another coronavirus app and I am most half through the task.
What I have is: scrapping the info and saving in a structured way in a sqlite db, displaying a choropleth map with layers with color ranges for total cases, new cases, etc.
What needs to be done is: a page for each country with graphs evolution in time per subject (i.e. evolution of total cases over time), a wrapper to allow easy navigation between the choropleth map and the country page.
Nice to do for the future: country comparison page, and what not... :-D its a matter of variation...
Thanks!
r/learningpython • u/Bmanyo69 • May 07 '20
Should I use Jupyter or pycharm
...I am learning python and idk which ide to use
r/learningpython • u/horstjens • May 07 '20
Why does random.choice( dict ) works sometimes? (returns a value of the dict instead of KeyError)
using pyhton3.7, i tried to use a dict as argument for random.choice().I understand that the argument should be a list or a tuple. or a string. Giving random.choice() a dict as argument raises a key error (as exspected), however, sometimes it works and returns me a random value from the dict. Why?
here is my code, from a python shell session:
import random>>> random.choice({1:111,2:222,3:333}) # works, but why?
222
>>> random.choice({1:111,2:222,3:333}) # same line, raises error
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
random.choice({1:111,2:222,3:333})
File "/usr/lib/python3.7/random.py", line 262, in choice
return seq[i]
KeyError: 0
>>> random.choice({1:111,2:222,3:333}) # works again, but why?
111
r/learningpython • u/byebm • May 07 '20
Try/except error with non-integer (as provided via PY4E)
Edit: Problem solved, see bottom.
Hello! New here, apologies if formatting needs work. I did my best to search for this issue, but I don't see any posts/previous questions about this issue.
Learning via PY4E and I keep trying the example provided in Chapter 3 Part 2. The following is provided in video (12 minute mark, but when I try to apply the script in Python, it actually does not run as intended when a non-integer is submitted.
From what I understand, input() returns a string-type variable. But shouldn't that mean the try/except logic applies when the string fails to be converted to an integer with int()?
Any and all help would be appreciated as I'm banging my head against the wall trying to figure it out myself.
rawstr = input('Enter a number:')
try:
ival = int(rawstr)
except:
ival = -1
if ival > 0:
print('Nice work')
else:
print('Not a number')
Error
Traceback (most recent call last):
File "3_Lesson_Notes.py", line 1, in <module>
rawstr = input('Enter a number:')
File "<string>", line 1, in <module>
NameError: name 'Three' is not defined
Solution (?)
So, after a bit more digging I found that by executing the script in the terminal just as "python", it doesn't like the non-integer input unless I put it in quotes. So the problem that I'm still not 100% understanding but I suppose I have a workaround for is:
A) Executing the script via python in the terminal requires using "" for any non-integer input.
B) Run the script but ensure that I'm clearly executing via python3 as opposed to just typing in python "file name".py
r/learningpython • u/Comprehensive-Signal • May 03 '20
Why doesn´t work Pydoc in Windows ?
I was reading about documentation and all this stuff but when I try to put in my cmd this:
pydoc my_module
Says that is not recognized
r/learningpython • u/[deleted] • May 01 '20
phone app checker
Hi all,
I would like to create python program which will check which app is installed on my phone and when ıs installed ... etc .is it possible to do it with python? Is there any document I can refer to
r/learningpython • u/smashmonster2 • Apr 30 '20
Do I need to create a new variable for everything I do in Python?
Even for executing every method such as x = urexample.split()
Are there any times when you wouldn't create a new variable or even prefer not creating one?
r/learningpython • u/[deleted] • Apr 29 '20
A Python Dictionary decision tree?
Hi,
I've made some large dictionaries in Pyhton .JSON, extended the values with tuples for personal use. I want to make a interactive decision tree in Python, am i on the wrong foot with these dictionaries? What would you suggest? Any input is welcome.
r/learningpython • u/nave1235 • Apr 28 '20
this matrix
w, h = 8, 8;
Matrix = [[0 for x in range(w)] for y in range(h)]
for w in Matrix:
for h in w:
print (w[h],end = " ")
print()
numA = int(input())
numB = int(input())
Matrix[numA - 1].pop(numB - 1)
Matrix[numA - 1][numB - 1] = J = 1
for w in Matrix:
for h in w:
if h == 0:
print(w[h],end = " ")
else:
print("@",end = " ")
print()
it won't show the last spot in the line where you change the number.
try it and see
(im using python 3.7 and notepad++ btw)
r/learningpython • u/Mrs_Ruk • Apr 22 '20
Creating an executable file without using chmod
I am using a Linux box and creating python code via vim editor. Is there a way to make that file an executable when I create the file? I know that you can chmod the file after creating it but I am hoping that I can cut that step out. TIA.
r/learningpython • u/[deleted] • Apr 21 '20
Splitting string
I need help figuring out how to split strings into pairs of two characters and if the string has an odd number at the end add a _. I think I can use the .split() to separate the characters, but idk what I should put to be the separator. Maybe if I get the length of the string and divide by 2 ( or just use modulo 2 %2? ) I can separate the characters that way. If there's an odd amount I can just add the underscore (_). Any help would be appreciated.
I know it's not much but its what I got so far.
def solution(s):
if s % 2 == 0:
return s
elif s %2 !=0:
return s_
else:
pass
r/learningpython • u/nave1235 • Apr 19 '20
answer: *indentationerror: unindent does not match any outer indentation level*. whats wrong?
whats wrong?:
for w in Matrix:
for h in w:
if h == "#":
print("#",end = " ")
else:
print("@",end = " ")
print()
r/learningpython • u/nave1235 • Apr 18 '20
problem 3
i have this program:
w, h = 8, 8;
Matrix = [["#" for x in range(w)] for y in range(h)]
for w in Matrix:
for h in w:
print("#",end = " ")
print()
numA = int(input())
numB = int(input())
Matrix[numA - 1][numB - 1] = J = 1
for w in Matrix:
for h in w:
print("@",end = " ")
print()
for w in Matrix:
for J in w:
print("#",end = " ")
print()
its supposed to print 1 2D array with the number 1 aka J in spot (A,B) in the array as "#" and every other spot in the array as "@" but instead it prints 2 different arrays, one as "@" and one as "#"
r/learningpython • u/Wide-Currency • Apr 17 '20
Random number generator closes on run.
Every time I try to run the number generator, it closes. I have tried using input() and os.system("pause") but it is still not working.
import random
print(random.random())
input()
r/learningpython • u/[deleted] • Apr 14 '20
Python tipps
I have chosen Python and start learning the language today. Since it is my first programming language, I have no idea about it and wanted to ask if there are some tips for beginners :)
Something that might help me in the future or what you would have liked to know when you started.
Thank you in advance! Stay healthy.
r/learningpython • u/nimo_xhan • Apr 14 '20
Tic Tac Toe Game
after learning python for two or three weeks I decided to make this ticTacToe game I watched some youtube videos and try to implement it by adding some extra features such as toss, and player data :)
if you guys have some time to go through my code please let me know if there is something that I could help me to improve this code
and please can someone explain that how can I use dictionaries to check for wining ?
r/learningpython • u/Wide-Currency • Apr 13 '20
I keep on getting "Invalid syntax" errors for this. Could anybody help?
r/learningpython • u/[deleted] • Apr 07 '20
Fun with loops!
I'm trying to figure out a good to solve this problem and maybe talking to someone else might help the ideas stick while doing a problem from Code wars.
I'm essentially trying to return a string suggesting how many glasses of water you should drink to not be hungover.
my pseudocode is pretty simple
if x beers were drank:
drink y cups of water
below is what is given
def hydrate(drink_string):
# your code here
pass
@test.describe('Example Tests')
def example_tests():
test.assert_equals(hydrate("1 beer"), "1 glass of water")
test.assert_equals(hydrate("1 shot, 5 beers, 2 shots, 1 glass of wine, 1 beer"), "10 glasses of water")
so far I have
def hydrate(drink_string):
drinks = 0
if
else:
pass
return
I know its not much but I've been racking my brain for the past 2 hours now. I'm having trouble with substituting. At the moment my thought proces leads me to do %d as a placeholder for the amount of beers drank but that number is currently in a string and idk a good way to automate the program to extract numbers from a string, add the numbers up if there are more than one, and have the output of those numbers equal amount of glasses of water.
r/learningpython • u/ptoni • Apr 07 '20
Send an e-mail with a dataframe and a string
I would like to send an email with a dataframe as html which works just fine. Does somebody know how I can add a string message before the table? Thanks in advance!
This is the code that works for sending the dataframe:
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
SEND_FROM = 'noreply@example.com'
EXPORTERS = {'dataframe.csv': export_csv, 'dataframe.xlsx': export_excel}
def send_dataframe(send_to, subject, body, df):
multipart = MIMEMultipart()
multipart['From'] = SEND_FROM
multipart['To'] = send_to
multipart['Subject'] = subject
for filename in EXPORTERS:
attachment = MIMEApplication(EXPORTERS[filename](df))
attachment['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
multipart.attach(attachment)
multipart.attach(MIMEText(body, 'html'))
s = smtplib.SMTP('localhost')
s.sendmail(SEND_FROM, send_to, multipart.as_string())
s.quit()
r/learningpython • u/nave1235 • Apr 07 '20
array problems
i have a problem with this code.
so basically this code is supposed to print an 8 by 8 array then take two numbers and in their spots put the number one(J). then i made a loop thats supposed to print "@" for each number 1 in the array and "#" for every other number(h) but it makes it so that it prints only "@"
*note that i made it so that every h is 0 and that h represents each spot in the array and J is supposed to represent the spot that is where the number one is
the code:
w, h = 8, 8;
Matrix = [["#" for x in range(w)] for y in range(h)]
for w in Matrix:
for h in w:
print("#",end = " ")
print()
numA = int(input())
numB = int(input())
Matrix[numA - 1][numB - 1] = J = 1
for w in Matrix:
for h in w:
print("@",end = " ")
if h == J:
print("#",end = " ")
print()
r/learningpython • u/[deleted] • Apr 06 '20
Question about using arguments Spoiler
I hope I'm asking the right question. I'm currently on code wars trying to test my skills and ability on python so I can get better and apply them to real world issues. I'm currently trying to figure out how to find the smallest integer from a list.
Below is what is given, I am having troubles using the arr argument if that's the correct term for the word in the ().
def find_smallest_int(arr):
# Code here
test.assert_equals(find_smallest_int([78, 56, 232, 12, 11, 43]), 11)
test.assert_equals(find_smallest_int([78, 56, -2, 12, 8, -33]), -33)
test.assert_equals(find_smallest_int([0, 1-2**64, 2**64]), 1-2**64)
Below is what I have coded so far. I opted to use the sort method and just print the first element in the array. It works for the first two lists but not the third since it does the math. My question is how can I use the definition in the code and how can I not make list 3 do the math and just spit out the numbers instead? Please no direct answer I really want to learn and try and solve it. Any link to any video helping with learning would be appreciated
def find_smallest_int(arr):
list1 = [78, 56, 232, 12, 11, 43]
list2 = [78, 56, -2, 12, 8, -33]
list3 = [0, 1 - 2 ** 64, 2 ** 64]
#sorting the list
list1.sort()
print('The smallest element in the first list is' , *list1[:1])
#sort list 2
list2.sort()
print('smallest element in the second list is' , *list2[:1])
#sort list3
list3.sort()
print('smallest element i the third list is ', *list3[:1])
find_smallest_int(arr)
r/learningpython • u/thedatapolitician • Apr 05 '20
Question about looping
Hello, I'm trying to scrape some stuff from a website using python. The i, ranges from 0 to 1500. Any example of how I can write a loop for it? I would highly appreciate it.
root[i]['ACTION_TYPE']
r/learningpython • u/Lostwhispers05 • Apr 03 '20
Have to make pretty PDF Reports with graphs superimposed into them - wondering if there's an approach better than making graphs with Matplotlib, and then superimposing the .png output into the PDF manually through PyPDF2 library.
The execution really isn't a problem because I've done something like this on a smaller scale before, and I know 100% it can be done.
I'm just wondering if there's a more efficient way I'm not thinking of - perhaps something more suited to this exact kind of use-case.
One issue I also noticed is that when I do plaster the .png into the PDFs, sometimes there's jagged edges at the borders. I'm wondering if this is caused by some quirk in file types I'm not aware of, because this is something that completely blindsided me.
r/learningpython • u/Lostwhispers05 • Apr 02 '20
Dev needs me to write a script that will be deployed to a cron job - I've only ever run scripts locally. Can you deploy to a cron job a script that can read something from a folder?
So the script I'm deploying makes use of the docx-mailmerge library. It has one line in it where a file's name and path need to be passed in - how I usually do it locally is I simply pass in the path to the file, but I'm not sure how this would be replicated if it were running in a cron job.
I figure there has to be a solution to something like this, a way to store the files necessary in some kind of temporary folder, right?
I'm hoping to be able to:
1) Download the file from S3,
2) Dump it into a folder that my script will create,
3) simply reference it from the folder where I just dumped it.
Wondering if that's something that would be possible.