You are not logged in.
Pages: 1
The official Python tutorial:
1. Variable Assignment
2. If Condition
3. Looping
4. Array, List and Mapping
5. Define a Function
6. OO Concept (Object Oriented Concept)
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
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.
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).
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=300if (x < 500) and (y <= 300):
print ('Hello')
Output:
Hello
Example 4
Python Code:
x=100
y=300if (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.
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.
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
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.
Pages: 1