phython codes
phython codes
return num1
return num2
else:
return num3
python
Copy code
# Function to check if a number is prime
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
Example Usage:
Input:
mathematica
Copy code
Enter the starting number of the interval: 10
Enter the ending number of the interval: 30
Output:
sql
Copy code
Prime numbers between 10 and 30 are:
11
13
17
19
23
29
Here’s a Python program to swap two numbers without using a temporary variable, along
with an example:
python
Copy code
# Function to swap two numbers
def swap_numbers(a, b):
print(f"Before swapping: a = {a}, b = {b}")
a = a + b # Step 1: Sum both numbers
b = a - b # Step 2: Subtract the new b from the sum to get the
original a
a = a - b # Step 3: Subtract the new b from the sum to get the
original b
print(f"After swapping: a = {a}, b = {b}")
Example Usage:
Input:
mathematica
Copy code
Enter the first number: 5
Enter the second number: 10
Output:
less
Copy code
Before swapping: a = 5.0, b = 10.0
After swapping: a = 10.0, b = 5.0
Explanation:
The program takes two numbers (5 and 10) from the user.
It then swaps them using arithmetic operations without a temporary variable and
prints the results before and after the swap.
Certainly! Here’s a demonstration of various operators in Python with suitable examples:
i) Arithmetic Operators
v) Bitwise Operators
python
Copy code
a = 5
b = 3
max_value = a if a > b else b
print(max_value) # Output: 5
i) Arithmetic Operators
python
Copy code
# Prices of items
apple_price = 2.50
banana_price = 1.20
total_price = apple_price + banana_price # Total cost
print("Total Price:", total_price) # Output: 3.70
python
Copy code
age = 18
is_eligible = age >= 18 # Check if eligible to vote
print("Is eligible to vote:", is_eligible) # Output: True
python
Copy code
balance = 1000
balance += 500 # Deposit
print("Balance after deposit:", balance) # Output: 1500
balance -= 200 # Withdrawal
print("Balance after withdrawal:", balance) # Output: 1300
python
Copy code
username = "user"
password = "pass"
is_authenticated = (username == "user") and (password == "pass")
print("Is authenticated:", is_authenticated) # Output: True
v) Bitwise Operators
python
Copy code
a = 5 # 0101 in binary
b = 3 # 0011 in binary
print("Bitwise AND:", a & b) # Output: 1 (0001 in binary)
vi) Ternary Operator
python
Copy code
marks = 85
grade = "Pass" if marks >= 50 else "Fail"
print("Grade:", grade) # Output: Pass
python
Copy code
inventory = ["apple", "banana", "orange"]
is_in_stock = "banana" in inventory
print("Is banana in stock?", is_in_stock) # Output: True
python
Copy code
a = [1, 2, 3]
b = a # b points to the same list as a
c = [1, 2, 3] # c is a different object
python
Copy code
# Function to add two complex numbers
def add_complex(num1, num2):
return num1 + num2
Example Usage:
Input:
c
Copy code
Enter the first complex number (e.g., 2+3j): 2+3j
Enter the second complex number (e.g., 1+4j): 1+4j
Output:
python
Copy code
The sum of (2+3j) and (1+4j) is: (3+7j)
The product of (2+3j) and (1+4j) is: (-10+11j)
Explanation:
python
Copy code
# Function to print multiplication table
def print_multiplication_table(num):
print(f"Multiplication Table for {num}:")
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Example Usage:
Input:
css
Copy code
Enter a number to print its multiplication table: 5
Output:
css
Copy code
Multiplication Table for 5:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Explanation:
The program takes a number from the user and prints its multiplication table from 1 to
10.
Here’s a Python program that defines a function with multiple return values, using a real-
world example of calculating the area and perimeter of a rectangle.
python
Copy code
# Function to calculate area and perimeter of a rectangle
def rectangle_properties(length, width):
area = length * width
perimeter = 2 * (length + width)
return area, perimeter # Returning both area and perimeter
Example Usage:
Input:
mathematica
Copy code
Enter the length of the rectangle: 5
Enter the width of the rectangle: 3
Output:
mathematica
Copy code
Area of the rectangle: 15.0
Perimeter of the rectangle: 16.0
Explanation:
The function rectangle_properties takes the length and width of a rectangle and
returns both the area and perimeter.
The program captures the user input and calls the function, unpacking the returned
values into area and perimeter, which are then printed.
height = float(input("Enter the height of the box (or press Enter to use
default): ") or 5)
Enter the height of the box (or press Enter to use default): 2
Here’s a Python program to find the length of a string without using any library functions:
python
Copy code
# Function to calculate the length of a string
def string_length(input_string):
count = 0
for char in input_string:
count += 1 # Increment count for each character
return count
Example Usage:
Input:
c
Copy code
Enter a string: Hello, World!
Output:
csharp
Copy code
The length of the string is: 13
Explanation:
The string_length function iterates through each character in the input string and
increments a counter for each character.
Finally, it returns the count, which represents the length of the string.
python
Copy code
# Function to check if a substring is present in the given string
def is_substring_present(main_string, substring):
# Iterate through the main string
for i in range(len(main_string) - len(substring) + 1):
# Check if the substring matches
if main_string[i:i + len(substring)] == substring:
return True
return False
Example Usage:
Input:
mathematica
Copy code
Enter the main string: Hello, World!
Enter the substring to check: World
Output:
csharp
Copy code
The substring 'World' is present in the main string.
Explanation:
Here’s a Python program that demonstrates addition, insertion, and slicing operations on a list
with examples:
python
Copy code
# Initialize an empty list
my_list = []
# Example operations
# Adding elements
add_elements([1, 2, 3, 4, 5])
print("List after addition:", my_list) # Output: [1, 2, 3, 4, 5]
# Inserting an element
insert_element(2, 10) # Insert 10 at index 2
print("List after insertion:", my_list) # Output: [1, 2, 10, 3, 4, 5]
Example Usage:
1. Adding elements:
o Adding [1, 2, 3, 4, 5] to the list results in:
less
Copy code
List after addition: [1, 2, 3, 4, 5]
2. Inserting an element:
o Inserting 10 at index 2 results in:
less
Copy code
List after insertion: [1, 2, 10, 3, 4, 5]
less
Copy code
Sliced list: [2, 10, 3]
Here’s a Python program that demonstrates five built-in list functions using a sample list:
python
Copy code
# Sample list
my_list = [10, 20, 30, 40, 50]
Example Output:
less
Copy code
After append: [10, 20, 30, 40, 50, 60]
After insertion: [10, 20, 25, 30, 40, 50, 60]
After removal: [10, 20, 25, 40, 50, 60]
After sorting: [10, 20, 25, 40, 50, 60]
After reversing: [60, 50, 40, 25, 20, 10]