In [1]:
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!
In [3]:
print("Hello") # ctrl + space run the code
Hello
In [4]:
# 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
In [5]:
# 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
In [7]:
# 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
In [8]:
a = 2
In [15]:
b = "2"
In [16]:
type(1)
Out[16]:
int
In [11]:
type(b)
Out[11]:
str
In [12]:
a  + 3
Out[12]:
4
In [24]:
int(b) + 3
Out[24]:
5
In [18]:
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
In [21]:
c = "5 + (6 * 3)"
In [23]:
c
Out[23]:
'5 + (6 * 3)'
In [25]:
b
Out[25]:
'2'
In [26]:
int(b)
Out[26]:
2
In [27]:
v = "3333"
In [28]:
int(v) + 2
Out[28]:
3335
In [29]:
aaa = 1
In [30]:
aaa_bbb = 2
In [31]:
2aaa = 2
  Input In [31]
    2aaa = 2
     ^
SyntaxError: invalid syntax
In [32]:
a = 2
In [33]:
A = 4
In [34]:
a
Out[34]:
2
In [35]:
A
Out[35]:
4
In [38]:
for1 = 555
In [41]:
and2 = 333
In [42]:
x = 0.6
In [43]:
x = 3.9 * x * (1-x)
In [44]:
x
Out[44]:
0.9359999999999999
In [45]:
x = 3.9 * x * (1-x)
In [46]:
x
Out[46]:
0.2336256000000002
In [57]:
x = 1
In [58]:
x
Out[58]:
1
In [63]:
x = x + 3
In [64]:
x
Out[64]:
10
In [53]:
i = j = k = 111
In [54]:
i
Out[54]:
111
In [55]:
j
Out[55]:
111
In [56]:
k
Out[56]:
111
In [65]:
m, n = 20, 30
In [66]:
m
Out[66]:
20
In [67]:
n
Out[67]:
30
In [73]:
print(m,n)
30 20
In [72]:
m,n = n,m
In [74]:
print(m,n)
30 20
In [75]:
# 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
In [76]:
X = 666
In [77]:
X = 5555
In [78]:
X
Out[78]:
5555
In [79]:
#   final int X = 77;   # Java
In [80]:
PI = 3.149
In [81]:
a = 3
In [82]:
type(a)
Out[82]:
int
In [83]:
b = 3.0
In [84]:
type(b)
Out[84]:
float
In [85]:
3 // 2
Out[85]:
1
In [86]:
3 / 2
Out[86]:
1.5
In [87]:
2 ** 3
Out[87]:
8
In [88]:
pow(2,3)
Out[88]:
8
In [89]:
20 % 3
Out[89]:
2
In [90]:
20 // 3
Out[90]:
6
In [91]:
# 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
In [95]:
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')
In [96]:
2e3
Out[96]:
2000.0
In [97]:
45.6e5
Out[97]:
4560000.0
In [98]:
4e-3
Out[98]:
0.004
In [99]:
4E5
Out[99]:
400000.0
In [100]:
2E-1
Out[100]:
0.2
In [101]:
1 + (2 * 3)
Out[101]:
7
In [102]:
(1 + 2) * 3 
Out[102]:
9
In [103]:
1 + 2 * 3
Out[103]:
7
In [105]:
a = 5 * 2
In [106]:
type(a)
Out[106]:
int
In [107]:
b = 5 * 2.0
In [108]:
type(b)
Out[108]:
float
In [109]:
b
Out[109]:
10.0
In [110]:
a
Out[110]:
10
In [111]:
i = 10
In [112]:
i = i + 10
In [113]:
i
Out[113]:
20
In [114]:
i += 5 #    i = i + 5
In [115]:
i
Out[115]:
25
In [116]:
i++    # i = i + 1
  Input In [116]
    i++
       ^
SyntaxError: invalid syntax
In [117]:
i--
  Input In [117]
    i--
       ^
SyntaxError: invalid syntax
In [118]:
a = 4.0
In [119]:
b = 4
In [120]:
int(a)
Out[120]:
4
In [121]:
float(b)
Out[121]:
4.0
In [122]:
str(4)
Out[122]:
'4'
In [124]:
# 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
In [125]:
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
In [ ]: