PythonLearn | primary school students are learning Python | Compiler library
kandi X-RAY | PythonLearn Summary
kandi X-RAY | PythonLearn Summary
I feel a lot of pressure. The primary school students are learning Python. I just started to learn Python when I have more free time. I record some notes during the learning process here. I hope everyone can encourage them! PS: The Java part will be updated synchronously, and may not be updated on
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Show an article .
PythonLearn Key Features
PythonLearn Examples and Code Snippets
Community Discussions
Trending Discussions on PythonLearn
QUESTION
I just started learning Python recently. I want to calculate the frequency of each character in a list. However, my code only works for the first element in the list.
my code:
...ANSWER
Answered 2020-Oct-03 at 14:00I see two problems here:
- You are parsing your list of arguments in the wrong way.
- No need to manually increment a counter. The
Counter
class fromcollections
will do fine here:
QUESTION
I am using Python 3.7.4 Simply trying to run this code:
...ANSWER
Answered 2020-Jan-16 at 05:40Try python 2.7
QUESTION
def fun(a, b, c, d):
print('a:', a, 'b:', b, 'c:', c, 'd:', d)
...ANSWER
Answered 2019-Nov-20 at 20:00With *(23,)
, you are unpacking the values in the tuple (23,)
as positional arguments, following the positional arguments that are already defined, namely 3
for a
and 7
for b
, so 23
would be assigned to parameter c
, which is why fun(3, 7, d=10, *(23,))
works, but in fun(3, 7, c=10, *(23,))
you are also assigning to value 10
to c
as a keyword argument, so it is considered a conflict as c
cannot be assigned with both 23
and 10
.
Note that while legal, it is discouraged by some to unpack iterable arguments after keyword arguments, as discussed here, although the syntax is ultimately ruled to stay.
QUESTION
Running following code:
...ANSWER
Answered 2019-Jul-31 at 17:05Inside NonMeta.__new__
, x
is an instance of NonMeta
, so the assignment to x.attr
is creating an instance attribute for the instance x
.
Inside Meta.__new__
, x
is an instance of Meta
, namely a new class. In this case, x
is the class WithMeta
, so the assignment to x.attr
creates a class attribute.
The class
statement is, in some sense, syntactic sugar for a call to the metatype. That is,
QUESTION
So I've been doing a bit of research regarding the underscore character (_
). I know most of its use cases and its semantics in them so I'll just drop them below as a recap and I'll conclude with the question, which is more of a conceptual one regarding 2 of the use cases.
- To store the last evaluation in an interpreter (outside the interpreter it has no special semantics and it's undefined as a single character)
- In internationalization context, where you'd import functions such as
gettext
aliased as_
In decimal grouping to aid visibility (specifically doing groups of 3 such as 1_000_000) - Note that this is only available from Python 3.6 onward.
Example:
...
ANSWER
Answered 2019-Jun-05 at 19:21Your point of view seems to be based on the assumption that what is private or public ( or the equivalent suggestions in python ) is based on the developer who read and write code. That is wrong.
Even if you write all alone a single application that you only will use, if it is correctly designed it will be divided into modules, and those modules will expose an interface and have a private implementation.
The fact that you write both the module and the code that uses it doesn't mean that there are not parts which should be private to keep encapsulation. So yes, having the leading underscore to mark parts of a module as private make sense regardless of the number of developer working on the project or depending by it.
EDIT:
What we are really discussing about is encapsulation, which is the concept, general to software engineering for python and any other language.
The idea is that you divide your whole application into pieces (the modules I was talking about, which might be python packages but might also be something else in your design) and decide which of the several functionality needed to perform your goal is implemented there (this is called the Single Responsibility Principle).
It is a good approach to design by contract, which means to decide the abstraction your module is going to expose to other parts of the software and to hide everything which is not part of it as an implementation detail. Other modules should not rely on your implementation by only on your exposed functionalities so that you are free to change whenever you want to increase performance, favor new functionalities, improve maintainability or any other reason.
Now, all this theoretical rant is language agnostic and application agnostic, which means that every time you design a piece of software you have to rely on the facilities offered by the language to design your modules and to build encapsulation.
Python, one of the few if not the only one, as far as I know, has made the deliberate choice (bad, in my opinion), to not enforce encapsulation but to allow developers to have access to everything, as you have already discovered.
This, however, does not mean that the aforementioned concepts do not apply, but just that they cannot be enforced at the language level and must be implemented in a more loose way, as a simple suggestion.
Does it mean it is a bad idea to encapsulate implementation and freely use each bit of information available? Obviously not, it is still a good SOLID principle upon which build an architecture.
Nothing of all this is really necessary, they are just good principles that with time and experience have been proven to create good quality software.
Should you use in your small application that nobody else uses it? If you want things done as they should be, yes, you should.
Are they necessary? Well, you can make it work without, but you might discover that it will cost you more effort later down the way.
What if I'm not writing a library but a finished application? Well, does that mean that you shouldn't write it in a good, clean, tidy way?
QUESTION
I'm trying to collect some soccer game data to a single table by python3, jinja2 to a stylized HTML template page. I'm just beginner in programming and I may have made some mistake on transferring the data.
I tried to print the result out it seems ok like
...ANSWER
Answered 2019-Apr-26 at 11:31Perhaps try:
QUESTION
What I need to do is this:
Write a program that categorizes each mail message by which day of the week the commit was done. To do this look for lines that start with “From”, then look for the third word and keep a running count of each of the days of the week. At the end of the program print out the contents of your dictionary (order does not matter).
I've gotten the basics down reading into the file searching, but I can't figure out how I would have python search through the line to find the day of the week and then add to the count. The method I tried returns an error.
I'm using Repl.it to run python and it is v3 of it. if you need to view the txt file here it is http://www.pythonlearn.com/code3/mbox.txt
...ANSWER
Answered 2019-Apr-21 at 00:31just change the for line in rd
loop into this:
QUESTION
The main issue is I cannot identify what is causing the code to produce this value. It is supposed to read the values in the text file and then calculate the average confidence of the values. But I've recieved repeated errors. the one here and another which states 'could not convert string into float' if I have it tell me which line it will be the first one.
I'm using Repl.it to run python and it is v3 of it. I've tried doing this on my computer I get similar results, however, it is very hard to read the error so I've moved it there to see better.
...ANSWER
Answered 2019-Apr-14 at 20:07There are several problems with your code:
- you're using string methods like
find()
andstrip()
on the listrd
instead of parsing the individual line. find()
returns the lowest index of the substring if there's a match (since"X-DSPAM-Confidence: "
seems to occur at the beginning of the line in the text file, it will return index 0), otherwise it returns -1. However, you're not checking the return value (so you're always assuming there's a match), andrd[srchD + 1:fmLen]
should beline[srchD + len("X-DSPAM-Confidence: "):fmLen-1]
since you want to extract everything after the substring till the end of the line.count
andtotal
are not defined, although they might be somewhere else in your code- with
total = fltNum + count
, you're replacing the total at each iteration withfltNum + count
... you should be addingfltNum
to the total every time a match is found
Working implementation:
QUESTION
I am studying "Python for Everybody" book written by Charles R. Severance and I have a question to the exercise2 from Chapter7.
The task is to go through the mbox-short.txt file and "When you encounter a line that starts with “X-DSPAM-Confidence:” pull apart the line to extract the floating-point number on the line. Count these lines and then compute the total of the spam confidence values from these lines. When you reach the end of the file, print out the average spam confidence."
Here is my way of doing this task:
...ANSWER
Answered 2019-Jan-20 at 15:33List-comprehensions can often replace for-loops that add to a list:
QUESTION
I wrote the spark program which reads the CSV file and write the result in console. I am getting the error when running it. I am using spark 2.2.0.
Sample File:
...ANSWER
Answered 2018-Sep-03 at 13:30You don't need the dot in the format
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install PythonLearn
You can use PythonLearn like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page