import this
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
print("Hello") # ctrl + space run the code
Hello
# Assign a radius
radius = 20 # radius is now 20
# Compute area
area = radius * radius * 3.14159
# Display results
print("The area for the circle of radius", radius, "is", area)
The area for the circle of radius 20 is 1256.636
# Prompt the user to enter a radius
radius = eval(input("Enter a number for radius: "))
# Compute area
area = radius * radius * 3.14159
# Display results
print("The area for the circle of radius", radius, "is", area)
Enter a number for radius: 40 The area for the circle of radius 40 is 5026.544
# Prompt the user to enter three numbers
number1 = eval(input("Enter the first number: "))
number2 = eval(input("Enter the second number: "))
number3 = eval(input("Enter the third number: "))
# Compute average
average = (number1 + number2 + number3) / 3
# Display result
print("The average of", number1, number2, number3,
"is", average)
Enter the first number: 1 Enter the second number: 5 Enter the third number: 7 The average of 1 5 7 is 4.333333333333333
a = 2
b = "2"
type(1)
int
type(b)
str
a + 3
4
int(b) + 3
5
b + 3
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [18], in <cell line: 1>() ----> 1 b + 3 TypeError: can only concatenate str (not "int") to str
c = "5 + (6 * 3)"
c
'5 + (6 * 3)'
b
'2'
int(b)
2
v = "3333"
int(v) + 2
3335
aaa = 1
aaa_bbb = 2
2aaa = 2
Input In [31] 2aaa = 2 ^ SyntaxError: invalid syntax
a = 2
A = 4
a
2
A
4
for1 = 555
and2 = 333
x = 0.6
x = 3.9 * x * (1-x)
x
0.9359999999999999
x = 3.9 * x * (1-x)
x
0.2336256000000002
x = 1
x
1
x = x + 3
x
10
i = j = k = 111
i
111
j
111
k
111
m, n = 20, 30
m
20
n
30
print(m,n)
30 20
m,n = n,m
print(m,n)
30 20
# Prompt the user to enter three numbers
number1, number2, number3 = eval(input(
"Enter three numbers separated by commas: "))
# Compute average
average = (number1 + number2 + number3) / 3
# Display result
print("The average of", number1, number2, number3,
"is", average)
Enter three numbers separated by commas: 2,4,6 The average of 2 4 6 is 4.0
X = 666
X = 5555
X
5555
# final int X = 77; # Java
PI = 3.149
a = 3
type(a)
int
b = 3.0
type(b)
float
3 // 2
1
3 / 2
1.5
2 ** 3
8
pow(2,3)
8
20 % 3
2
20 // 3
6
# Prompt the user for input
seconds = eval(input("Enter an integer for seconds: "))
# Get minutes and remaining seconds
minutes = seconds // 60 # Find minutes in seconds
remainingSeconds = seconds % 60 # Seconds remaining
print(seconds, "seconds is", minutes,
"minutes and", remainingSeconds, "seconds")
Enter an integer for seconds: 125 125 seconds is 2 minutes and 5 seconds
255.0 ** 1000
--------------------------------------------------------------------------- OverflowError Traceback (most recent call last) Input In [95], in <cell line: 1>() ----> 1 255.0 ** 1000 OverflowError: (34, 'Result too large')
2e3
2000.0
45.6e5
4560000.0
4e-3
0.004
4E5
400000.0
2E-1
0.2
1 + (2 * 3)
7
(1 + 2) * 3
9
1 + 2 * 3
7
a = 5 * 2
type(a)
int
b = 5 * 2.0
type(b)
float
b
10.0
a
10
i = 10
i = i + 10
i
20
i += 5 # i = i + 5
i
25
i++ # i = i + 1
Input In [116] i++ ^ SyntaxError: invalid syntax
i--
Input In [117] i-- ^ SyntaxError: invalid syntax
a = 4.0
b = 4
int(a)
4
float(b)
4.0
str(4)
'4'
# Prompt the user for input
purchaseAmount = eval(input("Enter purchase amount: "))
# Compute sales tax
tax = purchaseAmount * 0.06
# Display tax amount with two digits after decimal point
print("Sales tax is", int(tax * 100) / 100.0)
Enter purchase amount: 21 Sales tax is 1.26
import time
currentTime = time.time() # Get current time
print(currentTime)
# Obtain the total seconds since midnight, Jan 1, 1970
totalSeconds = int(currentTime)
# Get the current second
currentSecond = totalSeconds % 60
# Obtain the total minutes
totalMinutes = totalSeconds // 60
# Compute the current minute in the hour
currentMinute = totalMinutes % 60
# Obtain the total hours
totalHours = totalMinutes // 60
# Compute the current hour
currentHour = totalHours % 24
# Display results
print("Current time is", currentHour, ":",
currentMinute, ":", currentSecond, "GMT")
1664893245.6626434 Current time is 14 : 20 : 45 GMT