In [1]:
data = eval(input("Enter an integer (the input exits " + 
    "if the input is 0): "))

# Keep reading data until the input is 0
sum = 0
while data != 0:
    sum += data

    data = eval(input("Enter an integer (the input exits " +
        "if the input is 0): "))

print("The sum is", sum)
Enter an integer (the input exits if the input is 0): 5
Enter an integer (the input exits if the input is 0): 6
Enter an integer (the input exits if the input is 0): 7
Enter an integer (the input exits if the input is 0): 0
The sum is 18
In [2]:
data = eval(input("Enter an integer (the input exits " + 
    "if the input is 0): "))

# Keep reading data until the input is 0
sum = 0
while True:
    sum += data

    data = eval(input("Enter an integer (the input exits " +
        "if the input is 0): "))
    if data == 0:
        break

print("The sum is", sum)
Enter an integer (the input exits if the input is 0): 5
Enter an integer (the input exits if the input is 0): 6
Enter an integer (the input exits if the input is 0): 7
Enter an integer (the input exits if the input is 0): 8
Enter an integer (the input exits if the input is 0): 0
The sum is 26
In [3]:
a = 5
In [4]:
b = 5.0
In [5]:
type(a)
Out[5]:
int
In [6]:
type(b)
Out[6]:
float
In [11]:
2 ** 10000
Out[11]:
19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376
In [10]:
2.0 ** 10000
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 2.0 ** 10000

OverflowError: (34, 'Result too large')
In [12]:
# Initialize sum
sum = 0

# Add 0.01, 0.02, ..., 0.99, 1 to sum
i = 0.01
while i <= 1.0:
    sum += i
    i = i + 0.01
    print("The sum is", sum)
    
# Display result
print("After loop, The sum is", sum)
The sum is 0.01
The sum is 0.03
The sum is 0.06
The sum is 0.1
The sum is 0.15000000000000002
The sum is 0.21000000000000002
The sum is 0.28
The sum is 0.36000000000000004
The sum is 0.45000000000000007
The sum is 0.55
The sum is 0.66
The sum is 0.78
The sum is 0.91
The sum is 1.05
The sum is 1.2
The sum is 1.3599999999999999
The sum is 1.5299999999999998
The sum is 1.7099999999999997
The sum is 1.8999999999999997
The sum is 2.0999999999999996
The sum is 2.3099999999999996
The sum is 2.53
The sum is 2.76
The sum is 3.0
The sum is 3.25
The sum is 3.5100000000000002
The sum is 3.7800000000000002
The sum is 4.0600000000000005
The sum is 4.3500000000000005
The sum is 4.65
The sum is 4.960000000000001
The sum is 5.280000000000001
The sum is 5.610000000000001
The sum is 5.950000000000001
The sum is 6.300000000000002
The sum is 6.660000000000002
The sum is 7.030000000000002
The sum is 7.410000000000002
The sum is 7.8000000000000025
The sum is 8.200000000000003
The sum is 8.610000000000003
The sum is 9.030000000000003
The sum is 9.460000000000003
The sum is 9.900000000000002
The sum is 10.350000000000003
The sum is 10.810000000000004
The sum is 11.280000000000005
The sum is 11.760000000000005
The sum is 12.250000000000005
The sum is 12.750000000000005
The sum is 13.260000000000005
The sum is 13.780000000000005
The sum is 14.310000000000006
The sum is 14.850000000000007
The sum is 15.400000000000007
The sum is 15.960000000000008
The sum is 16.53000000000001
The sum is 17.11000000000001
The sum is 17.70000000000001
The sum is 18.30000000000001
The sum is 18.91000000000001
The sum is 19.530000000000012
The sum is 20.16000000000001
The sum is 20.80000000000001
The sum is 21.45000000000001
The sum is 22.11000000000001
The sum is 22.780000000000012
The sum is 23.46000000000001
The sum is 24.150000000000013
The sum is 24.850000000000012
The sum is 25.560000000000013
The sum is 26.280000000000012
The sum is 27.010000000000012
The sum is 27.750000000000014
The sum is 28.500000000000014
The sum is 29.260000000000016
The sum is 30.030000000000015
The sum is 30.810000000000016
The sum is 31.600000000000016
The sum is 32.40000000000001
The sum is 33.210000000000015
The sum is 34.030000000000015
The sum is 34.860000000000014
The sum is 35.70000000000002
The sum is 36.55000000000002
The sum is 37.41000000000002
The sum is 38.280000000000015
The sum is 39.16000000000002
The sum is 40.05000000000002
The sum is 40.95000000000002
The sum is 41.86000000000002
The sum is 42.78000000000002
The sum is 43.71000000000002
The sum is 44.65000000000002
The sum is 45.60000000000002
The sum is 46.560000000000024
The sum is 47.53000000000002
The sum is 48.510000000000026
The sum is 49.50000000000003
After loop, The sum is 49.50000000000003
In [ ]:
item = 1
sum = 0
while item != 0: # No guarantee item will be 0
    sum += item
    item -= 0.1  # 0.9 , 0.8, 0.7, ....., 0.1, 0
    #if item < -100 and item % 100 == 0:
    print(item)
