This question already has answers here:
python: how to get information about a function?
(5 answers)
Closed 8 years ago.
whenever I use help() on a method I get a weird looking output.
For example, help(obj.readline) gives me the following output:
Help on built-in function readline:
readline(...)
That's all i get. I don't know how to interpret it.Why there is no description about the method? Can somebody please explain to me what's going on?
Is this weird output?
>>> f = open('data.txt')
>>> help(f.readline)
Help on built-in function readline:
readline(...)
readline([size]) -> next line from the file, as a string.
Retain newline. A non-negative size argument limits the maximum
number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.
Related
This question already has answers here:
Understanding .get() method in Python [duplicate]
(5 answers)
Closed 11 months ago.
dict1={"a":4,"b":2,"A":6}
print({k.lower():dict1.get(k.lower(),0) + dict1.get(k.upper(),0) for k in dict1.keys()})
I copied this code from a youtube video. I couldn't understand the code properly. Please help me to figure it out.
I couldn't understand the purpose of 0 in second line.
k.lower():dict1.get(k.lower(),0)
I'm a beginner in python. kindly help me
The second parameter is the value parameter, that is , if the key does not exist this will be the value.
In your example, dict1["B"] does not exist. Normally that would cause an error. But because the value parameter is 0, instead of causing an error it pretends that dict1["B"] is 0. Note that this does not change the original dictionary.\
This question already has answers here:
Python Sort() method [duplicate]
(3 answers)
Closed 2 years ago.
Taking an intro to python course, the following code sorts the string words appropriately, but the function does not return sortString, so I'm not understanding why the output is correct. Can you please help me understand?
def sort_words(string):
splitString = string.split()
sortString = splitString.sort()
return splitString
print(sort_words('python is pretty cool'))
Python .sort() returns None, much like print() does. list.sort() works in place - meaning the list is sorted without needing to assign it to another variable name.
This question already has answers here:
How to check if re.sub() has successfully replaced in python? [duplicate]
(2 answers)
Python: how to substitute and know whether it matched
(3 answers)
Closed 2 years ago.
I want to use re.subn in my program to get no of times changes are made but I can't figure it out how to write the code. I have written the program with re.sub and it works fine but If I use (re.sub(r'\bi\b','I',contents)) in this program it shows this error.
TypeError: write() argument must be str, not int
import re
with open ('Sample.txt','r') as rf:
contents=rf.read()
change=(re.sub(r'\bi\b','I',contents))
with open ('Sample2.txt','w') as wf:
wf.writelines(change)
Can someone please guide how can I use re.subn to get the count.
Thank you.
This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 4 years ago.
I have written a code that ends up outputting what I want but in list format. Just to make it easier to understand, I will make up an input.
If I get
>>>
['H','e','l','l','o',' ','W','o','r','l','d']
as an output, how can I change it to:
>>>
'Hello World'
I have tried using .join() but it tells me that it does not work with lists as an error code.
If you need any more information, or I am being vague, just leave a comment saying so and I will update the question.
And if you leave a downvote, can you at least tell me why so that I can fix it or know what to improve for later posts
You join on the connector like this: ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
Just use join method by passing a list as parameter.
str = ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 6 years ago.
I am trying to test this function
def print_board(M):
for i in range (M.shape[0]):
line=''
for j in range (M.shape[1]):
line+=str(int(M[i,j]))
print(line)
I created a new file to test it and imported my file and the function array but I am not too sure on how I can test it since I don't return it, I tried this :
assert(print_board(array([[1,1,1],[0,0,0],[1,1,1]],dtype='bool')) == '''111 000 111''')
but got
AssertionError
What you could do is redirect Python standard output (where print sends it) to a string, and then compare that string.
See this question for an explanation of how to do that.