Arithmetic Operators and Expressions

Copied!
Happy Pythoning!

s150042028

What do you mean by unary +2 in the second example? 5 - 2 = 3. I can’t understand unary. Please explain it.

Bartosz Zaczyński RP Team

@s150042028 In Python, both the plus (`+`) and minus (`-`) operators have binary and unary versions. The word “binary” in this context means that the operator takes two operands or arguments, while “unary” implies only one argument.

When used with two operands, the plus operator adds them together, which can mean different things depending on the types of the operands:

``````>>> 5 + 2
7

>>> "bat" + "man"
'batman'
``````

When used with numbers, the plus operator performs the addition. For strings, on the other hand, the plus operator is defined as the concatenation operation.

The unary version of the plus operator does nothing, so numeric literals written with or without it are considered equal:

``````>>> +2
2

>>> +2 == 2
True
``````

The minus operator is only defined for numbers. More specifically, the binary minus operator performs the subtraction of two numbers, whereas the unary minus operator flips the sign of the number, or more generally, the arithmetic expression that follows:

``````>>> 5 - 2
3

>>> -2
-2

>>> -(5 - 2)
-3
``````

Let me know if that clears it up for you!

s150042028

Hello,

I am still confused, but I will read it again. However, I am not feeling comfortable with this structure of the course. I mean, there are no projects or exercises. Watching videos is not a useful way for me. There is a 90% chance that I will cancel my membership due to this poor method of teaching. There should be an interactive environment with videos, examples, and exercises. Do you have any suggestions, please?

Bartosz Zaczyński RP Team

@s150042028 Thank you for your feedback. We appreciate you taking the time to share it with us, as we’re continually working to improve our course offerings. Your comments are immensely beneficial in that process!

Please note that this video course is part of a series based on the Python Basics book. We’re working hard to eventually accompany each course in this series with a separate hands-on course that contains practical exercises. We’re almost done with recording all of them, but some will require a little bit of patience. The exercises course for this particular one will come out next Tuesday, November 7.

Once released, you’ll have a chance to solve the accompanying exercises yourself and then confront your solutions with our walkthrough, where we explain each step in detail and the thought process behind it.

In the meantime, I’d highly encourage you to take advantage of other benefits that Real Python offers to its members. If you haven’t already, you can ask for help on our Slack community, or you can come to Office Hours where we can address your doubts in real time. It’s a weekly webinar where we often talk about various Python-related concepts, including solving actual programming challenges that people face at work through screen sharing. This would be a perfect opportunity for you to get some hands-on experience and interact with our team on a personal level. We hope to see you there and look forward to helping enhance your learning experience with us.

Davide70

Difficile da capire un pò, ma solo per chi è agli inizi, ovviamente. Basta rileggere varie volte è diventa molto più chiaro. Fin’ora ottimi videi. Complimenti.

to join the conversation.