What is the difference between ", " and "+" in Python?

Asked

Viewed 239 times

3

In a code

mensagem = "ola "
nome = input("Diga seu nome ")
print (mensagem + nome ) 
mensagem = "ola"
nome = input("Diga seu nome ")
print (mensagem , nome ) 

The two do the same function , with the difference that using the signal + , I have to give a space. I know that + concatenate, but that’s the only difference?

2 answers

3

In fact the operator + in this context makes the concatenation of texts in a simple way, which means that it will generate another object that will contain the contents of the two objects used with the operator, and that object will be used wherever necessary, for example can be an argument to pass to a function as it was used in the first code. The concatenation by chance is being composed with the function print().

The operator , has nothing to do with it, it serves to separate elements in a list in some contexts, and the exact shape varies according to the context.

The function print() accepts several arguments. From 0 to virtually unlimited can be used because it is like passing a array. How do you separate each argument to be passed to a function? Simple, that’s a list of arguments, separates with the comma.

So in the second code is calling the function by passing two arguments to the function print(). By chance who wrote it determined that this function would print the arguments that come after the first with a separate blank space. In that context it happens, but it is what the print() does, does not serve for other situations in the language.

It may seem that the result is almost the same, but what is happening is totally different. No new object is created for this, so it is more efficient.

It’s actually more complicated than you think. I’m sorry to inform you, but you seem to be learning in a way that doesn’t give you a sense of how things really work in code, so you’re gonna have a hard time forever if you don’t change the way you learn.

When you learn the causes and consequences you can build more advanced code in each step you take and program that is it, understand each concept and go combining them correctly to produce the best result. When you learn only the results you can only do what you’ve seen someone do before, and that’s not programming.

Let me give you an example that sounds silly. Why did you give space before opening parentheses in one function and not in the other? Both work. But understanding why it gives space or why it’s better without space makes a difference to actually understand what the code is, why it’s doing that. It works to do randomly as it is in the code, but without thinking about what you’re writing, you’ll see that there’s a reason not to use space there and you’ll take the first step to be more aware of everything you write and understand every detail of the code, even the ones that don’t seem important, but that has a meaning.

That’s what differentiates who’s new for a while and who’s new for a lifetime. But of course everyone can choose the path they think best, I gave the tip because it seemed like they want to learn the right one.

  • That’s exactly how I feel. I’m studying watching the Lura videos and reading a book called "Intensive Python Course", the videos are based on taking a project, and teaching you how to do it, but there are no specific videos about each function, many things are passed very quickly, the book presents many things, but without much deepening , in the end I will make 3 projects, the most interesting is a game. Do you know of any course or book that I can delve into? I have a disability in English ( study every day, will soon be solved), in English is not a possibility

  • 3

    That’s what courses do, they create an illusion that you’re learning because you see the result. These courses are in the business of creating happiness and not preparing people to be professionals. That’s why they sell so much. And I’m not talking about a specific one, I’m talking about a general one, I can’t individually evaluate any of them. But it’s a bit of a problem for people as well. They believe they can learn simply and quickly, the courses deliver what they want. You have to pick up a lot of content, some boring, you have to look at things that seem irrelevant to a layman, but they’re not.

2


As @Maniero explained, the way it works is completely different, despite doing the same thing(in this case), and I would like to add some information not mentioned by him.

The print function has the parameter sep=' ', which is a separation character (by default, it is a white space). So, if you pass more than one object to print(string, int, etc), it inserts the objects with the separation character. For example:

print('oi','MrLexotan')

returns

>>>oi MrLexotan

however

print('oi', 'MrLexotan', sep='@')

returns

>>>oi@MrLexotan

and if you do

print('oi', 'MrLexotan','tudo', 'bem','?', sep='@')

returns

>>>oi@MrLexotan@tudo@bem@?

What advantage do you have with sep?

The advantage is: in the future, it is possible to come across a situation where you do not know the number of objects to pass in the print function, so it is impossible to give space individually between them (as you did in the case of the message + name. You set a tab ends this problem.

I hope you understand. Although I found the friend Maniero a little impatient in his reply, I tried to explain in the most didactic way possible. Programming languages are made by human beings, who have encountered problems, and so have created these solutions. Hardly two things will have the same function.

Don’t be discouraged by people who have dominated the area for years and accuse you of learning the wrong way. Learning the way you can is better than giving up. Programming tb is learning to learn, and this is caught up with practice.

Hugs and good luck in the journey through the world of technology :)

  • I get it, thank you very much. I understand his point of view , and having to partially agree , I feel that I could be learning more, as I study, more doubts appear , but I can not find a course or book in Portuguese, that can remedy my doubts, I know that English is very important in programming, I am studying, but at the level I am, trying to learn is not an option, a lot would be left out. Recommends some material in English ?

  • 1

    I found your answer impatient with my answer. If I think it wrong do not you the same thing you are criticizing. Criticism is valid when it is not committed.

  • Keep in mind that English is not a fixed content. Like English, you learn specific terms to certain areas when you contact them. At the basic level you can still learn Portuguese(Na udemy has some fees, but if you don’t want to spend it, I recommend https://www.youtube.com/watch?v=YO58tXerKDc&list=PLUukMN0DTKCtbzhbYe2jdF4cr8MOWClXc (excellent).

  • Similarly, if you follow in the area, it is inevitable you learn the basics. As you progress, the best content will be in English, sometimes even non-existent version in Portuguese

Browser other questions tagged

You are not signed in. Login or sign up in order to post.