Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ◇C++, ◇Clojure, ◇Scala


[ edit | logout ]
[ sandbox | passwd ]

Swap

Python style

(1) Python style
a = 3
b = 5

print((a, b))  # (3, 5)

a, b = b, a

print((a, b))  # (5, 3)
var
  a = 3
  b = 5

echo (a, b)         # (3, 5)

(a, b) = (b, a)     # Parentheses are obligatory!

echo (a, b)         # (5, 3)

C style

(2) C style
#include <stdio.h>

void swap(int *p1, int *p2)
{
    int tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
}

int main()
{
    int a = 3;
    int b = 5;

    printf("%d, %d\n", a, b); // 3, 5

    swap(&a, &b);

    printf("%d, %d\n", a, b); // 5, 3

    return 0;
}
var
  a = 3
  b = 5

echo (a, b)         # (3, 5)

swap(a, b)

echo (a, b)         # (5, 3)

Notes

If you just want to swap the values of two variables, use the swap() function.

If you want to do something fancier, like calculating the next Fibonacci number, use the tuple trick: (a, b) = (b, a+b) . The right side is evaluated first and then parallel assignment happens, just like in Python.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 May 06, 20:56