print(sum)
In [13]:
c = 1/3 # decimal system
In [14]:
c
Out[14]:
0.3333333333333333
In [4]:
for num in [1,2,3,55]:
    print(num)
1
2
3
55
In [5]:
c = [22,33,44,555,777] # This is called a list.
In [6]:
for i in c:
    print(i)
22
33
44
555
777
In [7]:
s = "Hello"
In [8]:
for j in s:
    print(j)
H
e
l
l
o
In [9]:
range(5)
Out[9]:
range(0, 5)
In [11]:
for i in range(3):
    print(i)
0
1
2
In [12]:
for i in range(7):
    print(i)
0
1
2
3
4
5
6
In [13]:
for i in range(5,8):
    print(i)
5
6
7
In [14]:
for i in range(0,8):
    print(i)
0
1
2
3
4
5
6
7
In [15]:
for i in range(8):
    print(i)
0
1
2
3
4
5
6
7
In [16]:
for i in range(1,15,3):
    print(i)
1
4
7
10
13
In [19]:
for i in range(18,7,-2):
    print(i)
18
16
14
12
10
8
In [23]:
for i in range(1,3):
    for j in range(1,5):
        print("i=",i,"  j=",j)
i= 1   j= 1
i= 1   j= 2
i= 1   j= 3
i= 1   j= 4
i= 2   j= 1
i= 2   j= 2
i= 2   j= 3
i= 2   j= 4
In [24]:
for i in range(1,3):
    for j in range(1,5):
        for k in range(1,5):
            print("i=",i,"  j=",j, "k=",k)
i= 1   j= 1 k= 1
i= 1   j= 1 k= 2
i= 1   j= 1 k= 3
i= 1   j= 1 k= 4
i= 1   j= 2 k= 1
i= 1   j= 2 k= 2
i= 1   j= 2 k= 3
i= 1   j= 2 k= 4
i= 1   j= 3 k= 1
i= 1   j= 3 k= 2
i= 1   j= 3 k= 3
i= 1   j= 3 k= 4
i= 1   j= 4 k= 1
i= 1   j= 4 k= 2
i= 1   j= 4 k= 3
i= 1   j= 4 k= 4
i= 2   j= 1 k= 1
i= 2   j= 1 k= 2
i= 2   j= 1 k= 3
i= 2   j= 1 k= 4
i= 2   j= 2 k= 1
i= 2   j= 2 k= 2
i= 2   j= 2 k= 3
i= 2   j= 2 k= 4
i= 2   j= 3 k= 1
i= 2   j= 3 k= 2
i= 2   j= 3 k= 3
i= 2   j= 3 k= 4
i= 2   j= 4 k= 1
i= 2   j= 4 k= 2
i= 2   j= 4 k= 3
i= 2   j= 4 k= 4
In [29]:
print("          Multiplication Table")
# Display the number title
print("  |", end = '')
for j in range(1, 10):
    print("  ", j, end = '')
print() # Jump to the new line
print("-----------------------------------------")

# Display table body
for i in range(1, 10):
    print(i, "|", end = '')
    for j in range(1, 10): 
        # Display the product and align properly
        print(format(i * j, '4d'), end = '')
    print()# Jump to the new line
          Multiplication Table
  |   1   2   3   4   5   6   7   8   9
