Uman Sheikh’s Post

🚨 𝗣𝘆𝘁𝗵𝗼𝗻 𝗙𝘂𝗻 𝗙𝗮𝗰𝘁 𝗗𝗮𝘆 𝟲 You don’t need a temp variable to swap values 😏 𝚊 = 𝟻 𝚋 = 𝟷𝟶 𝚊, 𝚋 = 𝚋, 𝚊 𝚙𝚛𝚒𝚗𝚝(𝚊, 𝚋) 𝗢𝘂𝘁𝗽𝘂𝘁: 𝟷𝟶 𝟻 𝗪𝗮𝗶𝘁… 𝗵𝗼𝘄 𝗱𝗶𝗱 𝘁𝗵𝗮𝘁 𝗲𝘃𝗲𝗻 𝘄𝗼𝗿𝗸? In many languages, you’d do: 𝚝𝚎𝚖𝚙 = 𝚊 𝚊 = 𝚋 𝚋 = 𝚝𝚎𝚖𝚙 But Python says: “3 lines? Nah… 1 hi kaafi hai” 😌 𝗪𝗵𝗮𝘁’𝘀 𝗵𝗮𝗽𝗽𝗲𝗻𝗶𝗻𝗴 𝘂𝗻𝗱𝗲𝗿 𝘁𝗵𝗲 𝗵𝗼𝗼𝗱: Python creates a tuple behind the scenes: (𝚊, 𝚋) = (𝚋, 𝚊) Then it unpacks values back into variables. 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝗶𝘀 𝗽𝗼𝘄𝗲𝗿𝗳𝘂𝗹: • Cleaner code  • Less chance of bugs • Widely used in real-world Python    𝗕𝗼𝗻𝘂𝘀 𝘁𝗿𝗶𝗰𝗸: You can swap more than 2 variables too: 𝚊, 𝚋, 𝚌 = 𝟷, 𝟸, 𝟹 𝚊, 𝚋, 𝚌 = 𝚌, 𝚊, 𝚋 𝚙𝚛𝚒𝚗𝚝(𝚊, 𝚋, 𝚌) 𝗣𝘆𝘁𝗵𝗼𝗻 𝗯𝗲 𝗹𝗶𝗸𝗲: “Shortcut bhi elegant hona chahiye” 📌 𝗗𝗮𝘆 𝟳 𝗰𝗼𝗺𝗶𝗻𝗴 𝘁𝗼𝗺𝗼𝗿𝗿𝗼𝘄: Two variables that look the same… but behave totally differently 😈 Follow for more “yeh itna simple tha??” moments 😄 #Python #Programming #Developers #Coding #AI #DataScience #LearnPython

  • No alternative text description for this image

All my old computer science professors are probably shaking their heads.

To view or add a comment, sign in

Explore content categories