#13 Python Tutorial for Beginners | Swap 2 Variables in Python

Поділитися
Вставка
  • Опубліковано 21 жов 2018
  • Spring and Microservices Live Course : bit.ly/springmslive
    Coupon: TELUSKO25 (25% Discount)
    Industry-Ready Java Spring Microservices Developer Live : bit.ly/JavaMS2
    Complete Java Developer Course : bit.ly/Comp-Java-Dev-2
    Coupon: TELUSKO20 (20% Discount)
    For More Queries WhatsApp or Call on : +919008963671
    website : courses.telusko.com/
    Instagram : / navinreddyofficial
    Linkedin : / navinreddy20
    TELUSKO Android App : bit.ly/TeluskoApp
    Discord : / discord
    In this lecture we are discussing about different ways to swap two numbers:
    -- suppose we have two variables a and b and we want to swap the value of a and b
    -- we can do this by directly assigning the value of b to a and a to b lead to loose of value of a and b
    -- we can do this by using temp variable
    -- we can do this by using xor operator
    -- we can do this by using python technique
    directly assigning leads to loose of value
    a=5
    b=6
    a=b
    print(a)
    6
    print(b)
    6
    a=5 # resign the value of a to 5
    use temp variable for swapping
    temp=a
    a=b
    b=temp
    print(a)
    6
    print(b)
    5
    #without using third variable we can swap the value
    a=a+b
    b=a-b
    a=a-b
    print(a)
    5
    print(b)
    6
    #use of xor operator for swapping
    a=a^b
    b=a^b
    a=a^b
    print(a)
    6
    print(b)
    5
    in python we have another technique
    a,b=b,a
    print(a)
    5
    print(b)
    6
    Github :- github.com/navinreddy20/Python-
    Python for Beginners :- bit.ly/3JOLQhl
    Java and Spring Framework For beginners with Spring Boot : - bit.ly/3LDMj8D
    Java Tutorial for Beginners (2023) :- bit.ly/3yARVbN
    Check out our website: www.telusko.com
    courses.telusko.com
    Subscribe to our other channel:
    Navin Reddy : / @navinreddy
    Telusko Hindi : / @teluskohindi
    Donation & Support:
    Indian Payment : www.instamojo.com/@NavinReddy/
    PayPal Id : navinreddy20
    Patreon : navinreddy20
  • Наука та технологія