-----------------------------------------
1 |   1   2   3   4   5   6   7   8   9
2 |   2   4   6   8  10  12  14  16  18
3 |   3   6   9  12  15  18  21  24  27
4 |   4   8  12  16  20  24  28  32  36
5 |   5  10  15  20  25  30  35  40  45
6 |   6  12  18  24  30  36  42  48  54
7 |   7  14  21  28  35  42  49  56  63
8 |   8  16  24  32  40  48  56  64  72
9 |   9  18  27  36  45  54  63  72  81
In [31]:
#Prompt the user to enter two integers
n1 = eval(input("Enter first integer: "))
n2 = eval(input("Enter second integer: "))

gcd = 1
k = 2
while k <= n1 and k <= n2:
    if n1 % k == 0 and n2 % k == 0:
        gcd = k
        print("gcd=",gcd)
    k += 1

print("The greatest common divisor for", 
    n1, "and", n2, "is", gcd)
Enter first integer: 20
Enter second integer: 30
gcd= 2
gcd= 5
gcd= 10
The greatest common divisor for 20 and 30 is 10
In [36]:
year = 0 # Year 0
tuition = 10000   # Year 1

while tuition < 100000:
    year += 1
    tuition = tuition * 1.08

print("Tuition will be doubled in", year, "years")
print("Tuition will be $" + format(tuition, ".2f"), 
      "in", year, "years")
Tuition will be doubled in 30 years
Tuition will be $100626.57 in 30 years
In [37]:
year = 0 # Year 0
tuition = 10000   # Year 1

while tuition < 20000:
    year += 1
    tuition = tuition * 1.07

print("Tuition will be doubled in", year, "years")
print("Tuition will be $" + format(tuition, ".2f"), 
      "in", year, "years")
Tuition will be doubled in 11 years
Tuition will be $21048.52 in 11 years
In [40]:
import random

NUMBER_OF_TRIALS = 10_000_000 # Constant
numberOfHits = 0

for i in range(NUMBER_OF_TRIALS):
    x = random.random() * 2 - 1
    y = random.random() * 2 - 1

    if x * x + y * y <= 1:
        numberOfHits += 1

pi = 4 * numberOfHits / NUMBER_OF_TRIALS

print("PI is", pi)
PI is 3.1417288
In [44]:
sum = 0
for i in range(100):
    print(i)
0
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
In [43]:
sum = 0
for i in range(100):
    print(i)
    if i == 15:
        break
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
In [45]:
for i in range(1,3):
    for j in range(1,5):
        print("i=",i,"  j=",j)
        if j == 3:
            break
i= 1   j= 1
i= 1   j= 2
i= 1   j= 3
i= 2   j= 1
i= 2   j= 2
i= 2   j= 3
In [47]:
sum = 0
number = 0

while number < 20:
    number += 1
    sum += number # sum = sum + number
    print("sum = ", sum, "number = ", number )
    if sum >= 100: 
        break

print("The number is", number)
print("The sum is", sum)
sum =  1 number =  1
sum =  3 number =  2
sum =  6 number =  3
sum =  10 number =  4
sum =  15 number =  5
sum =  21 number =  6
sum =  28 number =  7
sum =  36 number =  8
sum =  45 number =  9
sum =  55 number =  10
sum =  66 number =  11
sum =  78 number =  12
sum =  91 number =  13
sum =  105 number =  14
The number is 14
The sum is 105
In [48]:
sum = 0
number = 0

while number < 20: 
    number += 1
    if number == 10 or number == 11: 
        continue
    print("sum = ", sum, "number = ", number )    
    sum += number

print("The sum is", sum)
sum =  0 number =  1
sum =  1 number =  2
sum =  3 number =  3
sum =  6 number =  4
sum =  10 number =  5
sum =  15 number =  6
sum =  21 number =  7
sum =  28 number =  8
sum =  36 number =  9
sum =  45 number =  12
sum =  57 number =  13
sum =  70 number =  14
sum =  84 number =  15
sum =  99 number =  16
sum =  115 number =  17
sum =  132 number =  18
sum =  150 number =  19
sum =  169 number =  20
The sum is 189
In [49]:
import random

#Generate a random number to be guessed
number = random.randint(0, 100) 

print("Guess a magic number between 0 and 100")

while True: 
    #Prompt the user to guess the number
    guess = eval(input("Enter your guess: "))

    if guess == number: 
        print("Yes, the number is " + str(number))
        break
    elif guess > number:
        print("Your guess is too high")
    else:
        print("Your guess is too low")
