Basic Data Structures Flashcards

1
Q

What is a data type?

A

A category for values and every value belongs to exactly one data type.

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

How can you check the type of a variable or value?

A

Use the function type()

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

What is an integer?

A

A value that is a whole number

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

What is the operator for exponent?

A

**

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

What is the operator for modulus?

A

%

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

What is the operator for integer division?

A

//

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

What are the operators for addition, subtraction, multiplication and division?

A

+ - * /

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

How is data typing in Python?

A

Dynamic - variables can change types

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

What is None in Python?

A
The null value, a data type of the class NoneType
different from empty string, zero or False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a use case for the modulus (%)?

A

Perform an action every nth time

E.g. every third session the system will require the user to login

for i in session_number:
if i % 3 == 0:
#require login

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

What is the order of operations in Python?

A
  1. exponentiation and root extraction
  2. multiplication and division
  3. addition and subtraction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you change the order of operations?

A

Use parentheses

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

How can you add a value to an existing variable?

A

Use the += operator

e.g. num = 5
num += 2

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

What happens when you try to divide by 0?

A

You get a ZeroDivisionError

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

What is a floating point?

A

A value with a decimal point

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

What type of data do you get when you divide 2 integers?

A

a float

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

What type of data do you get when you divide 2 integers using the // operator?

A

an integer

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

What is a string?

A

A series or list of characters.
Each character has an index, starting at 0.

Anything inside single (‘’) or double (“”) quotes

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

If name = “Marianne”, name[4] = ?

A

a

20
Q

How do you concatenate two strings?

A

Use the “+” operator

21
Q

How can you concatenate a number to some text?

A

Explicitly convert the integer to a string

e.g. “John is “ + str(6) + “ feet tall.”

22
Q

Print “JohnJohnJohnJohn” without typing John more than once

A

print(“John” * 4)

23
Q

How can you create multi-line strings?

A

Use three single (‘’’) or double (“””) quotes

24
Q

How do you write ‘It’s a lovely day!’ without getting an error?

A

“It’s a lovely day!” or ‘It's a lovely day!’

25
Q

How do you add a new line and a tab to a string?

A

\n for new line

\t for tab

26
Q

What is a string method?

A

An action that Python can perform on a string

27
Q

Change the value of variable “name” so that each word begins with a capital letter

A

name.title()

28
Q

Change the value of variable “name” so that all letters are lowercase

A

name.lower()

29
Q

Change the value of variable “name” so that every letter is uppercase

A

name.upper()

30
Q

What are f-strings?

A

f-strings are string literals that have an f at the beginning and curly braces containing expressions that will be replaced with their values. (source: Real Python)

31
Q

With name = “John”, use f-strings to print “Happy birthday, John”

A

f”Happy birthday, {name}!”

32
Q

What are whitespaces?

A

nonprinting characters (space, tab, end-of-line symbol)

33
Q

How do you strip whitespaces from the right end of a string, from the left end and from both ends?

A

var_name.rstrip()

var_name.lstrip()

var_name.strip()

34
Q

What are booleans?

A

True or False values

35
Q

What Python objects are mutable?

A

list, set, dict

36
Q

What Python objects are immutable?

A

bool, int, float, tuple, str, frozenset

37
Q

What is the difference between mutable and immutable objects?

A

Mutable objects can be changed after they are created, while immutable objects can’t

38
Q

Capitalize the first letter of ‘string’, with all other characters lowercased

A
string.capitalize()
# doesn't modify the original string
39
Q

Get “—John—” from variable name = “John”

A

name.center(10, ‘-‘)

# 10 is the total length of the string after padding
# if no second argument, default padding is blank space
40
Q

What is .casefold() and should I use it?

A

.casefold() is a more aggressive version of .lower() and should be used when dealing with Unicode text or user input.

41
Q

Get the number of times the substring “and” is present in the variable string_var = ‘John and Mary and Simon and Jane’, then assign it to substring_count

A

substring_count = string_var.count(‘and’)

42
Q

Return True if variable ‘proposition’ is a question.

A

proposition.endswith(‘?’)

43
Q

Return the index of first occurrence of substring ‘and’ in string variable ‘names’. If not found, return -1

A

names.find(‘and’)

44
Q

Return True if all the letters in string variable ‘name’ are in lowercase, else return False

A

name.islower()

45
Q

Return True if all characters in string variable ‘name’ are in uppercase, else return False

A

name.isupper()

46
Q

Take variable names = “John, Mary, Rebecca, Oliver” and make a list of these names, called names_list.

A

names_list = names.split(“,”)

47
Q

Turn the value “John and Rebecca” of variable names into “John or Rebecca” without using the assignment operator (‘=’)

A

names.replace(‘and’, ‘or’)