Lab Practice 1
Lab Practice 1
Lab1
Part 1
Using the interpreter, type in the expressions below. Copy and paste the output into the output column. If the result is
unexpected, note that in the third column.
Section 1
Input Output Did it do something
unexpected?
a 5 + 2 * 2
x = 7
y = 3
x + y
total = x + y
total
b 2/3
2.0 * 1.5
(2 + 3) * 10
5.0 // 2
5.0 % 2
9 ** (1 / 2)
-13 // 4
7.5 % 3.5
c a
'a + b'
'a' + 'b'
'a' * 'b'
'a' * 2
d print('Welcome to Python!')
print("Welcome to Python!")
print('Welcome', 'to', 'Python!')
print('Welcome\nto\n\nPython!')
print('Display "hi" in quotes')
print('Display 'hi' in quotes')
print('Display \'hi\' in quotes')
print("Display the name O'Brien")
print("Display \"hi\" in quotes")
print("""Display "hi" and 'bye' in quotes""")
e triple_quoted_string = """This is a
triple-quoted string that spans two lines"""
print(triple_quoted_string)
triple_quoted_string
name = input("What's your name? ")
name
Section 2 (conditionals)
Input Output Did it do something
unexpected?
a x = 2
y = 3
if x > y:
print('x is greater')
elif x < y:
print('y is greater')
else:
print('x & y are equal')
b x = int(input('Enter a number'))
if x % 2 == 0:
print('even')
else:
print('odd')
Section 3 (iterations)
a x = 10
while x > 0:
print(x)
x -=1
b def square_root(number):
return number ** (0.5)
square_root(4)
square_root(2)
Section 5 (strings)
a f'{65:c}'
f'{17.489:.2f}'
f'{15:b}'
f'{15:x}'
f'{15:o}'
f'{"hello":s} {7}'
b f'[{27:10d}]'
f'[{3.5:10f}]'
f'[{"hello":10}]'
f'[{27:<15d}]'
f'[{"hello":>15}]'
f'[{3.5:^7.1f}]'
c f'[{27:+10d}]'
f'[{27:+010d}]'
print(f'{27:d}\n{27: d}\n{-27: d}')
f'{12345678:,d}'
f'{123456.78:,.2f}'
'{} {}'.format('Amanda', 'Cyan')
'{0} {0} {1}'.format('Happy', 'Birthday')
d sentence = ' This is a test string. '
sentence.strip()
'happy birthday'.capitalize()
'happy birthday'.title()
'Orange' < 'orange'
'Orange' == 'orange'
b while True:
# attempt to convert and divide values
try:
number1 = int(input('Enter numerator: '))
number2 = int(input('Enter denominator: '))
result = number1 / number2
except ValueError:
print('You must enter two integers\n')
except ZeroDivisionError:
print('Attempted to divide by zero\n')
else:
print(f'{number1:.3f} / {number2:.3f} = {result:.3f}')
break
Section 7 (lists and tuples)
c import numpy as np
numbers = np.array([1, 4, 9, 16, 25, 36])
np.sqrt(numbers)
numbers2 = np.arange(1, 7) * 10
numbers2
np.add(numbers, numbers2)
np.multiply(numbers2, 5)
numbers3 = numbers2.reshape(2, 3)
numbers3