Python Programs Demonstrating Expressions and Built-in Functions
Next Topic(s):
Created:
12th of August 2024
09:07:25 AM
Modified:
3rd of October 2024
07:12:02 AM
Python Programs Demonstrating Expressions and Built-in Functions
The following programs illustrate various aspects of expressions and built-in functions in Python, providing practical examples to help you understand how to use them effectively in your programming tasks.
Program 1: Simple Addition
Problem: Write a program simple_addition.py
that adds two numbers and prints the result.
# Program 1: Simple Addition
# Use simple_addition.py
x = 3
y = 5
result = x + y
print(f"The result of {x} + {y} is: {result}")
Explanation: This program adds two numbers and prints the result using a simple expression.
Sample Output:
c:\demo>python simple_addition.py
The result of 3 + 5 is: 8
c:\demo>
Program 2: Subtraction of Variables
Problem: Write a program variable_subtraction.py
that subtracts one variable from another and prints the result.
# Program 2: Subtraction of Variables
# Use variable_subtraction.py
x = 10
y = 3
result = x - y
print(f"The result of {x} - {y} is: {result}")
Explanation: This program demonstrates how to subtract one variable from another and print the result.
Sample Output:
c:\demo>python variable_subtraction.py
The result of 10 - 3 is: 7
c:\demo>
Program 3: Multiplication of Variables
Problem: Write a program variable_multiplication.py
that multiplies two variables and prints the result.
# Program 3: Multiplication of Variables
# Use variable_multiplication.py
x = 4
y = 5
result = x * y
print(f"The result of {x} * {y} is: {result}")
Explanation: This program multiplies two variables and prints the result.
Sample Output:
c:\demo>python variable_multiplication.py
The result of 4 * 5 is: 20
c:\demo>
Program 4: Division of Variables
Problem: Write a program variable_division.py
that divides one variable by another and prints the result.
# Program 4: Division of Variables
# Use variable_division.py
x = 20
y = 4
result = x / y
print(f"The result of {x} / {y} is: {result}")
Explanation: This program divides one variable by another and prints the result.
Sample Output:
c:\demo>python variable_division.py
The result of 20 / 4 is: 5.0
c:\demo>
Program 5: Exponentiation
Problem: Write a program exponentiation.py
that raises one variable to the power of another and prints the result.
# Program 5: Exponentiation
# Use exponentiation.py
x = 2
y = 3
result = x ** y
print(f"The result of {x} ** {y} is: {result}")
Explanation: This program demonstrates exponentiation, raising one number to the power of another.
Sample Output:
c:\demo>python exponentiation.py
The result of 2 ** 3 is: 8
c:\demo>
Program 6: Absolute Value and Maximum
Problem: Write a program abs_max_example.py
that calculates the absolute value of a number and finds the maximum of two numbers, then prints the results.
# Program 6: Absolute Value and Maximum
# Use abs_max_example.py
x = -5
y = 7
z = 2
result = abs(x) + max(y, z)
print(f"The result of abs({x}) + max({y}, {z}) is: {result}")
Explanation: This program demonstrates how to use the abs()
function to get the absolute value and max()
to find the maximum value.
Sample Output:
c:\demo>python abs_max_example.py
The result of abs(-5) + max(7, 2) is: 12
c:\demo>
Program 7: Multiplication and Subtraction with Division
Problem: Write a program complex_expression.py
that multiplies two numbers, divides another two, subtracts the result of the division from the multiplication, and prints the result.
# Program 7: Multiplication and Subtraction with Division
# Use complex_expression.py
x = 6
y = 4
a = 20
b = 5
result = (x * y) - (a / b)
print(f"The result of ({x} * {y}) - ({a} / {b}) is: {result}")
Explanation: This program combines multiplication, division, and subtraction in a single expression. Observe the BODMAS rules are applied by the software also. Operator Precedence is a very important concept to be clear and through with.
Sample Output:
c:\demo>python complex_expression.py
The result of (6 * 4) - (20 / 5) is: 14.0
c:\demo>
Program 8: Using the len()
Function
Problem: Write a program length_of_string.py
that calculates the length of a string and prints it.
# Program 8: Using the len() Function
# Use length_of_string.py
my_string = "Python"
length = len(my_string)
print(f"The length of the string '{my_string}' is: {length}")
Explanation: This program uses the len()
function to calculate and print the length of a string.
Sample Output:
c:\demo>python length_of_string.py
The length of the string 'Python' is: 6
c:\demo>
Program 9: Finding the Minimum Value
Problem: Write a program find_minimum.py
that finds the minimum value among a list of numbers and prints it.
# Program 9: Finding the Minimum Value
# Use find_minimum.py
numbers = [1, 3, 2]
minimum = min(numbers)
print(f"The minimum value in {numbers} is: {minimum}")
Explanation: This program demonstrates how to find and print the minimum value in a list of numbers.
Sample Output:
c:\demo>python find_minimum.py
The minimum value in [1, 3, 2] is: 1
c:\demo>
Program 10: Summing a List of Numbers
Problem: Write a program sum_numbers.py
that calculates the sum of a list of numbers and prints it.
# Program 10: Summing a List of Numbers
# Use sum_numbers.py
numbers = [1, 2, 3, 4]
total = sum(numbers)
print(f"The sum of {numbers} is: {total}")
Explanation: This program uses the sum()
function to calculate and print the sum of a list of numbers.
Sample Output:
c:\demo>python sum_numbers.py
The sum of [1, 2, 3, 4] is: 10
c:\demo>
Key Takeaway
These programs illustrate the use of various expressions and built-in functions in Python. By experimenting with these examples, you can develop a deeper understanding of how to perform common operations and manipulate data effectively in your own Python programs.