КОМЕНТАРІ • 405

  • @8binodkumarbinodkumar936
    @8binodkumarbinodkumar936 4 роки тому +77

    Sir are there any notes of yours u are really osm and my last helper..

  • @full_disclosure_now5844
    @full_disclosure_now5844 3 роки тому +33

    I'm studying Python in a Master course and Telusko is better than my professors, keep doing it please!

  • @gitanjalidas4762
    @gitanjalidas4762 4 роки тому +49

    I feel like i am in a class. So good sound, english, way of learning and teaching... Wow, i didnt expect so much! You're awesome! Keeo rocking!🙃

  • @NitinJainNoida
    @NitinJainNoida 5 років тому +77

    To calculate XOR of two numbers, the thumb rule is, "If the two digits are same then put 0 else if they are different put 1 ". For.e.g calculate xor of 10 and 20. So the binary of 10 and 20 is shown below and the calculation
    01010
    10100
    -----------
    11110 which is 30
    Thanks

  • @fccy9760
    @fccy9760 2 роки тому +5

    thank you so much! i just spent half an hour trying to understand my professor's analogy for variable swapping. this was straightforward and comprehensive, thank you!!!

  • @sahilprajapati8306
    @sahilprajapati8306 4 роки тому +14

    Another way to swap-
    a = a*b
    b = a/b
    a = a/b
    👍👍👍👍
    By the way big fan...

    • @shaikhimran3401
      @shaikhimran3401 3 роки тому +1

      Its should be
      a = a*b
      b = a//b
      a = a//b
      Then u will ans single number .. without .0

    • @deepthichaitanya9111
      @deepthichaitanya9111 3 роки тому

      yes o/p comes in float. Need to use //(Integer division) to get correct o/p if using integers.

    • @cartoons__for__kids_Hindi
      @cartoons__for__kids_Hindi 3 роки тому

      But you are wasting 1bit

  • @kashishchadha18
    @kashishchadha18 4 роки тому +57

    If your confused like me how to use phycharm then just go to tutorial 17 it's all about how to use the software

    • @samindhungana6361
      @samindhungana6361 3 роки тому +2

      Thank you

    • @PBrokenShowsEtechyguy
      @PBrokenShowsEtechyguy 3 роки тому +1

      thanks kashish

    • @samrudhraja224
      @samrudhraja224 3 роки тому +1

      Thank you

    • @nishuahuja1688
      @nishuahuja1688 3 роки тому

      Thank you

    • @maedre9330
      @maedre9330 3 роки тому

      To use pycharm ,open it , make a new project, right click on blank screen, click on create and choose python file. Then open it. Type urncode and use shift+f10 to run it.preyty simple

  • @ashishdash2010
    @ashishdash2010 5 років тому +5

    When i saw this in java i was confused that how this formula just worked. I was confused about swapping.. But now all that stuff is cleared.. Thanks to naveen bro...

  • @adityabhambhani207
    @adityabhambhani207 2 роки тому +2

    i have been learning alot from your videos sir ......your way of teaching is awesome, learnt more in 13 videos than in any other year

  • @gowthama5833
    @gowthama5833 2 роки тому

    You are really awesome sir teaching way is excellent, while iam studying final year iam fearing about python course but when I saw ur videos it's very easy 😁

  • @kyle.winter
    @kyle.winter Місяць тому

    I was searching high and low for a good explanation of swapping variables with a third variable. This was by far the easiest to understand!

  • @artmaster9409
    @artmaster9409 4 роки тому +1

    Thank u so much sir😃ur the best teacher of programming.ur all videos are excellent and understandable....and all videos are helpful.once again thanks for making all those videos☺️

  • @AMAdet
    @AMAdet Рік тому +1

    wow, this swapping concept is amazing. Thanks Navin for this

  • @vineethkumar2272
    @vineethkumar2272 5 років тому +24

    I learned The Xor swapping here Tq for that Navin Sir
    Finally in Telusko nenu Telusukunna

  • @shankargope2475
    @shankargope2475 8 місяців тому

    Wonderful the concept of swapping variables is really amazing👌. You have explained with different concepts to execute this and it's clear to me now specially the rot_two(). Thank you very much for such efforts❤

  • @mikemcconeghy4658
    @mikemcconeghy4658 4 роки тому +149

    He keeps calling us aliens, making me wonder what planet he is from.

  • @theodoramavrikiou3268
    @theodoramavrikiou3268 3 роки тому

    great, now i can simply copy this technique to finish my computer homework that our computer teacher has given us. Thank you wise man.

  • @vidhyavarshiniprakash4574
    @vidhyavarshiniprakash4574 4 роки тому +27

    Thanks bro seriously....it was damn good even for a beginner!!!

  • @AmitSingh-er3lk
    @AmitSingh-er3lk 5 років тому +2

    Your way of teaching is amazing

  • @benindi
    @benindi 4 роки тому +2

    This was very informative. I did not know that Python was this good

  • @mathstutorclass10thmathsso86
    @mathstutorclass10thmathsso86 4 роки тому +2

    Very nice teaching sir.....i am really enjoying....
    Thanks for these informative vdos

  • @ayaazahmad2888
    @ayaazahmad2888 3 роки тому +1

    You are far better than my subject teacher..👍🏻

  • @shashwathx6571
    @shashwathx6571 3 роки тому

    this is the best python tutorial i have ever seen

  • @kshamaawasthi4523
    @kshamaawasthi4523 2 роки тому

    Amazing ...you make it so simple...u have deep knowledge❤️

  • @abhishekshrivastav5410
    @abhishekshrivastav5410 4 роки тому +7

    For those of you wondering how he got the pycharm ide go to video no. 17 @5:40

  • @JourneywithRaquel
    @JourneywithRaquel 4 роки тому

    I have been loving the videos and learning a lot. At this point I think what is occurring for me is that I have learned the lessons up to this point but how do I use what I have learned? What would I be using it for? How do I get it to stick without application and repetition? Help!

  • @kakarlasundarganesh3216
    @kakarlasundarganesh3216 5 років тому +62

    Please give us a lesson to work with pycharm environment.we r not able to get u

    • @boopahari9062
      @boopahari9062 3 роки тому

      Yeah

    • @tharunkumar9208
      @tharunkumar9208 3 роки тому

      Same bro....dont know how to use pycharm...its very confusing

    • @sangu2227
      @sangu2227 3 роки тому

      @@tharunkumar9208 same here bro plz tell us how to use pycharm

    • @anithaachal1537
      @anithaachal1537 3 роки тому

      Go to tutorial 17

  • @pillasatish8826
    @pillasatish8826 2 роки тому

    Superb explanation...you are the best among all which i have seen..
    Can you please share a notes or pdf if you have

  • @aravindamatha
    @aravindamatha 4 роки тому +1

    sir because of you i learned python very fast

  • @64_hrushant_raghwarte44
    @64_hrushant_raghwarte44 3 роки тому +1

    It's a really helpful for me.... thank you sir😇

  • @mayankjulah4408
    @mayankjulah4408 3 роки тому +12

    You haven't explained how to use Pycharm in any of your previous video and you're using it in this video without even telling the fundamentals of it. It's not fair.I am sure a lot of viewers (Including me) won't be able to get this and will find it confusing. I myself have wasted a lot of time just trying to figure out Pycharm. So please take care of all this stuff.

  • @vivekp5619
    @vivekp5619 4 роки тому +2

    a mechanical engineer is learning python bcaz of u, thank u gurujiiiiiiiiiiiiiiiii

    • @manishkadam6724
      @manishkadam6724 3 роки тому +1

      A CA student learning Programming Because of Him. Respect for this MAN !!

  • @shivanishrivastava4539
    @shivanishrivastava4539 3 роки тому +1

    Hi, Navin
    I didn't understand why there's formula for swaping and that's clearly doing iteration adding/substracting making it one another step!

  • @saidedipya7805
    @saidedipya7805 2 роки тому

    Very good explanation...short vedios are creating intrest to watch more.Thank you

  • @rakshitrakshit6022
    @rakshitrakshit6022 3 роки тому

    great video sir i am able to learn very easily from this

  • @yogeshkumarshankariya642
    @yogeshkumarshankariya642 4 роки тому +1

    Dear sir, I think you should give guidance regarding how to create or familiar with pycharm.

  • @amandaahringer7466
    @amandaahringer7466 2 роки тому

    This was super helpful, thank you!

  • @ShivamPanchbhai
    @ShivamPanchbhai 5 років тому +15

    a=a^b
    b=a^b
    a=a^b
    sir aap konsi duniye se aye ho
    never thought of using xor in this way

    • @ayush4731
      @ayush4731 5 років тому +6

      Welcome to the alien world :)

    • @user-hh6ne9gc8v
      @user-hh6ne9gc8v 17 днів тому

      well your method is good but is not accurate , your answers will be in decimal if you use your method. Example, if a = 36 and b = 56, then you use your method your answer will be 56.6 for a and 36.6 for b but you can correct it by typing = int(a) which is 56 and int(b) which is 36

  • @asutoshmohapatra1339
    @asutoshmohapatra1339 4 роки тому

    Great video no one ever taught me that xor method

  • @KingKing-px6oh
    @KingKing-px6oh 3 роки тому

    Sir your teaching is good,,🌹🌹

  • @shridayesinghgondal7527
    @shridayesinghgondal7527 3 роки тому +7

    Sir we can use "a, b = b, a" also... It works
    .
    .
    Big Fan🙏

  • @meeram9859
    @meeram9859 2 роки тому

    Thanks for explaining variety of swapping

  • @anindyabanerjee6262
    @anindyabanerjee6262 2 роки тому

    Sir, your mental thinking is unbeleavable

  • @vinitbishnoi8752
    @vinitbishnoi8752 5 років тому +14

    use a,b=b,a

  • @yashbinwani5536
    @yashbinwani5536 4 роки тому +1

    Bro u r Magician

  • @aa6x_x
    @aa6x_x 2 роки тому +2

    it's easier than you think, you can just fool the client by doing this:
    print('The value of a is '+str(b))
    print('The value of b is '+str(a))

  • @vakhariyajay2224
    @vakhariyajay2224 Рік тому +1

    Thank you very much. You are a genius.

  • @abdulmateen7486
    @abdulmateen7486 5 місяців тому

    Excellent video. Pl clear the concept of "wasting 1 bit"

  • @Rajadahana
    @Rajadahana 2 роки тому +2

    To understand the 2nd method at 1:50 I thought of a story.
    Just imagine that you have two rods and also two stickers called "A" and "B".
    You take the rod with a length of 5 and paste the sicker "A" on it.
    Then you take the other rod with a length of 6 and paste the sicker "B" on it.
    Now you join the two, which is with a length of 11, you leave the sticker "A" on it and remove the sticker "B".
    Now you cut away the pole from the joint and paste the sticker "B" on the side of the length with 5.
    You leave the sticker "A" on the side with a length of 6.
    If you could visualize this you will understand this method easily. Keep it up!!!

  • @adilshaikh4788
    @adilshaikh4788 4 роки тому

    Awesome as usual

  • @kaursimran191
    @kaursimran191 3 роки тому

    Sir
    .u r videos r awesome 👌👌👌👌👌👌

  • @rajvaibhavtawadkar164
    @rajvaibhavtawadkar164 4 роки тому +17

    I find also different way swaping
    a=a*b
    b=a/b
    a=a/b
    Nice video 😃

    • @harshgupta9761
      @harshgupta9761 4 роки тому +1

      If any of a and b is 0 then this logic don't work

  • @PrasannaKumar-zx7gr
    @PrasannaKumar-zx7gr 4 роки тому +1

    Well explained bro....

  • @vamsiyarra864
    @vamsiyarra864 5 років тому +1

    Nice explanation :)

  • @ChandraSekhar-tr7sf
    @ChandraSekhar-tr7sf 2 роки тому

    Generally I am not interested to give a comments but this time after seeing this video I can't keep myself calm 🔥

  • @imtiazem
    @imtiazem 2 роки тому

    thank you for making these videos.

  • @barkeeper7887
    @barkeeper7887 5 років тому +4

    lmao the Thumbnail already showed me the answer to my questin thx man

  • @ShivamPanchbhai
    @ShivamPanchbhai 5 років тому +7

    how can't this channel have billion subscribers

    • @II_xD_II
      @II_xD_II 5 років тому +8

      coz this is not T-Series

    • @Rock28099
      @Rock28099 5 років тому

      🤣🤣

  • @kaursimran191
    @kaursimran191 3 роки тому

    And ur way of talking is really cool😎

  • @sambhavmittal581
    @sambhavmittal581 5 років тому +53

    could u pls make a video on how python different from another language (By using Functions)
    those who intrested pls like and reply this

  • @hardeepkour4653
    @hardeepkour4653 9 місяців тому

    Thank you so much sir
    This really helped in alot

  • @srushtiagarwal2748
    @srushtiagarwal2748 4 роки тому +6

    sir, explain concept of rotation with example

  • @amanrauniyar2942
    @amanrauniyar2942 3 роки тому

    Amazing tutorial sir,

  • @kisantimsinacoverz9744
    @kisantimsinacoverz9744 4 роки тому +2

    I enjoy a lot ..😁😁

  • @vanitha433
    @vanitha433 2 роки тому +1

    Awesome teaching

  • @oldokra
    @oldokra 2 роки тому

    very much helpful, thank you.

  • @snehavedpathak3573
    @snehavedpathak3573 2 роки тому +1

    Hello sir.. Just curious..
    If we take 2 input from user
    n1 and n2
    and without doing any operations we just print
    First
    print(n2)
    print(n1)
    Will it work na..
    And can pass all the test cases and also
    Time and space complexity will also be less..
    Please correct me if am wrong...

  • @arunaagt8229
    @arunaagt8229 5 років тому +2

    1.Are you running this program in pycharm? 2.what is the valid interpreter in pycharm while running a program?

  • @lambuking
    @lambuking 10 місяців тому

    you rock and keep teaching

  • @sunilkharwar5633
    @sunilkharwar5633 3 роки тому

    keep making videos like this sir

  • @keerthanareddy8856
    @keerthanareddy8856 4 роки тому +1

    Your explanation is very well sir..I'm very thankful for your lectures☺☺
    If==> a,b,c=1,2,3
    a,b,c=c,a,b=b,a,c=c,b,a
    print(a,b,c)
    What will be stored in a,b,c?
    I'm waiting for your answer sir
    I also need explanation sir
    Thank you...!!

    • @labdhogranagasamudram4364
      @labdhogranagasamudram4364 3 роки тому

      New (a, b, c)=(2.3.1)

    • @keerthanareddy8856
      @keerthanareddy8856 3 роки тому

      @@labdhogranagasamudram4364 would you pls explain the procedure🙂!

    • @labdhogranagasamudram4364
      @labdhogranagasamudram4364 3 роки тому +1

      @@keerthanareddy8856
      Actually I'm not able to txt the compilation process here😅
      The value changes according to the nxt =...
      Or else if u want u can mail me I'll send the pic of my compiled one

    • @labdhogranagasamudram4364
      @labdhogranagasamudram4364 3 роки тому +1

      @@keerthanareddy8856 actually sry for late😂

    • @keerthanareddy8856
      @keerthanareddy8856 3 роки тому +1

      @@labdhogranagasamudram4364 ur mail I'd??

  • @NarutoUzumaki-vs8hc
    @NarutoUzumaki-vs8hc 3 роки тому

    Well explained 👏 👌

  • @madhavarora1641
    @madhavarora1641 4 роки тому

    This a+b method or XOR method works for only integers. How to do it for strings?
    I have got one way using lists without using third variable and python way of a,b=b,a but want to understand from others too.
    @Naveen Sir, Please guide me on this.

  • @ayushjaiswal1825
    @ayushjaiswal1825 2 роки тому

    Unbelievable it's all free.

  • @marthal3kab311
    @marthal3kab311 Рік тому

    You are amazing🙌🙌

  • @prakashregmi889
    @prakashregmi889 5 років тому +1

    when you do (a,b) = (b,a), wont the value of b be updated resulting on getting the same value again? or is it working because we have applied it in same line so that the value of a swaps with b and with out updating the original value of b, a gets the original value of b. is it how its working? -_-

  • @yashisingh2204
    @yashisingh2204 3 роки тому

    my first python project

  • @bramleenkaur
    @bramleenkaur 4 роки тому

    the concept in this video was quit difficult because you did explain,,,,,, but not deeply.......

  • @krishj8011
    @krishj8011 4 роки тому

    thanks...very useful...

  • @mdabusharif3842
    @mdabusharif3842 5 років тому +7

    Sir add game tutorial as well...it will cover more area..also it will be fun to learn..!!!!

    • @II_xD_II
      @II_xD_II 5 років тому +1

      not for class 2 -_- even im in 8th and hate that idea

  • @AMiTSingh-th6pt
    @AMiTSingh-th6pt 4 роки тому +5

    But XOR also performs the same function as addition in the first line of formula that means it adds a and b. So , how it uses less number of bits. Can you explain

    • @muhammadseyan8361
      @muhammadseyan8361 2 роки тому

      it simply does not perform the same operation. search for XOR swap in wikipedia

  • @cosmicwarriorx1
    @cosmicwarriorx1 4 роки тому +1

    Navin can you give quick tutorial to switch to pycharm. thanks

  • @dipak002
    @dipak002 4 роки тому +3

    Hey Navin, another great video! I just do not understand why you did not speak two lines about XOR(exclusive OR) which is like a binary operation involving three bits only for your example.

  • @Hmmmmm651
    @Hmmmmm651 Рік тому

    Thanks sir for this information🙏

  • @alienalien2612
    @alienalien2612 3 роки тому +1

    Thanks Sir🤩

  • @swethanooka896
    @swethanooka896 4 роки тому

    Could you plz explain about stack and quea in detail?

  • @praveenagrawal3035
    @praveenagrawal3035 4 роки тому

    smart one!!

  • @vinothasornavel9523
    @vinothasornavel9523 4 роки тому

    what is the use of programming in pycharm ??? We can also type this program in python also.... then for what we are using pycharm. We can use python itself for this?? Or we have to do in pycharm

  • @kkartheek4378
    @kkartheek4378 3 роки тому +5

    print ("ANOTHER METHOD FOR SWAPPING TWO VARIABLES IN PYTHON")
    a=5
    b=6
    a=a*b # a = 5 * 6= 30 a : 30
    b=a/b # b=30 / 6= 5 b : 5
    a=a/b # a=30 / 5=6 a : 6
    print(a)
    print(b)
    Output:
    6
    5

  • @anushaganji6395
    @anushaganji6395 4 роки тому +8

    Hi Naveen, could you please show us to use "temp" for swapping 3 variables

    • @rahulpareek7450
      @rahulpareek7450 4 роки тому +5

      It depend on you then in what manner you want to swap....
      If a=1
      b = 2
      c= 3
      For a=2, b=3 , c= 1....
      Then lets swap as follows:
      temp = a
      a= b
      b = c
      c= temp.....
      Similarly in any manner you can do ....

    • @moodybaalika3102
      @moodybaalika3102 4 роки тому

      @@rahulpareek7450 How can we use the formula for three variables??

    • @ritikaratan3195
      @ritikaratan3195 3 роки тому

      @@rahulpareek7450 ya this is right we can swap the variables this way.

    • @ritikaratan3195
      @ritikaratan3195 3 роки тому

      @@moodybaalika3102 a=1,b=2,c=3
      a=a+b+c
      b=a-b-c
      c=a-b-c
      a=a-b-c
      You will get a=3,b=2,c=1

  • @dishanamdev2620
    @dishanamdev2620 3 роки тому

    Sir please make a video on how ROT_TWO works.

  • @ysabhishekmishra
    @ysabhishekmishra 2 роки тому

    import numpy as np
    a=3
    b=6
    temp=a
    a=b
    b=temp
    print(a,b)

  • @rufashprince7794
    @rufashprince7794 2 роки тому

    sir i did understand this video where did you do the coding

  • @jonsng7
    @jonsng7 Рік тому

    thank you very much!

  • @sureshhh8082
    @sureshhh8082 3 роки тому

    hey buddies...start working on juypter notebook...its the simplest❤..As a complete beginner i am quite comfortable with it.

  • @ALTGamer525
    @ALTGamer525 3 роки тому

    Thank you very much

  • @054_ritesh3
    @054_ritesh3 3 роки тому +1

    very very good

  • @harshivdesai8089
    @harshivdesai8089 4 роки тому

    Can we swap two no. By multiplying and dividing as it is in 'c' ??

  • @syangurmeet1
    @syangurmeet1 4 роки тому

    Need to know xor in detail and how to use exactly here.

  • @pgkgaming4229
    @pgkgaming4229 6 місяців тому

    nice explanation