images/blog-posts

The Beginning・Python Calculator

We can use python to do some calc operations like a calculator. It will be power than many calculators if you know enough python. And some calculators have embedded python. Casio FX 9860 GIII is an example.

The Number

In WIDLE(WIDLE is a python ide run in a web browser), you can input expressions and click the run button to get answers. Use the print function to show the answers. The grammar of these expressions is so direct. Some symbols like +, -, *, / just like in calculator. And by the brackets like () can deal with the priority. An example:

print(1 + 2)
print(3 - 4)
print(5 * 6)
print(7 / 8)
print(1 + (2 - 3) * 4 / 5)

The operation result:

3
-1
30
0.875
0.19999999999999996

Integer like these 2, 4, 20 is int type, float like these 5.0, 1.6 is float type.

Do divide using the symbol / will return a float value, but // will return an integer value. Use % to do modulo operation.

print(10 / 3)
print(10 // 3)
print(10 % 3)

The operation result:

3.3333333333333335
3
1

The equal symbol = is used to assign the variable. The python variable is like the variable in math, and we can use a python variable to refer to a value. A = B is assigned a value B to a variable A.

width = 10
height = 10
area = width * height
print(area)

The operation result:

100

The variable in the python program is case-sensitive. Some recommend formats of the variable are below.

  1. Put letters or underscore to first;
  2. Characters of the variable only letters, underline, and numbers(A-z, 0-9, and _);

The String

In addition to the number, there is the string in python. The string format of python is multiple, for example, single quotation marks '...' and double quotation marks "...".

print('Hi! kk.')
print("Hi! jojo.")

The operation result:

Hi! kk.
Hi! jojo.

Convert some characters to special characters by a backslash symbol \:

print('0\t1\t2\t3')
print('0\n1\n2\n3')

The operation result:

0	1	2	3
0
1
2
3

We can new lines in a string when using triple quotation marks. If you want it not to be a new line, add a backslash symbol \ at the end of the previous line. Below is an example:

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

The operation result:

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

Concatenation strings using +, using * to repeat it:

print('123' + '456')
print('abc' * 3)

The operation result:

123456
abcabcabc

We can use the len function to calculate string length

numbers = '1234567890'
print(len(numbers))

The operation result:

10

The Types

Use type function to print type name of objects:

a = 123
print(type(a))
b = 123.321
print(type(b))
c = "123.321"
print(type(c))

The operation result:

<class 'int'>
<class 'float'>
<class 'str'>

We can use int, float, str functions to do type converting:

a = 123
print(a, type(a))
b = float(a)
print(b, type(b))
c = str(b)
print(c, type(c))
e = float(c)
print(e, type(e))
f = int(e)
print(f, type(f))

The operation result:

123 <class 'int'>
123.0 <class 'float'>
123.0 <class 'str'>
123.0 <class 'float'>
123 <class 'int'>

SUBSCRIBE


🔒 No spam. Unsubscribe any time.

About kk

kk

Vincenzo Antedoro is an engineer who helps those who want to invest in renewables. For the rest he enjoys teaching with the method of learning by doing..

» More about kk