Skip to content

Conversation

@Raj142
Copy link
Contributor

@Raj142 Raj142 commented Oct 1, 2020

"Hacktoberfest 2020"


temp = a
a = b
b = temp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi.
You can swap two integers without temp by using xor operator:
a = a^b
b = b^a
a = a^b

or much better.
You can swap with tuples:
a, b = b, a

b = temp

print('The value of a after swapping: {}'.format(a))
print('The value of b after swapping: {}'.format(b)) No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using f string instead of format makes the print more understandable and short.

print(f"The value of a after swapping: {a}") is equivalent to your code.
It may not look advantageous in this case, but when sentences get long with more variables, it is certain to come in handy. Just some tips for better python programming :-)

@anshrathod anshrathod merged commit 29176de into anshrathod:master Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants