• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Pro Programming

Professional way of Programming: Learn C, C++, Java, Python, Dot Net, Android the professional way

  • Home
  • C MCQs
  • C/C++ Programs
  • Java Programs
  • C#
  • Python
  • MySQL
  • Topics
    • Arrays
    • Strings
    • Link Lists
    • Trees
    • Shapes
  • Projects
  • Articles
  • Games
You are here: Home / Archives for Python

Python

Program to swap two numbers in Python

Leave a Comment

Today we will write a program to swap two numbers in Python, we have two ways to do it:

  1. Swap using temporary variable.
  2. Swap without using temporary variable.

 

Program to swap two numbers in Python using temporary variable:

a = 10
b = 20
print("before swapping\na=", a, " b=", b)
temp = a
a = b
b = temp
print("\nafter swapping\na=", a, " b=", b)

OUTPUT:

before swapping
a= 10 b= 20
after swapping
a= 20 b= 10

 

Program to swap two numbers in Python without using temporary variable:

Method 1:

Python provides a way to directly swap two number without temporary variable. It can be done in following way.

a, b = b, a

 

Method 2:

In this method we can use addition and subtraction operators.

a = a + b
b = a - b
a = a - b

Method 3:

We can also swap numbers using multiplication and division operator in following way.

a = a * b
b = a / b
a = a / b
This method will not work when one of the number is 0.

 

Method 4:

It is another method in which we use bitwise xor operator.

a = a ^ b
b = a ^ b
a = a ^ b

 

 

Comment below for any help.

Filed Under: Python

Program to find largest among three numbers in Python

Leave a Comment

Today we are going the write one of the simplest program in Python to find largest among three numbers.

Here, we will input three numbers and our program will find the largest of them all.

 

Program to find largest among three numbers in Python:

print("enter three numbers:")
a = int(input())
b = int(input())
c = int(input())
if a>b and a>c:
    print(a, " is largest")
elif b>a and b>c:
    print(b, " is largest")
else:
    print(c, " is largest")

 

OUTPUT:

enter three numbers:
9
2
12
12 is largest

 

That’s our quick program, need help then comment below.

Filed Under: Python

  • « Go to Previous Page
  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4

Primary Sidebar

Recent Posts

  • UPSC Civil Services Question Paper 2015
  • UPSC Civil Services Question Paper 2014
  • Total Quality Management Questions and Answers – Agile Manufacturing
  • How to Become a Cyber Security Engineer?
  • How to Become a Software Architect?
  • Privacy Policy
  • About
  • Contact US

© 2020 ProProgramming Privacy Policy About Contact Us