Download (right-click, save target as ...) this page as a jupyterlab notebook from:
LAST NAME, FIRST NAME
R00000000
ENGR 1330 Laboratory 2 - Homework
Notice the code cell below! From this notebook forward please include and run the script in the cell, it will help in debugging a notebook. Its ok if the code makes no sense right now - mostly the cell executes system commands. As you change machines, and rerun the cell the output will change (its supposed to!)
# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
atomickitty sensei /opt/jupyterhub/bin/python3 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0] sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)
The cell below is type RAW, change it into a code cell and activate (suppress the comments) and run the script.
What is the value of area?
# Demonstrate some assignment operations
width = 4
length = width
length += 2
area = length * width
Copy your working script below and add necessary code to output the value of area.
# copy here
print('Value of area is ',area)
Value of area is 24
Change the RAW cell below into a Code cell, and run the script (fix any syntax errors)
# data types
print ('integers and reals')
x1 = 1.0
y1 = 1.
z1 = 1
x2 = 5.0
y2 = 5.
z2 = 5
print ('x1 = ', x1, ' y1 = ', y1, ' z1 = ', z1)
print ('x2 = ', x2, ' y2 = ', y2, ' z2 = ', z2)
print ('x1/x2 = ',x1/x2,' y1/y2 = ',y1/y2,' z1/z2 = ',z1//z2)
integers and reals x1 = 1.0 y1 = 1.0 z1 = 1 x2 = 5.0 y2 = 5.0 z2 = 5 x1/x2 = 0.2 y1/y2 = 0.2 z1/z2 = 0
a. Of the six variables, which are integers?
# put your answer here
print('x1 is type: ',type(x1))
print('y1 is type: ',type(y1))
print('z1 is type: ',type(z1))
print('x2 is type: ',type(x2))
print('y2 is type: ',type(y2))
print('z2 is type: ',type(z2))
x1 is type: <class 'float'> y1 is type: <class 'float'> z1 is type: <class 'int'> x2 is type: <class 'float'> y2 is type: <class 'float'> z2 is type: <class 'int'>
print('z1 and z2 are integer, rest are real (floats)')
z1 and z2 are integer, rest are real (floats)
b. What is the difference (in effect) between x1=1.0 and y1=1.?
# put your answer here
print(hex(id(x1))) # give the memory location of x1
print(hex(id(y1))) # give the memory location of x1
0xffff8549ad50 0xffff8549ad70
# now the contents, again in hex just because
print(x1) # give the contents location of x1
print(y1) # give the contents location of x1
1.0 1.0
print('The two variables have different locations, but identical values!')
The two variables have different locations, but identical values!
c. Examine the division results; Why does z1//z2 return a value of 0?
# put your answer here
print('Floor division returns how many integer times the denominator divides the numerator. In the example, the value is zero with remainder one')
Floor division returns how many integer times the denominator divides the numerator. In the example, the value is zero with remainder one
# change this cell to CODE to run
a = 21
b = 10
c = 0
Then change the cell below to code and run it.
# change this cell to CODE to run
c = a + b
print ("Value of c is ", c)
Value of c is 31
Now using the example in the cell above, exaluate the following expressions in the indicated cells below:
#c = a - b
c = a - b
print ("Value of c is ", c)
Value of c is 11
#c = a * b
c = a * b
print ("Value of c is ", c)
Value of c is 210
#c = a / b
c = a + b
print ("Value of c is ", c)
Value of c is 31
#c = a % b
c = a % b
print ("Value of c is ", c)
Value of c is 1
#a = 2, b = 3, c = a** b
a = 2
b = 3
c = pow(a,b)
print ("Value of c is ", c)
Value of c is 8
#a = 10, b = 5, c = a//b
a = 10
b = 5
c = a // b
print ("Value of c is ", c)
Value of c is 2