# Given two already defined variables, i and j, write a statement that swaps their associated values

1 vote
248 views

## Problem:

I am new in python programming language and I am very confused about programming logics. Supose, someone has given two already defined variables, i and j, write a statement that swaps their associated values.

Can anyone help me with this problem?

1 vote

To swap the values you should use the code or the statement:

``````i = input('Please enter value of i: ')
j = input('Please enter value of j: ')

temp = i
i = j
j = temp

print('The value of i after swapping: {}'.format(i))
print('The value of j after swapping: {}'.format(j))``````

Run this and your output will be:

``````Please enter value of i: 2
Please enter value of j: 4
The value of i after swapping: 4
The value of j after swapping: 2``````

## Related questions

25 views
Problem: Given that play_list has been defined to be a list, write a statement that sorts the list?
199 views
Problem : I need solution for below problem using Python Given that play_list has been defined to be a list, write a statement that sorts the list.
50 views
Problem: Given the array a, write an expression that refers to the first element of the array?
2 views
Problem: Given an unsorted array, find the max j - i difference between indices such that j > i and a[j] > a[i] in O(n). I am able to find j and i using trivial methods in O(n^2) complexity but would like to know how to do this in O(n)? Please help?
2 views
Question: I know this might seem like an absolutely silly question to ask, yet I am too curious not to ask. Why did "i" and "j" become THE variables to use as counters in most control structures? Although common sense tells me they are just like X, which is ... have some obscure reason behind it? Just in case, I know I can give them whatever name I want and that variables names are not relevant.
29 views
Problem: write a method swaparrayends() that swaps the first and last elements of its array parameter.