#!/usr/bin/env python # coding: utf-8 # In[6]: # continuing section 2.7 # FACTS: # 1. operation on two integers --> generally gives an integer # Check this: print("1+2=",1+2) print(type(1+2)) # 2. operation on two floats --> will give a float print("1.0+2.0=",1.0+2.0) print(type(1.0+2.0)) # 3. operation containing mixed (int and float) --> will give float x=1 print("x=1 and x+2.0=",x+2.0) print(type(x+2.0)) print("the type of x is",type(x)) # # If you need to force a variable conversion, you can # use int() and float() to do it. print("x=",x) print("type(x)=",type(x)) print("type(float(x))=",type(float(x))) # In[19]: # Section 2.8 -- More about output # We know already how the following behaves: print('A') print('B') print('C') # Of course: it prints over three lines. # This is because the "default" "end line" character # is the so-called "newline character" # read more: https://en.wikipedia.org/wiki/Newline # print('--------------------') # # We can change the endline character: # it is just an "option" for the print function # the following command behaves same as print('A'): print('A',end="\n") # the following appends abracadabra to the end of the print # WITH NO SPACE OR NEW LINE print('B',end="abracadabra") # the following appends a space after it prints the character # C but nothing else print('C',end=" ") print('more stuff') # see print documentation: https://docs.python.org/3/library/functions.html#print # test the sep option # compare printthis="stuff stuff" print('a',printthis,'b',printthis) print('a',printthis,'b',printthis,sep='17') # In[23]: # Section 2.9 -- named constants # Imagine scenario where you are hired by a bank to code # you come across the following code: balance = 15.00 feedividend=balance*0.069 # here: the number 0.069 is a "magic number" in that it # appears to be important to the code functioning # properly, but its real purpose is not clear # # One way to fix the issue of not knowing what this number # is for is to use comments liberally!!! # # Another way -- use a "named constant" INTEREST_RATE=0.069 feedividend=balance*INTEREST_RATE costowed=balance+50*INTEREST_RATE # Example of a magic number # In Quake III Arena, the coders needed to compute # 1/sqrt(x), where x was some number. # # At this time, computing 1/sqrt(x) was expensive for the # computer -- doing so the "old way" might cause slowdowns # in the graphic processing of the map. # # How they did it: # shift right operation: shifts the binary bits to right by 1 print("11>>1=",11>>1) # 1. Took a 32-bit float number and shifted right # 2. subtract the shifted number from the hexadecimal number # 0x5F3759DF which encodes the number 1597463007 in hex # 3. run one instance of Newton's method to increase precision # # the magic number was 0x5F3759DF # In[24]: # # this code doesn't run in Jupyter notebook # RUN IT HERE INSTEAD: https://repl.it/languages/tkinter # # Here we finally learn hwo to # import a package import time import turtle # this shows the turtle turtle.showturtle() print("starting position=",turtle.pos()) # this halts for 5 seconds before doing next thing time.sleep(2) # this one moves the turtle forward 30 pixels turtle.forward(30) print("after moving forward 30",turtle.pos()) time.sleep(2) turtle.forward(10) print("after moving forward another 10",turtle.pos()) time.sleep(2) # rotates the turtle to the right # by the given number of degrees turtle.right(45) time.sleep(2) turtle.forward(50) print("after moving forward another 50",turtle.pos()) time.sleep(2) # we can set a "heading" -- an angle for which # it should point # we measure angles starting at 0 deg to 360 deg in a # counterclockwise motion # # # we force an angle for turtle to point using the following: turtle.setheading(190) time.sleep(2) turtle.forward(100) print("after moving forward another 100",turtle.pos()) time.sleep(2) # can change the turtle's position to anywhere # we want # # can clear and reset the turtle back to default: turtle.reset() turtle.setheading(180) turtle.forward(100) time.sleep(2) turtle.setheading(0) turtle.forward(50) time.sleep(2) turtle.setheading(270) turtle.forward(200) time.sleep(5)