You are not logged in.

addiesam   AE Family   Offline   #1 2019-12-05 00:58:43

4 - Programming Basic and Python

The official Python 3.7 programming doc:

https://docs.python.org/3.7/


The official Python tutorial:

https://docs.python.org/3/tutorial/index.html

addiesam   AE Family   Offline   #2 2019-12-14 10:16:21

Re: 4 - Programming Basic and Python

Programming 6 Cores:

1. Variable Assignment
2. If Condition
3. Looping
4. Array, List and Mapping
5. Define a Function
6. OO Concept (Object Oriented Concept)

addiesam   AE Family   Offline   #3 2019-12-15 05:52:21

Re: 4 - Programming Basic and Python

1. Variable assignment

1.1. Dynamic Type

Variables hold values. In Python, variables do not require forward declaration - all you need to do is provide a variable name and assign it some value.


Example 1

Python Code:

x = 100
print (x)

Output:

100


Example 2

Python Code:

x = 100
x = abc
print (x)

Output:

abc


Example 3

Python Code:

x = 100
y = 200
print (x+y)

Output:

300

addiesam   AE Family   Offline   #4 2019-12-15 06:05:30

Re: 4 - Programming Basic and Python

1.2. Problem in Double Precision

https://en.wikipedia.org/wiki/Double-pr … int_format


Example 4

Python Code:

x=2.64

x=x + 0.2
print(x)

x=x + 0.2
print(x)

Output:

2.8400000000000003
3.0400000000000005

Remark:
In binary 2.64 is 10.10100011110101110000101000111101... recurring, in other words not exactly presentable in binary, hence the small error.  Similar idea to decimal value $\frac{1}{3} = 0.33333333333333333$.

It is not acceptable for calculation of money.

addiesam   AE Family   Offline   #5 2019-12-15 07:02:10

Re: 4 - Programming Basic and Python

1.3. "mpmath" Library for Accurate Precision

It is good thing that mpmath is well setup on the Anaconda environment.  It can be invoked directly without any extra setup.


mpmath documentation:
http://w.tkolp.com/data/addiesam/tutori … latest.pdf


Example 5

Python Code:

from mpmath import *

x = mpf(2.64 + 0.2)
y = 2.64 + 0.2
print(x)
print(y)

Output:

2.84
2.8400000000000003


Example 6

Python Code:

from mpmath import *

mp.dps = 3
x = mpf(2.64 + 0.01)
print (x)

mp.dps = 2
x = mpf(2.64 + 0.01)
print (x)

mp.dps = 1
x = mpf(2.64 + 0.01)
print (x)

Output:

2.65
2.6
3.0


Remark:
"mp.dps" defines the decimal point.  Value will be rounded up.


Example 7

Python Code:

from mpmath import *

mp.prec = 100
mp.dps = 333
print(mp)

print(pi)

Output:

Mpmath settings:
  mp.prec = 1110              [default: 53]
  mp.dps = 333                [default: 15]
  mp.trap_complex = False     [default: False]
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096

Remark:
The term "prec" denotes the binary precision (measured in bits) while "dps"(short fordecimal places) is the decimal precision.  Binary and decimal precision are related roughly according to the formula prec = 3.33*dps.   For example, it takes a precision of roughly 333 bits to hold an approximation of pi that is accurate to 100 decimal places (actually slightly more than 333 bits is used).

addiesam   AE Family   Offline   #6 2019-12-15 07:39:45

Re: 4 - Programming Basic and Python

2. If Condition

This is logic.


Example 1

Python Code:

x=100

if (x < 500):
    print ('Hello')

Output:

500


Example 2

Python Code:

x=100

if (x >= 500):
    print ('Hello')

Output:

$\,$

Remark:
Nothing is printed out because the condition is not met.


Example 3

Python Code:

x=100
y=300

if (x < 500) and (y <= 300):
    print ('Hello')

Output:

Hello


Example 4

Python Code:

x=100
y=300

if (x >= 500) or (y <= 300):
    print ('Hello')

Output:

Hello


Example 5

Python Code:

x=100

if (x < 50):
    print ('Hello 1')
elif (x < 200):
    print ('Hello 2')
elif (x < 500):
    print ('Hello 3')
else:
    print ('Hello 4')

Output:

Hello 2

Remark:
Although x is less than 200 and 500, the if-then-else statment stops when the first condition is met.  The output is "Hello 2" only but "Hello 2" and "Hello 3".


Example 6

Python Code:

x=800

if (x < 50):
    print ('Hello 1')
elif (x < 200):
    print ('Hello 2')
elif (x < 500):
    print ('Hello 3')
else:
    print ('Hello 4')

Output:

Hello 4


Example 7

Python Code:

x=100

if (x < 500):
    print ('Hello 1')
    print ('Hello 2')
    print ('Hello 3')

Output:

Hello 1
Hello 2
Hello 3

Remark:
The indentation codes are inside the if condition.


Example 8

Python Code:

x=800

if (x < 500):
    print ('Hello 1')
    print ('Hello 2')
    print ('Hello 3')
print ('Hello 4')

Output:

Hello 4

Remark:
The "print ('Hello 4')" is outside the if condition.

addiesam   AE Family   Offline   #7 2019-12-15 07:56:39

Re: 4 - Programming Basic and Python

3. Looping

The code becomes simpler.


Example 1

Python Code:

for x in range(1, 10):
   print (x)

Output:

1
2
3
4
5
6
7
8
9

Remark:
x is 1 in first round, x is 2 in second round, ... ...

rang(1, 10) means that x is ranged from 1 to 10 - 1 (or 9) instead of 10.  This is usual in programming.


Example 2

Python Code:

for x in range(1, 10):
   print (x)
   if x == 3:
       break

Output:

1
2
3

Remark:
"break" is to stop the looping.

addiesam   AE Family   Offline   #8 2019-12-15 08:15:14

Re: 4 - Programming Basic and Python

4. Array, List and Mapping

Variable can not only store a value but a list of values and key-value mapping.


4.1. List

Example 1:

Python Code:

x = ['cat', 'dog', 'bird']

print(x[1])

Output:

dog

Remark:
The value of list can be retrieved by a given index.  The index starts from 0.  It is usual in programming world.


Example 2:

Python Code:

x = ['cat', 'dog', 'bird']

for y in x:
    print(y)

Output:

cat
dog
bird

Remark:
Loop through the list elements.

y is the first element in first round, y is second element in second round, ... ...


Example 3:

Python Code:

x = ['cat', 'dog', 'bird']

for y in range(0, 3):
    print(y, x[y])

Output:

0 cat
1 dog
2 bird

addiesam   AE Family   Offline   #9 2019-12-15 08:43:41

Re: 4 - Programming Basic and Python

4.2. Mapping

Example 1:

Python Code:

x = {'c':'cat', 'd':'dog', 'rr':'robot'}

print(x['d'])

Output:

dog

Remark:
The values "c", "d" and "rr" are the keys and the corresponding values are "cat", "dog" and "robot".


Example 2:

Python Code:

x = {'c':'cat', 'd':'dog', 'rr':'robot'}
y = ['c', 'rr', 'rr', 'd']

for z in y:
    print(x[z])

Output:

cat
robot
robot
dog


Example 3:

Python Code:

x = {'c':'cat', 'd':'dog', 'rr':'robot'}

for z in x:
    print(x[z])

Output:

cat
dog
robot

Remark:
Loop through the keys from mapping.  How about the ordering?

From Python 3.6 onwards, the standard dict type (key-value mapping) maintains insertion order by default.

Board footer