Guess a magic number between 0 and 100
Enter your guess: 50
Your guess is too low
Enter your guess: 75
Your guess is too low
Enter your guess: 85
Your guess is too low
Enter your guess: 90
Your guess is too low
Enter your guess: 92
Your guess is too high
Enter your guess: 91
Yes, the number is 91
In [52]:
NUMBER_OF_PRIMES = 50  # Number of primes to display
NUMBER_OF_PRIMES_PER_LINE = 5 # Display 10 per line
count = 0 # Count the number of prime numbers
number = 2 # A number to be tested for primeness

print("The first 50 prime numbers are")

# Repeatedly find prime numbers
while count < NUMBER_OF_PRIMES:
    # Assume the number is prime
    isPrime = True #Is the current number prime?

    # Test if number is prime
    divisor = 2
    while divisor <= number / 2:
        if number % divisor == 0:
            # If true, the number is not prime
            isPrime = False  # Set isPrime to false
            break  # Exit the for loop
        divisor += 1

    # Display the prime number and increase the count
    if isPrime:
        count += 1 # Increase the count

        print(format(number, '5d'), end = '')
        if count % NUMBER_OF_PRIMES_PER_LINE == 0:
            # Display the number and advance to the new line
            print() # Jump to the new line

    # Check if the next number is prime
    number += 1
The first 50 prime numbers are
    2    3    5    7   11
   13   17   19   23   29
   31   37   41   43   47
   53   59   61   67   71
   73   79   83   89   97
  101  103  107  109  113
  127  131  137  139  149
  151  157  163  167  173
  179  181  191  193  197
  199  211  223  227  229
In [53]:
import turtle
for x in range(4):
    turtle.forward(100)
    turtle.right(90)
turtle.done()

try:
    turtle.bye()
except:
    print("bye") 
bye
In [56]:
import turtle
for x in range(8):
    turtle.forward(100)
    turtle.right(45)
turtle.done()


try:
    turtle.bye()
except:
    print("bye") 
bye
In [57]:
import turtle
NUM_CIRCLES = 36    # Number of circles to draw
RADIUS = 100        # Radius of each circle
ANGLE = 10          # Angle to turn

for x in range(NUM_CIRCLES):
    turtle.circle(RADIUS)
    turtle.left(ANGLE)
turtle.done()

try:
    turtle.bye()
except:
    print("bye") 
bye
In [76]:
import turtle

START_X = -200  # Starting X coordinate
START_Y = 0  # Starting Y coordinate
NUM_LINES = 36  # Number of lines to draw
LINE_LENGTH = 400  # Length of each line
ANGLE = 170  # Angle to turn

turtle.hideturtle()
turtle.penup()
turtle.goto(START_X, START_Y)
turtle.pendown()

for x in range(NUM_LINES):
    turtle.forward(LINE_LENGTH)
    turtle.left(ANGLE)

turtle.done()

try:
    turtle.bye()
except:
    print("bye")
bye
In [97]:
 
In [ ]:
import turtle
from random import randint

turtle.speed(1) # Set turtle speed to slowest

# Draw 16 by 16 lattices
turtle.color("gray") # Color for lattice
x = -80 
for y in range(-80, 80 + 1, 10):
    turtle.penup()
    turtle.goto(x, y) # Draw a horizontal line
    turtle.pendown()
    turtle.forward(160)

y = 80
turtle.right(90)
for x in range(-80, 80 + 1, 10):
    turtle.penup()
    turtle.goto(x, y) # Draw a vertical line
    turtle.pendown()
    turtle.forward(160)
    
turtle.pensize(3)
turtle.color("red")

turtle.penup()
turtle.goto(0, 0) # Go to the center
turtle.pendown()

x = y = 0 # Current pen location at the center of lattice
while abs(x) < 80 and abs(y) < 80:    
    r = randint(0, 3)
    if r == 0:
        x += 10  # Walk east
        turtle.setheading(0)
        turtle.forward(10)      
    elif r == 1:
        y -= 10 # Walk south
        turtle.setheading(270)
        turtle.forward(10)      
    elif r == 2:
        x -= 10 # Walk west
        turtle.setheading(180)
        turtle.forward(10)      
    elif r == 3:
        y += 10 # Walk north
        turtle.setheading(90)
        turtle.forward(10)      

turtle.done() 
In [ ]: