What is an integer and float?
What does this mean:
a = 4
b = 3.142
c = 5.
Basic integer (int) types represent whole numbers, and are useful to act as programming counters, indices, and for basic integer arithmetic. They are made with a variable name, an equals sign = and the value of the integer.
Float datatypes in Python represent decimal numbers, and follow a similar construction pattern to integers, the main difference is that the number has a decimal point after the =
a = 4 as an integer
b = 3.142 as a float
c = 5.0 as a float
If:
a = 5
b = 6.
c = 4
d = 2
What is:
1. print(a - b)
2. print(a + c)
3. print(a/c)
4. print(b/c)
5. print(c**d)
What are strings?
Define:
a = ‘You’
b = “Yeah”
print(b, a)
print( a + ‘smell’)
A string (str) represents text as a sequence of characters, and is used for various tasks such as file input/output processes or printing messages to the user. Strings are defined in Python using either single ‘ or double “
marks around the characters
Yeah you
You smell
What are three data containers and are they for order or unordered data structures?
-For unordered data, we can use a dict (dictionary), which is very efficient for looking up entries in large datasets.
Explain what a the list function is in python
What does this script mean?
a = [ 3, 5, 6, 8]
b = [ 7, 8, 9 ]
c = [ 5, 2, 8]
a. append(3)
b. extend(c)
print(a)
print(b)
[3, 5, 6, 8, 3]
[7, 8, 9, 5, 2, 8]
What does this script mean?
a = [ ‘one’, ‘up’, ‘one’, ‘down’]
a[3] = ‘Smash’
print(a)
print(a[1])
print(a[-2])
[‘one’, ‘up’, ‘one’, ‘Smash’]
up
one
b = [ ‘I’, ‘dont’, ‘actually’, ‘get’, ‘very’, ‘nervous’ ]
b. remove( ‘actually’)
del b[3]
print(b)
c = [ ‘im’, ‘to’, ‘get’, ‘shaky’ ]
c. insert(1, ‘starting’)
print(c)
[‘I’, ‘dont’, ‘get’, ‘nervous’]
[‘im’, ‘starting’, ‘to’, ‘get’, ‘shaky’]
What are Tuples?
What are Dictionaries?
What does this script mean?
Food = {
‘burgers’ : 1,
‘pizza’ : 2,
‘cheese’ : 300,
}
Food[‘paella’] = 4
Food[‘burgers’] = 7
print(Food)
print(list(Food.keys()))
print(list(Food.values()))
print(list(Food.items()))
{‘burgers’: 7, ‘pizza’: 2, ‘cheese’: 300, ‘paella’: 4}
[‘burgers’, ‘pizza’, ‘cheese’, ‘paella’]
[7, 2, 300, 4]
[(‘burgers’, 7), (‘pizza’, 2), (‘cheese’, 300), (‘paella’, 4)]
What is slicing?
a = [5,6,9,2,6,77,7,8,10,23,1]
print(a[1:3])
print(a[:4])
print(a[5:])
print(max(a), min(a))
b = ‘abcdefghijklmnop’
print(b[2:10:2])
print(b[ : : -2])
print(len(a))
print(len(b))
[6, 9]
[5, 6, 9, 2]
[77, 7, 8, 10, 23, 1]
(77, 1)
cegi
pnljhfdb
11
16
What is Control Flow and what are three types?
-Python uses indentation of code with 4 spaces to indicate control flow structure, most text editors will automatically add 4 spaces when TAB key is pressed.
What does this script mean?
a = 6
if (a > 0) and ( a % 2 == 0):
print( ‘ Postive and even’)
b = -7
if (b < 0) and ( a % 2 != 0):
print( ‘negative and odd’)
Positive and even
Negative and odd
What does this script mean?
a = -5
if a > 0:
print(‘Variable is positive’)
elif a == 0:
print(‘Variable is zero’)
else:
print(‘Variable is negative’)
Variable is negative
What are for and while loops?
for i in range(100, 300, 50):
j = i + 10
print(‘Iterating variable is {0} and i + 10 = {1}’.format(i, j))
Iterating variable is 100 and i + 10 = 110
Iterating variable is 150 and i + 10 = 160
Iterating variable is 200 and i + 10 = 210
Iterating variable is 250 and i + 10 = 260
What does this script mean?
D = {
‘Connor’: ‘a tiny willy’,
‘Sam’: ‘no soul’,
‘Joe’: ‘a really interesting diss’,
}
for key, value in D.items():
print(key, ‘has’, value)
(‘Connor’, ‘has’, ‘a tiny willy’)
(‘Sam’, ‘has’, ‘no soul’)
(‘Joe’, ‘has’, ‘a really interesting diss’)
counter = 12
while counter >= 0
print(counter)
counter -= 2
12
10
8
6
4
2
0
What are breaks and continues in a loop?
Using break in a for loop
What does this script mean
for i in range(10):
if i == 5:
break
print(i)
Using continue in a for loop
for i in range(10):
if i % 2 == 0:
continue
print(i)
Output: 0 1 2 3 4 - The break stops the loop when i = 5
Output: 1 3 5 7 9 - the code skips the number if it is even
How would you find what pi is using a python script?
import math
pi = math.pi
print(pi)
This imports the entire maths module the find what pi is
or
from math import pi
print(pi)
This only import pi from the math module
What do the math function ceil, floor and how it it different to the round function ?
round goes to the closest integer
ceil rounds up
floor rounds down