Chapters 20-end Flashcards

1
Q

What is a bit?

A

A bit is a bi(nary digi)t. In computing terms it represents a 1 or a 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a byte?

A

In computer convention a byte is a sequence of 8 bits. The range of values accommodated can range from binary 0 - 00000000 to binary 255 - 11111111

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can a byte be used to represent values?

A

A byte has 256 possible values (0-255). By associating a value with a letter, character or symbol - the byte can be used to encode the values of a letter, character or symbol into a byte. Strings of bytes can then be used to represent information (e.g. words).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is ASCII?

A

ASCII is the American Standard Code for Information Interchange. It is an encoding or codec that was one of the first to be internationally accepted. Within the 255 values available most letters, characters and symbols required in common Western European languages (e.g. English) can be accommodated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Unicode and why do we need it?

A

ASCII can only handle English and a few other European languages in its 255 character codec.

Unicode is a Universal encoding that can handle all human languages. Unicode uses between 1 and 4 bytes to represent each character so up to 32 bits can be used to encode characters giving around 4,000 million possible characters.

Unicode is commonly described as UTF (Unicode Transformation Format) with UTF-8, UTF-16 and UTF-32 bit formats available

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is UTF-8?

A

UTF-8 stands for Unicode Transformation Format 8 bit. UTF-8 is a simplified Universal encoding that can handle most human languages. Unicode uses between 1 and 4 bytes to represent each character. UTF-8 is backwards compatible with ASCII.

UTF-8 is the most commonly used codec - used in over 95% of applications

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In Python how do you convert a decimal value to binary?

A

Use bin()

z = bin(125)
populates z with the string ‘0b1111101’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does ord() do in Python?
e.g.

print(ord(‘h’))

A

ord() returns the unicode encoding number that represents the specified character

