forked from ernestas-poskus/interactive-programming-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodular_arithmetic.py
More file actions
78 lines (38 loc) · 1.27 KB
/
modular_arithmetic.py
File metadata and controls
78 lines (38 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Remainder - modular arithmetic
# systematically restrict computation to a range
# long division - divide by a number, we get a quotient plus a remainder
# quotient is integer division //, the remainder is % (Docs)
# problem - get the ones digit of a number
num = 49
tens = num // 10
ones = num % 10
print tens, ones
print 10 * tens + ones, num
# application - 24 hour clock
# http://en.wikipedia.org/wiki/24-hour_clock
hour = 20
shift = 8
print (hour + shift) % 24
# application - screen wraparound
# Spaceship from week seven
width = 800
position = 797
move = 5
position = (position + move) % width
print position
# Data conversion operations
# convert an integer into string - str
# convert an hour into 24-hour format "03:00", always print leading zero
hour = 3
ones = hour % 10
tens = hour // 10
print tens, ones, ":00"
print str(tens), str(ones), ":00"
print str(tens) + str(ones) + ":00"
# convert a string into numbers using int and float
# Python modules - extra functions implemented outside basic Python
import simplegui # access to drawing operations for interactive applications
import math # access to standard math functions, e.g; trig
import random # functions to generate random numbers
# look in Docs for useful functions
print math.pi