Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Wednesday, October 25, 2017

Adventures in Python: User-Entry Function

Adventures in Python:  User-Entry Function

The following script allows the user to enter a function, including math functions (math.sqrt, math.sin, etc).  The function is entered as string and when calculation is needed, the eval function is called. 

Script:

# Program 010:  Asking for a function - Using Eval
# To generate a table

# allow for math functions
import math


# ask for the parameters
print('Generate f(x): You can use math. functions')
print('Such as math.sqrt, math.sin, math.exp, etc.')
print('Remember the default angle measure is radians.')
print('Use math.pi for \u03C0')
# f(x) and parameters will be entered as a string
fx = input('Please enter f(x): ')
# use float for end marks
beg = input('Beginning point:  ')
end = input('Ending point:  ')
step = input('Step: ')
# use eval to evaluate the parameters, turning them into floating numbers
beg = eval(beg)
end = eval(end)
step = eval(step)


print('BEGIN')

# since a floating number with begin, end, and step may be used,
# we must use a while loop instead of a for loop
k = beg
while k<=end:
    # function evaluation
    x = k
    y = eval(fx)
    #  print the data to six decimal places
    x = round(x,6)
    y = round(y,6)
    print(x,',',y)
    print('-----')
    # use += for storage arithmetic
    k += step

# end of table
print('END')

Example:

f(x) = √(x^2 + 1) from x = 0 to x = 4, step 0.5

Result:

Generate f(x): You can use math. functions
Such as math.sqrt, math.sin, math.exp, etc.
Remember the default angle measure is radians.
Use math.pi for π
Please enter f(x): math.sqrt(x**2+1)
Beginning point:  0
Ending point:  4
Step: 0.5
BEGIN
0 , 1.0
-----
0.5 , 1.118034
-----
1.0 , 1.414214
-----
1.5 , 1.802776
-----
2.0 , 2.236068
-----
2.5 , 2.692582
-----
3.0 , 3.162278
-----
3.5 , 3.640055
-----
4.0 , 4.123106
-----
END

Pretty straight forward.  I hope you are enjoying this series on Python.  Until next time,

Eddie


This blog is property of Edward Shore, 2017

Thursday, October 19, 2017

Adventures in Python: String Manipulation and Function/Derivative Table (Subroutines)

Adventures in Python:  String Manipulation and Function/Derivative Table (Subroutines)

String Manipulation

This script demonstrates several string functions:

length:  the number of characters in a string

stringname.replace(‘target string’,‘new string’):  replace a part of a string

stringname.capitalize(): capitalize the first letter of a string

stringname.split():  splits a string at its spaces

Other string functions:

stringname.lower(): makes all letters of a string to lower case

stringname.upper():  makes all letters of a string to upper case

You can join strings with the plus symbol ( + ).

The nice thing is that you won’t need to import any modules for the above string functions.

Script:

# program 008
print('Program 008: String Demo')
str1 = input('Please type some text.  ')
# input defaults to strings
# length
print('Length')
l = len(str1)
print('Length',l)
# capitalizes the first letter
print('Capitalize')
str2 = str1.capitalize()
print(str2)
# string splits at spaces
print('Split')
str3 = str1.split()
print(str3)
# replacement
str4 = str1
str4 = str4.replace('a','e')
str4 = str4.replace('u','a')
str4 = str4.replace('o','u')
str4 = str4.replace('i','o')
str4 = str4.replace('e','i')
print('Can you read this?')
print(str4)

Output:  with the “This is my demonstration.”

Program 008: String Demo
Please type some text.  This is my demonstration.
Length
Length 25
Capitalize
This is my demonstration.
Split
['This', 'is', 'my', 'demonstration.']
Can you read this?
Thos os my dimunstritoun.


Function/Derivative Table (Subroutines)

You can create user functions (subroutines) in python by the def structure.  You return results by the return command.

Script (you can enter whatever your function you like, I have imported the math module):

# Program 009 Using def and creating a table
# import math
import math

def fx(x):
     f = math.exp(-x**2/2)
     return f;
    
print('Function Table')
beg = float(input('start: '))
end = float(input('stop: '))
stp = float(input('step: '))

# loop begins
h = .00001
k = beg
print('k',':','f(x)',';','slope')
while k <= end:
     # function
     a = fx(k)
     # derivative
     b = fx(k+h)
     c = fx(k-h)
     d = (b - c)/(2 * h)
     print(k,',',a,',',d)
     print('- - - - - - - -')
     k = k + stp
           

Output (with start = 0, stop = 5, step = 1)

Function Table
start: 0
stop: 5
step: 1
k : f(x) ; slope
0.0 , 1.0 , 0.0
- - - - - - - -
1.0 , 0.6065306597126334 , -0.6065306596914066
- - - - - - - -
2.0 , 0.1353352832366127 , -0.27067056647955834
- - - - - - - -
3.0 , 0.011108996538242306 , -0.033326989618259056
- - - - - - - -
4.0 , 0.00033546262790251185 , -0.0013418505118650097
- - - - - - - -
5.0 , 3.726653172078671e-06 , -1.8633265866508763e-05
- - - - - - - -

Eddie


This blog is property of Edward Shore, 2017.

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues The programs are listed for the Swiss Micros DM42 an...