e.g.
print(ord(‘h’)) returns 104 (the ascii / utf value for ‘h’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does chr() do in Python?
e.g.

print(chr(97))

A

chr() returns the character represented by the specified unicode number

e.g.
print(chr(97)) returns ‘a’ - the unicode character mapped by the value 97

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe how the internal representation of a string is handled in Python

A

In Python a string is a UTF-8 encoded sequence of characters for displaying or working with text.

Text entered from keyboard or read from files by default is captured as text and encoded as a UTF-8 encode sequence of characters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does DBES mean?

A

Decode bytes and encode strings.

If you have a string and want to get the underlying bytes then you need to encode it to give a string of raw bytes e.g. b’\xe6\x96\x87\xe8\xa8\x80’

my\_string = '文言'
print('original string is ',my\_string)
raw\_bytes = my\_string.encode() 
print('encoded raw bytes is ',raw\_bytes) 
 # outputs b'\xe6\x96\x87\xe8\xa8\x80'
decoded\_bytes = raw\_bytes.decode() 
print('decoded raw bytes is ',decoded\_bytes)
 # outputs '文言'

If you have a string and want to do an operation on it usually it will work but sometimes Python will throw up an error saying it doesn’t know how to encode it. In that case you must use .encode to get the bytes you need.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you create python documentation for your own functions and classes? e.g. so that when someone types help(my_function) in python command line they will see your help doco

A

Add a comment enclosed by “”” quotes immediately after the function definition line

def break\_words(stuff): 
 """This function will break up words for us""" 
 words = stuff.split(' ') # words breaks the sentence up and stores individual words in a list 
 # the sentence is broken into str elements whenever a ' ' occurs 
 # other delimiters could be used for other effects 
 return words
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you access functions in an imported module e.g. call a function in mymodule?

A

import mymodule

requires each call to start with

mymodule.my_function()

With

from mymodule import *

means you can simply call code direct without referencing the module name

my_function()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If stuff is a sentence (str) what does the following code do?

words = stuff.split(‘ ‘)

A

stuff is broken up based on each occurence of a space (‘ ‘)and a new list (words) is created to store each occurence. Spaces are discarded.

Works with other delimiters too.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If stuff is a sentence (str) what does the following code do?

words = stuff.split(' ') 
sorted\_words = sorted(words)
A

words creates a list containing the individual words in the sentence.

Using the inbuilt function sorted() creates a new sorted list of the words

e.g.
stuff = “All good things come to those who wait.”

words => [‘All’, ‘good’, ‘things’, ‘come’, ‘to’, ‘those’, ‘who’, ‘wait.’]

sorted_words => [‘All’, ‘come’, ‘good’, ‘things’, ‘those’, ‘to’, ‘wait.’, ‘who’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does pop() do? What about pop(0) and pop(-1)?

A

pop() removes the end element in a list from the list as does pop(-1).

pop(0) removes the first element in a list.

>>> sorted_words
[‘All’, ‘come’, ‘good’, ‘things’, ‘those’, ‘to’, ‘wait.’, ‘who’]
>>> sorted_words.pop()
‘who’
>>> sorted_words
[‘All’, ‘come’, ‘good’, ‘things’, ‘those’, ‘to’, ‘wait.’]

17
Q

what is a common cause of the error
SyntaxError: invalid syntax

A

check for missing previous line completion e.g. no ) or :

18
Q

NOT Truth tables - complete the table True or False?

NOT

not False =>

not True =>

A

NOT Truth tables - complete the table True or False?

not False => True

not True => False

19
Q

OR Truth Tables - complete the answer - true or false?

OR

True OR False =>

True OR True =>

False OR True =>

False OR False =>

A

OR Truth Tables - complete the answer - true or false?

True OR False => True

True OR True => True

False OR True => True

False OR False => False

with OR only False or False yields false every other option is True

20
Q

AND Truth Tables - complete the answer - true or false?

AND

True AND False =>

True AND True =>

False AND True =>

False AND False =>

A

AND Truth Tables - complete the answer - true or false?

AND

True AND False => False

True AND True => True

False AND True => False

False AND False => False

with AND only True and True yields True every other option is False

21
Q

NOT OR Truth Tables - complete the answer - true or false?

NOT OR

NOT (True OR False) =>

NOT (True OR True) =>

NOT (False OR True) =>

NOT (False OR False) =>

A

NOT OR Truth Tables - complete the answer - true or false?

NOT OR

NOT (True OR False) => False

NOT (True OR True) => False

NOT (False OR True) => False

NOT (False OR False) => True

with NOT OR only NOT (False and False) yields True

22
Q

NOT AND Truth Tables - complete the answer - true or false?

NOT AND

NOT (True AND False) =>

NOT (True AND True) =>

NOT (False AND True) =>

NOT (False AND False) =>

A

NOT AND Truth Tables - complete the answer - true or false?

NOT AND

NOT (True AND False) => True

NOT (True AND True) => False

NOT (False AND True) => True

NOT (False AND False) => True

with NOT AND only NOT (True and True) yields False

23
Q

Complete the following != (not equals) truth table:

1 != 0

1 != 1

0 != 1

0 != 0

A

Complete the following != (not equals) truth table:

1 != 0 => True

1 != 1 => False

0 != 1 => True

0 != 0 => False

24
Q

Complete the following = = (equals) truth table:

1 = = 0

1 = = 1

0 = = 1

0 = = 0

A

Complete the following = = (equals) truth table:

1 = = 0 => False

1 = = 1 => True

0 = = 1 => False

0 = = 0 => True

25
Q

What does

False and 0 != 0

evaluate to?

A

False

26
Q

What does

1 != 0 and 2 == 1

evaluate to?

A

False

27
Q

What does

not (1!=10 or 3==4)

evaluate to?

A

False

28
Q

How do you write an if statement, esle if statement and an else statement in Python

e.g.

if a>b then print(“a is greater than b”)

otherwise if a < b print(“a is less than b”)

else print(“a is equal to b”)

A

if a > b:
print(f”a {a} is greater than b {b}”)
elif a < b:
print(f”a {a} is less than b {b}”)
else:
print(f”a {a} is equal to b {b}”)

the colon indicates a conditional block follows

conditional blocks MUST be indented by 4 spaces

note no conditional statement can come after else and before the colon

29
Q

How do you write an if statement in Python

e.g. if a>b the print(“a is greater than b”)

A

if a > b :

print(“a is greater than b”)

the colon indicates a conditional block follows

conditional blocks must be indented by 4 spaces

30
Q

What happens if multiple elif statements in a block are True?

A

Python executes the first one that evaluates to True and passes by the rest without execution

e.g.

a = '1'
b = '2'

if a == ‘1’:
print(f”a = 1 - {a} is 1”)
elif b == ‘2’:
print(f”b = 2 - {b} is 2”)

yields

a = 1 - 1 is 1

31
Q

What is a list in Python?

A

Lists are used to store multiple items in a single variable.

Lists are one of 4 built-in data types in Python used to store collections of data.

Lists are created using square brackets: []

thislist = [“apple”, “banana”, “cherry”]
print(thislist)

32
Q

How do you create an empty list?

How do you add to an empty list?

A

>>> mylist = [] # empty list
>>> mylist.append(‘1st’)
>>> mylist.append(2)
>>> mylist.append([1,2,3])
>>> mylist
[‘1st’, 2, [1, 2, 3]]
>>>

33
Q

What are the key characteristics of a list in Python?

A

Lists are used to store multiple items in a single variable - they are ordered, changeable, and allow duplicate values.

Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

List items are ordered, changeable, and allow duplicate values.

List items are indexed, the first item has index [0], the second item has index [1] etc.

When we say that lists are ordered, it means that the items have a defined order, and that order will not change. If you add new items to a list, the new items will be placed at the end of the list. Note: There are some list methods that will change the order, but in general: the order of the items will not change.

The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.

Allow duplicates - Since lists are indexed, lists can have items with the same value.

34
Q

How do for loops work in Python? Give two examples.

A

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). It also works with range().

This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

fruits =[‘apples’, ‘oranges’, ‘pears’, ‘apricots’]
for fruit in fruits:
print(f”A fruit of type: {fruit}”)

elements = []
for i in range(0,6):
print(f”Adding {i} to the list.”)
# append is a function that lists understand
elements.append(i)

35
Q

What does the following code snippet output?

elements = []
for i in range(0,6):
print(f”Adding {i} to the list.”)
# append is a function that lists understand
elements.append(i)

print(elements)

A

Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
[0, 1, 2, 3, 4, 5]