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] = ?

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
How do you add a new line and a tab to a string?
\n for new line | \t for tab
26
What is a string method?
An action that Python can perform on a string
27
Change the value of variable "name" so that each word begins with a capital letter
name.title()
28
Change the value of variable "name" so that all letters are lowercase
name.lower()
29
Change the value of variable "name" so that every letter is uppercase
name.upper()
30
What are f-strings?
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
With name = "John", use f-strings to print "Happy birthday, John"
f"Happy birthday, {name}!"
32
What are whitespaces?
nonprinting characters (space, tab, end-of-line symbol)
33
How do you strip whitespaces from the right end of a string, from the left end and from both ends?
var_name.rstrip() var_name.lstrip() var_name.strip()
34
What are booleans?
True or False values
35
What Python objects are mutable?
list, set, dict
36
What Python objects are immutable?
bool, int, float, tuple, str, frozenset
37
What is the difference between mutable and immutable objects?
Mutable objects can be changed after they are created, while immutable objects can't
38
Capitalize the first letter of 'string', with all other characters lowercased
``` string.capitalize() # doesn't modify the original string ```
39
Get "---John---" from variable name = "John"
name.center(10, '-') ``` # 10 is the total length of the string after padding # if no second argument, default padding is blank space ```
40
What is .casefold() and should I use it?
.casefold() is a more aggressive version of .lower() and should be used when dealing with Unicode text or user input.
41
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
substring_count = string_var.count('and')
42
Return True if variable 'proposition' is a question.
proposition.endswith('?')
43
Return the index of first occurrence of substring 'and' in string variable 'names'. If not found, return -1
names.find('and')
44
Return True if all the letters in string variable 'name' are in lowercase, else return False
name.islower()
45
Return True if all characters in string variable 'name' are in uppercase, else return False
name.isupper()
46
Take variable names = "John, Mary, Rebecca, Oliver" and make a list of these names, called names_list.
names_list = names.split(",")
47
Turn the value "John and Rebecca" of variable names into "John or Rebecca" without using the assignment operator ('=')
names.replace('and', 'or')