-
Notifications
You must be signed in to change notification settings - Fork 0
/
INTRO.py
107 lines (80 loc) · 2.12 KB
/
INTRO.py
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
100
101
102
103
104
105
106
107
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.13.6
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# +
# 20240209
"""Finger exercise: Write a program that examines three variables—x, y, and z—and
prints the largest odd number among them. If none of them are odd, it should
print a message to that effect."""
# Tests
x, y, z = 0, 0, 0 # 'None of the number is odd'
# x, y, z = 1, 0, 0 # 1
# x, y, z = 0, 1, 0 # 1
# x, y, z = 0, 0, 1 # 1
# x, y, z = 1, 3, 5 # 5
# x, y, z = 3, 3, 1 # 3
result = None
if x%2 == 1:
result = x
if (y%2 == 1) and y > x:
result = y
if (z%2 == 1) and z > y:
result = z
elif (z%2 == 1) and z > x:
result = z
elif y%2 == 1:
result = y
if (z%2 == 1) and z > y:
result = z
elif z%2 == 1:
result = z
if result:
print(result)
else:
print('None of the numbers is odd')
# -
# 20240208
if x%2 == 0:
if x%3 == 0:
print('Divisible by 2 and 3')
else:
print('Divisible by 2 and not by 3')
elif x%3 == 0:
print('Divisible by 3 and not by 2')
# 20240208
x = 2
if x%2 == 0:
print('Even')
else:
print('Odd')
print('Done with conditional')
# 20240206
# 2 INTRODUCTION TO PYTHON
# 2.1 The Basic Elements of Python
print('Yankees rule!')
print('But not in Boston!')
print('Yankees rule,', 'but not in Boston!')
# 20240205
def Heron(guess, target, tolerance):
""" Assume guess a strictly positive float
Assumes target a positive float
Assumes tolerance a strictly positive float
prints the results of Heron of Alexandria square root search algorithm"""
guess = guess
while abs(guess**2-target)>tolerance:
guess = (guess+target/guess)/2
print(abs(guess**2-target))
print(guess, guess**2, target, abs(guess**2-target))
Heron(0.000001,25,0.0001)