1 (edited by Guitarman 2017-04-09 05:03:52)

Hi.
I'm making this topic because I am trying hard to learn python, and I am determined to succeed this time. So I don't have to create a new topic every time I have a question, I'm creating this so that whenever I have a programming question I can just post here to make it easy for me and for anyone who is willing to help me.
Right now I'm reading learn python the hard way, I did exercise zero and one and the study drills in exercise one. Exercise one is just printing text to the screen which I did. It said to put a # character at the beginning of one of the lines which I did, I know you use the # character to write a comment in python. My question is- one of the study drills says to make my script only print one of the lines of my script to the screen. So how do I do that? The only thing I can think to do is put a # character at the beginning of each line to make them comments and just leave one line without a # character so it only prints that line. Is this the correct way to do this? I can't think of any other way to make the script print only one line of text. If there is another way could you please tell me?

Thumbs up

Well you could delete the lines, but yes your idea is fine. Its fairly common for programmers to comment out code they don't want to necessarily delete in order to test segments or what have you.

Thumbs up +1

Thanks. The way it was written in the book it seemed like I missed something somewhere.

Thumbs up

I sincerely hope you are not learning Python 2... because if you are, it's going to be a difficult transition for you when Python 2 is officially declared unsupported and deprecated (and, if I get my way, that will be soon). I say that because developers have become too reliant on Python 2 and the only way to get Python3 more popular is to force the change on them since it seems like they are unwilling to do it themselves, despite the Python foundation supplying them with all the tools necessary to help with the transition.

Thumbs up +1

Attempting to force people to switch won't make v3 more popular, and its not up to you or I to decide what version a person chooses to use given their current needs. The practical differences between the two versions are negligible so transitioning isn't that difficult, and there are still some reasons for using it. Even if Python 2 were dropped tomorrow it wouldn't stop people from using it anymore than depreciating QBasic stopped people from using QBasic, or Pascal, etc.

If you want to blame someone for the current situation, then blame Guido van Rossum. Breaking reverse compatibility in v3 and creating a rift between the versions was his choice, and created this problem in the first place.

Thumbs up +1

@magurp244, you have a point. However, I do not see any reasons why we should still use it. The only reason I can think of is that there are still more libraries available for Python 2. The rift created by the two versions is quite good for us. If you have read the changelog for Python 3 you will find quite a bit of new things. Python2 is worse than Python3: it has multiple string types and is generally harder to use. If people would just use the tools the PSF has provided than this wouldn't be a problem.

Thumbs up +1

7 (edited by Hijacker 2017-04-07 16:42:11)

Ethin wrote:

@magurp244, you have a point. However, I do not see any reasons why we should still use it. The only reason I can think of is that there are still more libraries available for Python 2. The rift created by the two versions is quite good for us. If you have read the changelog for Python 3 you will find quite a bit of new things. Python2 is worse than Python3: it has multiple string types and is generally harder to use. If people would just use the tools the PSF has provided than this wouldn't be a problem.

Python 2 is not actually harder to learn, nor to use than Python 3. The syntax is nearly the same, some classes were renamed and some new features added, but nearly nothing removed. Always thought that more string types could somehow be positive? The string-decision Python 3 now includes internally, which reduces the string types to just one type at all, produces some significant overhead which can be kind of time-consuming on some time-efficient algorithms, just to name one case where Python 3 can be less efficient than Python 2. Yeah, you can reduce this overhead with some intelligent casting, but as soon as you understand this way of coding, you should also not have any problem learning Python 2.
I think it's cool that he looks into Python, no matter which version, and I also guess it's the wrong way to tell him anything about "use this, don't use that", since this will just cause confusion on his side of the line. Could we just stop discussing this stuff? Python 2 is absolutely alright for some beginners, and if he knows Python 2 well, thanks to the python 2to3 utility provided by Python itself, there shouldn't be any problem switching to Python 3 afterwards.
For now we should just concentrate on answering his questions as best as possible.
Best regards.
Hijacker

Thumbs up +2

Hello.
Thanks for all the posts! I appreciate hearing your thoughts and opinions. Yes Ethin I am using python 2, because learn python the hard way is written for python 2 and I want my code to match up with the book's examples to understand what I'm doing. I have no problem using python 3 in the future, but I think for now I'll learn python 2 until I get the hang of things. The book might have been rewritten to talk about python 3, but I'm reading the free online version, which is about python 2 exclusively.
@Ethin, I'm assuming from your posts that you use python. I'm curious what kinds of things do you use it for? This is actually off-topic it's not a programming question I just wondered what you use it for

Thumbs up

There's Python 2 to 3 convert script if you really need to feel the difference. However, once you get enough Python coding experience, you'll be able to transition to Python3 easily.
Yep, the bad fact is that many libraries are still incompatible with Python3, and that's why many devs stick with 2. For example, NVDA is stil maintained in Python2, because wxPython Phoenix for Py3 is still under development, there's no Scons for Python3 (I believe), and they wanna keep XP compatibility for some time. I think Chicken Nugget and TWBlue are also written in 2 due to wxPython and some other that currently work better with Py2.

Thumbs up +1

Hello
Well I'm doing well with learning python but I ran into a couple more questions.
I was writing code, learning how to use variables. I got into strings, and the book said something about using python formatting characters. The ones I used were %s, %r, and %d. What do these do? He didn't explain at all. I googled this, but I didn't understand it when I read the python reference. I also checked out stack overflow, but it just said that these are old string characters. I've looked at the script I've written but I can't make sense of what these do. Like I said I'm reading learn python the hard way, and using python 2.7.
My other question is what do floating point variables do? I had a variable, passengers = 4.0 and I erased the . to see what it did but I couldn't figure the calculation that it made with and without the period. Why would I use a floating point variable in a program?

Thumbs up

11 (edited by magurp244 2017-04-13 06:24:27)

Formatting characters like those are more for inserting variables into strings, this can be useful if you want to input changing numbers or text into a static string. For example:

import random
number = random.randint(0,100)
print "%i is a random number"%number

In the above example, the random number will appear at the beginning of the string when printed because of the format symbol instead of at the end. Different formatting symbols are used for integrating different types of data into strings.

Floating point is another word for Decimals, with things past the period representing a sub value. So 0.5 would be half of 1, 2.50 would be half of 5, etc. These have a wide variety of uses in programming such as physics, collision, and animation.

Thumbs up +1

Hello.
Well I just found out I have python 2.7.8, and I want to upgrade to 2.7.13. Is there a way to upgrade without uninstalling my old version and then reinstalling the current version of python 2?
I found out about pip and I wanted to use it. How do I use pip? Somebody said press windows r and type the command is this the correct way to get python packages?

Thumbs up

You probably can just install over the previous version since you are just talking about installing a minor release update.  Pip is typically installed when you install Python.  To access PIP from the command line, one method is to press Windows Key+R. This will bring up the run dialog. In that dialog type cmd and press enter. This will put you on a command line prompt. You can type PIP commands from there. In Windows 10, you can bring up a folder and select the File menu item.  Under that is an option for opening a command line propmpt and make the current folder the working directory.When you get to the point where you want to start writing software you want to share, I would suggest checking into virtualenv. You also may want to look at using GIT at that point. However, it sounds like you are still learning the foundations of Python so you might want to wait a while before you get into those aspects of development.

Thumbs up +1

I agree with Hektor.
If you intend to write code and don't feel like rewriting, trying to recover deleted files and otherwise dealing with code that you didn't back up, then you need a version control system. I recommend git. As mentioned above you probably have a while to go before this will mean anything to you, but when it does I think you'll be glad of the advice.

Also, deleting or otherwise messing up your code probably sounds like something you would never do and that you could easily recover from if you had backups, but there will come a time (whether a month or a year) when it will happen to you.
The backup will be out-of-date or won't exist at all, the file will accidentally get deleted when you selected one-two-many directories when cleaning up your disk, someone will delete files in a shared dropbox folder and you don't want to / can't use the web interface to pull out an old version... Something will happen. Something version control could've saved you from.
Hope the small rant will be helpful in the future tongue.

Thumbs up +1