Fix Python – String formatting in Python 3
I do this in Python 2:
“(%d goals, $%d)” % (self.goals, self.penalties)
What is the Python 3 version of this?
I tried searching for examples online but I kept getting Python 2 versions.
….
I do this in Python 2:
“(%d goals, $%d)” % (self.goals, self.penalties)
What is the Python 3 version of this?
I tried searching for examples online but I kept getting Python 2 versions.
….
Im trying to do a HTTPS GET with basic authentication using python. Im very new to python and the guides seem to use diffrent librarys to do things. (http.client, httplib and urllib). Can anyone show me how its done? How can you tell the standard library to use?
….
Why does Python 3 allow “00” as a literal for 0 but not allow “01” as a literal for 1? Is there a good reason? This inconsistency baffles me. (And we’re talking about Python 3, which purposely broke backward compatibility in order to achieve goals like consistency.)
For example:
>>> from datetime import time
>>> time(16, 00)
datetime.time(16, 0)
>….
I was trying to remove unwanted characters from a given string using text.translate() in Python 3.4.
The minimal code is:
import sys
s = ‘abcde12345@#@$#%$’
mapper = dict.fromkeys(i for i in range(sys.maxunicode) if chr(i) in ‘@#$’)
print(s.translate(mapper))
It works as expected. However the same program when executed in Python 3.4 and Python 3….
Say a I have a dataclass in python3. I want to be able to hash and order these objects.
I only want them ordered/hashed on id.
I see in the docs that I can just implement _hash_ and all that but I’d like to get datacalsses to do the work for me because they are intended to handle this.
from dataclasses import dataclass, field
@dataclass(eq=True, ….
I’m reading the tutorial here: https://docs.djangoproject.com/en/1.5/ref/models/fields/#choices
and i’m trying to create a box where the user can select the month he was born in. What I tried was
MONTH_CHOICES = (
(JANUARY, “January”),
(FEBRUARY, “February”),
(MARCH, “March”),
….
(DECEMBER, “December”),
)
month = CharField(….
I am using Python 3.6.1, and I have come across something very strange. I had a simple dictionary assignment typo that took me a long time to find.
context = {}
context[“a”]: 2
print(context)
Output
{}
What is the code context[“a”]: 2 doing? It doesn’t raise a SyntaxError when it should IMO. At first I thought it was creating a slice. However, t….
Python 3.6 is about to be released. PEP 494 — Python 3.6 Release Schedule mentions the end of December, so I went through What’s New in Python 3.6 to see they mention the variable annotations:
PEP 484 introduced standard for type annotations of function parameters, a.k.a. type hints. This PEP adds syntax to Python for annotating the types of var….
Perhaps I’ve fallen victim to misinformation on the web, but I think it’s more likely just that I’ve misunderstood something. Based on what I’ve learned so far, range() is a generator, and generators can be used as iterators. However, this code:
myrange = range(10)
print(next(myrange))
gives me this error:
TypeError: ‘range’ object is not an it….
After reading everything I can find on lambda, I still don’t understand how to make it do what I want.
Everyone uses the example:
lambda x, y : x + y
Why do you need to state both x and y before the 😕 Also how do you make it return multiple arguments?
for example:
self.buttonAdd_1 = Button(self, text=’+’, command=lambda : self.calculate(self.bu….