COMP1005 Exam
Study online at https://quizlet.com/_hcq7qc
1.
How do you put grid lines on the plot?
plt.grid(True)
2.
How do you make a directory?
mkdir [directory name]
3.
How do you remove a directory?
rmdir [directory name]
4.
How do you list the contents of a directory?
ls
5.
How do you change directory?
cd [directory name], to exit do
cd ..
6.
How do you get a history of commands?
history
7.
How do you copy file?
cp <old file> <new file>
8.
How do you create a file?
vim [filename].py for pythong
and .txt for text .csv for csv file
9.
What are some comparison operations?
!= is not equal
== is equal
>= is greater than or equal
<= is less than or equal
10. How do you convert x to an integer?
int(x [,base]), the base is if x is
a string, but without it base 10
is assumed
11. How do you convert x to a float?
float(x)
12. How do you convert x to a string?
str(x)
13. What are the different control sctructures?
if = checks to see if a statement
is true
while = repeats a block of statements until the condition is
1/5
COMP1005 Exam
Study online at https://quizlet.com/_hcq7qc
false
for = repeats a block of code for
a set number of iterations
14. How would you code a loop that asks for either a 1 valid = True
or 2
while valid:
choice = input("Which number, 1 or 2?")
if choice == "1":
valid = False
elif choice == "2":
valid = False
else:
print("That is invalid")
valid = True
15. What does i % 2 == 0?
This is essentially saying that if
16. How do you convert characters to upper and lower .upper() for uppercase
case?
.lower() for lowercase
17. How would you code a program that generated a x = "string"
string and then replaced every character divisble by new_x = ""
2 with a 1 and divisible by 3 is converted to upper for i in range(len(x)):
case
if i % 2 == 0:
new_x += "1"
elif i % 3 ==0:
new_x += x[1].upper
else:
new_x += x[i]
print(new_x)
2/5
COMP1005 Exam
Study online at https://quizlet.com/_hcq7qc
18. How do you get the length of something and print len(...)
it?
e.g x = "string", y = [1, 2, 3, 4,
5]
print(len(x))
print(len(y))
19. Explain how indexing works in pythong?
Each element in a string is assigned a number, even spaces
and apostrophes, starting at 0
20. How do you access individidual characters of a
string?
x = "string"
print(x[2]) which prints "t"
OR for i in range(len(x))
print(x[i])
21. How would you write code that prints the index
backwards skipping every second word?
for index in range(len(x)-1, -1,
-2):
print(x[i])
see this range give (start, stop,
skip), starts at x-1 which is the
last index, -1 means it ends at
0, and -2 means it skips
22. What are different functions we can add to print?? sep = " ", to seperate different
words in print
end = " ", to add something at
the end of each character
23. What are common operations when working with min(x) - prints lowers in alphastrings?
bet
max(x) - prints largest in alphabet
3/5
COMP1005 Exam
Study online at https://quizlet.com/_hcq7qc
x.count(...) shows how many of
each letter is in x
x.index(a letter), shows the index of a chosen letter
24. How do you generate random integers?
random.randint(start, end,
step)
25. How do you generate randome elements from an x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array?
choice = random.choice(x)
print(choice)
26. How do you import Numpy Array?
Import numpy as np
27. How do you create a numpy array?
np.array(object, dtype=None),
dtype is what type each thing
will be, integer, float, str ect...
28. How do you create an array of zeros
np.zeros(amount)
29. How do you create an array of all ones that are
integers?
np.ones(amount, dtype=int)
30. How do you create an array with a constant value
np.full(shape, the fill_value,
dtype)
31. How do you create an array of random float num- np.random.random(low, high,
bers?
size)
32. How do you create an array with a range and step? np.arrange(min, max, step)
33. How do you create an array within a range with a
set amount of elements?
4/5
np.linspace(min, max, amount
of elements)
COMP1005 Exam
Study online at https://quizlet.com/_hcq7qc
34. How do you index a numpy array?
array[index]
35. How do you slice a numpy array?
array[start:stop:step]
36. How do you find sum and min of arrays?
array.sum()
array.mean()
37. How do you get max and min of array?
array.min()
array.max()
38. How do you import matplotlib
import matplotlib.pyplot as plt
39. how do you plot a line graph?
Using two arrays
plt.plot(array1, arrray2), dimensions must be equal, can
also be a two dimensional array
40. How do you show the current plot?
plt.show()
41. How do you create a new figure?
plt.figure()
42. How can you set the title, x label and y label?
plt.title()
plt.xlabel()
plt.ylabel()
43. How do you set x and y limits and ticks
plt.x/ylim(min, max)
plt.x/yticks(ticks)
5/5