0% found this document useful (0 votes)
9 views

Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Aim

To write a program that swaps the values of two variables without using a
temporary variable.
Description
Swapping two numbers is a common problem in programming. Traditionally, a
temporary variable is used to facilitate the swap. However, there are
alternative methods that don't require extra space. This program demonstrates
how to swap two integers using arithmetic operations (addition and
subtraction) and bitwise XOR operations, both of which avoid using a
temporary variable.
Source Code
Here is the code for both methods in Python:

# Function to swap two numbers using arithmetic operations


def swap_arithmetic(a, b):
print(f"Original values: a = {a}, b = {b}")
a=a+b
b=a-b
a=a-b
print(f"Swapped values: a = {a}, b = {b}")

# Example usage
x=5
y = 10
swap_arithmetic(x, y)
output
Original values: a = 5, b = 10
Swapped values: a = 10, b = 5
4. Arithmetic Operators
Aim
To demonstrate the usage of arithmetic operators in Python for performing
basic mathematical operations.
Description
Arithmetic operators are used to perform arithmetic operations such as
addition, subtraction, multiplication, etc. This section illustrates how to use
these operators to compute various mathematical results.
Source Code
# Define two numbers
a = 10
b=3

# Addition
addition = a + b
print(f"Addition: {a} + {b} = {addition}")

# Subtraction
subtraction = a - b
print(f"Subtraction: {a} - {b} = {subtraction}")

# Multiplication
multiplication = a * b
print(f"Multiplication: {a} * {b} = {multiplication}")

# Division
division = a / b
print(f"Division: {a} / {b} = {division}")

# Modulus
modulus = a % b
print(f"Modulus: {a} % {b} = {modulus}")

# Exponentiation
exponentiation = a ** b
print(f"Exponentiation: {a} ** {b} = {exponentiation}")

# Floor Division
floor_division = a // b
print(f"Floor Division: {a} // {b} = {floor_division}")

output
Addition: 10 + 3 = 13
Subtraction: 10 - 3 = 7
Multiplication: 10 * 3 = 30
Division: 10 / 3 = 3.3333333333333335
Modulus: 10 % 3 = 1
Exponentiation: 10 ** 3 = 1000
Floor Division: 10 // 3 = 3
2. Relational Operators
Aim
To illustrate the use of relational operators for comparing values and returning
boolean results.
Description
Relational operators are used to compare two values. They return a boolean
result (True or False) based on the comparison. This section shows how these
operators work in different scenarios.

Source Code

# Define two numbers


x = 10
y = 20

# Equal to
print(f"{x} == {y} : {x == y}")

# Not equal to
print(f"{x} != {y} : {x != y}")

# Greater than
print(f"{x} > {y} : {x > y}")

# Less than
print(f"{x} < {y} : {x < y}")

# Greater than or equal to


print(f"{x} >= {y} : {x >= y}")
# Less than or equal to
print(f"{x} <= {y} : {x <= y}")

Output
10 == 20 : False
10 != 20 : True
10 > 20 : False
10 < 20 : True
10 >= 20 : False
10 <= 20 : True

3. Assignment Operators
Aim
To demonstrate how assignment operators work for assigning and modifying
values of variables.
Description
Assignment operators are used to assign values to variables and can also be
used to modify those values. This section shows how each assignment operator
updates the value of a variable.
Source Code
# Define a variable
num = 10

# Add and assign


num += 5
print(f"After += 5: {num}")
# Subtract and assign
num -= 3
print(f"After -= 3: {num}")

# Multiply and assign


num *= 2
print(f"After *= 2: {num}")

# Divide and assign


num /= 4
print(f"After /= 4: {num}")

# Modulus and assign


num %= 3
print(f"After %= 3: {num}")

# Exponentiation and assign


num **= 2
print(f"After **= 2: {num}")

# Floor division and assign


num //= 5
print(f"After //= 5: {num}")

Output
After += 5: 15
After -= 3: 12
After *= 2: 24
After /= 4: 6.0
After %= 3: 0.0
After **= 2: 0.0
After //= 5: 0.0

You might also like