Posts

Showing posts from July, 2023

parameters

n the above example, name and age are positional parameters. When calling the greet function, the value "Alice" is passed for name and 25 is passed for age  parameters and it's types   In Python, parameters refer to the variables that are defined in a function or method declaration. They represent the input values that are passed to a function when it is called. Parameters allow you to define the behavior and functionality of the function based on the values that are provided during its invocation. In Python, there are several types of parameters that can be used: Positional Parameters: Default Parameters Keyword Parameters Variable-Length Parameters: Positional Parameters: These are the most common type of parameters and are defined by their position in the function declaration. When calling the function, the values are passed in the same order as the parameter declaration. For example: def greet(name, age): print("Hello...

RETURN VALUE

RETURN VALUE In Python, you can return values from functions using the return statement. The return statement allows you to send a value back to the caller of the function. Here's an example code that demonstrates returning values in Python: distance between two points python code def add_numbers(a, b): return a + b result = add_numbers(5, 3) print(result) # Output: 8 In the above example, we have a function named add_numbers that takes two arguments a and b . Inside the function, we use the return statement to send back the sum of a and b . When we call the add_numbers function with 5 and 3 , it returns the sum 8 . We store the returned value in the result variable and then print it to the console. Here's another example that demonstrates returning multiple values from a function: def get_name_length(name): length = len(name) is_long = length > 5 return length, is_long name_length, is_name_long = get_name_l...

DISTANCE BETWEEN TWO POINT

distance between two points python code import math def calculate_distance(x1, y1, x2, y2): distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) return distance # Example usage: x1 = 1 y1 = 2 x2 = 4 y2 = 6 distance = calculate_distance(x1, y1, x2, y2) print("The distance between the two points is:", distance) OUTPUT: The distance between the two points is: 5.0 In this code, the calculate_distance() function takes the coordinates of two points (x1, y1) and (x2, y2) as input parameters. It uses the distance formula to calculate the distance and returns the result. The math.sqrt() function from the math module is used to calculate the square root of the expression (x2 - x1) ** 2 + (y2 - y1) ** 2. Finally, an example usage is provided, where the coordinates of the two points are assigned to variables x1, y1, x2, and y2. The calculated distance is then printed to the console.

To circulate the values

  To circulate the values To circulate the values of multiple variables n , you can shift their values in a circular manner. This means that the value of each variable will be assigned to the next variable, while the last variable's value will be assigned to the first variable CODE: # Number of variables n = 4 # Initial values values = [10, 20, 30, 40] # Example values # Circulate the values temp = values[n - 1] for i in range(n - 1, 0, -1): values[i] = values[i - 1] values[0] = temp # Print the circulated values for i in range(n): print(f"values[{i}] =", values[i]) OUTPUT: values[0] = 40 values[1] = 10 values[2] = 20 values[3] = 30

SWAPPING

SWAPPING   Swapping, in the context of programming, refers to the process of interchanging the values of two variables. It involves taking the value of one variable and assigning it to the other variable, while simultaneously taking the value of the second variable and assigning it to the first variable. As a result, the original values of the variables are exchanged. Swapping is commonly used in programming when there is a need to rearrange or manipulate data between two variables. It can be done using a temporary variable to hold one of the values temporarily, or by utilizing language-specific techniques that allow direct swapping without the need for an intermediary variable. Swapping variables is useful in various scenarios, such as sorting algorithms, data transformations, or when you need to update variable assignments based on changing conditions. In Python, swapping refers to exchanging the values of two variables. Swapping allows you to interchange the values stored ...

The Fibonacci series

  The Fibonacci series The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. So, the sequence begins as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. Mathematically, the Fibonacci sequence can be defined recursively as follows: F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) (for n > 1) Here, F(n) represents the nth number in the Fibonacci sequence. The Fibonacci sequence has a fascinating property where each number in the sequence is the sum of the two preceding numbers. This pattern continues indefinitely, creating an infinite sequence of numbers. The Fibonacci sequence has numerous applications in various fields, including mathematics, computer science, nature, and finance. It appears in nature in phenomena such as the arrangement of leaves on a stem, the branching of trees, and the spiral patterns found in seashells. In computer science, Fibonacci numbers are used in algorithms, especiall...

Fibonacci Series

CODE: def fibonacci(n):     if n <= 0:         return []     elif n == 1:         return [0]     elif n == 2:         return [0, 1]     else:         fib_series = [0, 1]         for i in range(2, n):             fib_series.append(fib_series[i - 1] + fib_series[i - 2])         return fib_series num_terms = int(input("Enter the number of Fibonacci terms: ")) fib_sequence = fibonacci(num_terms) print("Fibonacci Sequence:", fib_sequence)    OUTPUT :                                          Enter the number of Fibonacci terms: 25                                          Fibo...

ILLUSTRATIVE PROGRAMS IN PYTHON

1  Hello, World! Program: print ( "Hello, World!" ) This program simply prints the message "Hello, World!" to the console. 2 Simple Calculator: def add ( a, b ): return a + b def subtract ( a, b ): return a - b def multiply ( a, b ): return a * b def divide ( a, b ): return a / b num1 = float ( input ( "Enter the first number: " )) num2 = float ( input ( "Enter the second number: " )) print ( "Sum:" , add(num1, num2)) print ( "Difference:" , subtract(num1, num2)) print ( "Product:" , multiply(num1, num2)) print ( "Quotient:" , divide(num1, num2)) This program performs basic arithmetic operations (addition, subtraction, multiplication, and division) on two numbers provided by the user.