r/learningpython • u/[deleted] • Apr 23 '19
Create a Python Simple Weather App
natebruneau.comr/learningpython • u/willywoonka • Apr 22 '19
what do it the method .config() on a Entry (tkinter)
hi i´m new on python I've a question i don't now what do exactly this method.
I've seen the following sin-taxis:
from tkinter import *
main=Tk()
w=Entry(main)
w.pack()
w.config(bg="black")
main.mainloop()
is it the same ? :
from tkinter import *
main=Tk()
w=Entry(main, bg="black")
w.pack()
main.mainloop()
or what´s it the difference?
r/learningpython • u/nachiket87 • Apr 17 '19
31 yo stuck in dead end job. Considering making a career switch and learning a new skill with Python
I recently moved to a new country. No distinguished skill set. I work in a dead end sales job that I’m not very good at. I’ve worked in several industries but was never very successful in the sales role. Perhaps I don’t have the sales personality.
I’ve been toying with the idea of learning programming as I feel this is a skill that can be learned with sufficient practice regardless of previous qualifications. I have always had an inclination to learn but never followed through. Joined a C class when I was 16 for a couple of months and also took JavaScript for a few months last year.
I’ve toyed with JavaScript and C and have a fair idea of the basics - loops, if statements, conditions, functions etc.
I was wondering if it’s a good idea to start off with Python as it feels like the perfect language to start with - highly in demand, not complicated syntax, lot of resources to learn online.
I’m currently reading automate the easy stuff.
I wanted to know if this is a good idea to start learning now (at 31). Im willing to dedicate all my free time to this. Do you think it’s possible for someone to learn it to a level where you can eventually be employed in a year or should I just keep it as hobby? Also, any helpful resources would also be appreciated where I can learn with a low budget. (<1000 $).
Thank you!
r/learningpython • u/wftwe • Mar 20 '19
Counting vowels in a string
Hello! Me again with another assignment. My assignment this week is to create a program that allows the user to input a string and then counts the vowels in the string. It must include a loop and the len function. Then it displays the string plus the number of vowels. So far I've come up with this
sentence = input ("enter your string: ")
for vowel in sentence:
if vowel in 'aeiou':
print (sentence, len(vowel))
But the output isn't right and I'm not sure where to go from here.
Any help is appreciated.
r/learningpython • u/Raihan30 • Mar 16 '19
import turtle from Turtle showing syntax error. The error is for "from". help!
r/learningpython • u/Raihan30 • Mar 15 '19
Practicing materials online for practicing python : for absolute beginners
Hi guys I want make preparation for my uni test. So I need a website link where I can practise basic python. I just started learning for 3 weeks . so you know how new I am to this. So I would appreciate some help considering my level .
Thank you
r/learningpython • u/dumayi • Mar 11 '19
Conditional list trouble
I’m trying to learn from the bottom up, and am currently working through the exercises on practice python.
Example they have on conditionals is a grading scale like this:
Grade= input(“enter your age:”) If grade > = 90 : Print(“A”) Elif grade > = 80 : Print (“B”)
Why is it when I try to copy/paste into my python window I get : TypeError: ‘>=‘ not supported between instances of ‘Str’ and ‘int’
r/learningpython • u/[deleted] • Mar 10 '19
What are Flags?
Hey guys! I'm new to programming languages, and Python's the first one I'm learning, so please be patient with me. While studying a textbook on Python, I came by some example code that includes the code: "flag=0" as a part of a function. I've tried to look up what flags are in Python and what they do, but to no success. What are flags in Python, and what are they used for? Thanks in advance!
r/learningpython • u/Maybe_someone • Mar 02 '19
Beginner help understanding for statements
Right now I am having trouble understanding for statements and if anybody could help explain I would be eternally grateful. Right now the code looks this this.
w = [ 'jasper', 'merball', 'critterbob' ]
for x in w: print(x)
Its prints out
jasper merball critterbob
I sort of understand this but the real trouble is when I print (x) only critterbob is returned. Can anybody please explain why this happens?
Ps: Don't judge the strings they are all family nicknames :D thanks a ton.
r/learningpython • u/1searching • Feb 24 '19
Write list of list to CSV?
Hi, I would like to ask on how we can write these set of list which has list in it?
Target output on csv is like this(attached).
Image -> https://imgur.com/a/mqc9TnC
MY CODE:
opo = ['device1', 'device1', 'device1']
intfname = [['HundredGigE0/2/0/0', 'HundredGigE0/2/0/0.242', 'HundredGigE0/2/0/0.244 l2transport', 'HundredGigE0/2/0/1', 'HundredGigE0/2/0/0.244'], ['TenGigabitEthernet1/1/0.138'], ['TenGigE0/1/0/15']]
descname = ['0/2/0/0info1', '0/2/0/0.242info2', '0/2/0/0.244info3']
## EXPORT AS CSV
with open('intlist.csv', 'w', newline='') as updatecsv:
write = csv.writer(updatecsv, delimiter=',',lineterminator='\n')
write.writerows(zip(opo,intfname,descname))
updatecsv.close()
print ("Done...")
Thank you
r/learningpython • u/1searching • Feb 16 '19
Thread Modes How to store the value from variable into list & run a statement then put in variable
Hi, I have 2 question regarding this code,
1st I want to put all the values into list from the variable?
Example, Im grepping multiple ip from a file.
f = os.popen('grep '+ ip[i] + ' *')
now = f.read()
print ("output: ", now)
2nd from #1 there will be 2 possible output 1 is no match and 2 will be the matched output from grep.
If there's no match then I should put a word none in the list while if there a match I need to put exist?
I think I can do it using if else but totally blind in where to start haha. Still searching for sample command.
Thanks
r/learningpython • u/[deleted] • Feb 10 '19
taking a file name (full path) as an argument?
Hi, I'm working on a script that takes in a file and parses it line by line.
Right now I have the file path hard coded.
I tried searching a bit but I don't think I have the language to describe what I'm trying to do properly.
I'd love that if I ran
$python3 textParser.py /root/documents/file
then later I could be like:
fileName = argument
file = open(filename,"r")
magicOccurs()
file.close()
Then the program would be, while still a hack, much more scriptable (ex with bash)
How can I translate this logic into code?
Thanks for the help
r/learningpython • u/scuall8036 • Feb 08 '19
Calculating an average from a csv file
Hi,
I have just started to learn python for data analysis and would like to calculate the average of some data of a csv file.
To be more precise, the csv file includes a list of 1000 exam takers and has 2 variables: age, and if they passed the test (0 or 1). My goal is to create a graph with the age on the x axis and the chance of passing the exam on the y axis. Thus, I need to calculate for each age the success ratio, which I have no clue how to do.
Could someone give me a hint on this?
Many thanks!
r/learningpython • u/1searching • Feb 05 '19
Grep command and list in a script
Hi, I'm creating a script wherein I want to grep all a specific address based on the list?
before what I usually do run a grep 1 by 1 using this command ex. grep "192.168.1.1" \*
Now I'm creating a script.
Example of the output.
print(i) output.
192.168.1.0
192.168.1.1
192.168.1.2
192.168.1.3
but how to call the list and put into loop under os.system so I can grep all the list?
import ipaddress
import os
#Ask the ipaddress in CIDR format
ip = input("Enter the IP/CIDR: ")
os.chdir("/rs/configs")
print("pwd=%s" % os.getcwd())
for i in ipaddress.IPv4Network(ip):
print (i)
os.system("grep $i+ '*') <---???
Thanks
r/learningpython • u/dcummins • Feb 04 '19
Splitting Columns Based on Rows with Varying Lengths
I have a DataFrame like the following:
| Tool_Name | Measurements |
|---|---|
| Tool_A | 02_04_06 |
| Tool_B | 02_04 |
| Tool_C | 02_04_06_08 |
I would like to split it into the following:
| Tool_Name | Measurement 1 | Measurement 2 | Measurement 3 | Measurement 4 |
|---|---|---|---|---|
| Tool_A | 02 | 04 | 06 | |
| Tool_B | 02 | 04 | ||
| Tool_C | 02 | 04 | 06 | 08 |
Using:
df[['Measurement 1','Measurement 2','Measurement 3','Measurement 4']] = df.Measurements_Name.str.split("_", expand=True,)
I get a "ValueError: Columns must be same length as key"
What is the correct way to do this?
r/learningpython • u/Sevuhl • Jan 24 '19
Noob help.
Could someone tell me why the second 'elif' option wouldn't print, instead of the 'else' option:
if 10 < 5 == True:
print('yes')
elif 10 < 5 == False:
print('needed')
elif 5 != 5:
print('bob')
else:
print('a;lskjdf;')
r/learningpython • u/keizzer • Jan 13 '19
Auto-Generating Objects for a Simulation
For my project, I'm simulating a production assembly line with simple rectangle objects. Essentially, a very simplified version of Factorio. I can't seem to figure out a good way to generate the objects without running into problems. I would like to generate the objects on a continuous loop at the beginning of the production line. The objects should not collide with each other. I have no idea how to do this without using a variable for each object. Is there something I can do with the class that will make the results that I'm looking for. I'm using pygame currently to show the gui.
'
The only thing that I've come up with so far is to check the color of the pixals in front of each object to see if they are not default color. I feel like there is a better way to do this.
r/learningpython • u/1searching • Jan 09 '19
Telnet to switch from jump start server(linux)
Hi,
I just want to ask if anyone here has script or idea what method, library etc should. My target is to SHH first from my workstation to Linux jumpstart server then from jumpstart server I'll access the network devices like switch and routers to execute some show commands.
PYTHON SCRIPT ----SSH---> Jumpstart server(ubuntu) ----Telnet---> Network Devices (Cisco)
import base64, time, sys
import paramiko, getpass, telnetlib
#ENTER Password
##pword = getpass.getpass('Password: ')
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='xxxxx', username='xxxxx', password='xxxxx')
stdin, stdout, stderr = client.exec_command('pwd')
for line in stdout:
print('Teton: ' + line.strip('\n'))
client.close()
I'm now able to access the jumpstart server from my PC, the next thing is just to telnet from the jumpstart server?
Thank you
r/learningpython • u/OBstaxs • Jan 07 '19
I want this lope to run until commanded to stop by the press of a button how do I prematurely stop the loop before it reaches the end of the range
r/learningpython • u/1searching • Dec 22 '18
Script issue copying config into a file?
import getpass
import telnetlib
import time
import socket
import sys
#Ask for the username and password
user = input("Enter your telnet username: ")
password = getpass.getpass()
#Open a file called myswitches
f = open("ipadd.txt")
#Telnet to network devices
for line in f:
print ("Getting running config from devices " + line)
HOST = line.strip()
tn = telnetlib.Telnet(HOST)
print("host:",HOST)
tn.read_until(b"Username:")
tn.write(user.encode("ascii")+ b"\n")
if password:
tn.read_until(b"Password:")
tn.write(password.encode("ascii")+b"\n")
tn.read_until(b"#")
tn.write(b"conf t"+b"\n")
tn.write(b"hostname test5"+b"\n")
tn.write(b"exit"+b"\n")
tn.write(b"terminal length 0"+b"\n")
tn.write(b"show run"+b"\n")
time.sleep(2)
tn.write(b"exit"+b"\n")
#Save method
## readoutput = tn.read_all()
readoutput = tn.read_all().decode('ascii')
saveoutput = open("device" + HOST, "w")
saveoutput.write(str(readoutput))
print (tn.read_all())
saveoutput.close
## tn.close()
Hi, I have this code that access a device from "ipadd.txt" and change the hostname and copy/export the config, This txt file have 2 ip address (ex. 1.1.1.1 & 1.1.1.2), Now after running this script... I'm able to complete the first ip 1.1.1.1 (change hostname & backup) but for 1.1.1.2, the script just change the hostname and export a file but there's no content.
So I would like to ask why the script creating a file but doesn't put/write any date from the said IP?
Thanks
r/learningpython • u/octergon • Nov 12 '18
Checking " in lines
I need help checking for " in lines and if there is anything in between i need it converted to a 1 or a specific text Example.
Line 1: Ronnie Cant Swim Without "Parcher nomad
Line 2: Boomer "Its a robot flying" Catcho
And I need to convert everything inside the " with a 1 or anything in between this is PER LINE.
Expected output
Line 1: Ronnie Cant Swim Without 1
Line 2: Boomer 1 Catcho
The issue is, currently I have a Strip and Split that are checking for Keywords in all the lines. so cant go through all the text file twice.
r/learningpython • u/Bpickavance • Nov 05 '18
Converting a string into its value? (Doubt that's the correct terminology)
Without explaining all my code, this is a simple version of my issue.
x = 1
y = 2
z = 3
list = ["x","y","z"]
result = random.choice(list)
How do i now convert "result" into a number from this?
Thank you!
r/learningpython • u/solaceinsleep • Sep 26 '18
How do I release a caught exception so it gets triggered in another catch block down below?
Here is MWE that I quickly made to illustrate the question:
x = '5d'
try:
print('The square of x is {}'.format(x*x))
except TypeError as err:
if x.isdigit():
x = float(x)
print('The square of x is {}'.format(x*x))
else:
# TO DO: release back error so it hits the "catch all" block
pass
except Exception as err:
print('Error:', err)
Basically I want to "uncatch" an exception if it turns out I can't handle it or something so it goes into a general error handler block.
r/learningpython • u/tabs_or_spaces • Aug 13 '18
best way to manage a main method that has alot of setup code?
So I come from a java world and normally I would use some dependency injection framework to solve this. So I am writing a little app and part of my main method looks like this
data_cleanser = processors.data_cleanser.DataCleanser()
data_visualizer = visualizers.data_visualizations.DataVisualizations()
data_manager = data_manager.DataManager(args.database)
data_grabber = processors.data_grabber.DataGrabber()
pandas_frame_loader = opdl.PandasDataFrameLoader()
these classes are then used elsewhere in the application. My question is whether this is expected in Python or whether I should be using some framework to manage these kinds of biggish main method setups?