Basic Data Structures Flashcards
What is a data type?
A category for values and every value belongs to exactly one data type.
How can you check the type of a variable or value?
Use the function type()
What is an integer?
A value that is a whole number
What is the operator for exponent?
**
What is the operator for modulus?
%
What is the operator for integer division?
//
What are the operators for addition, subtraction, multiplication and division?
+ - * /
How is data typing in Python?
Dynamic - variables can change types
What is None in Python?
The null value, a data type of the class NoneType different from empty string, zero or False
What is a use case for the modulus (%)?
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
What is the order of operations in Python?
- exponentiation and root extraction
- multiplication and division
- addition and subtraction
How can you change the order of operations?
Use parentheses
How can you add a value to an existing variable?
Use the += operator
e.g. num = 5
num += 2
What happens when you try to divide by 0?
You get a ZeroDivisionError
What is a floating point?
A value with a decimal point
What type of data do you get when you divide 2 integers?
a float
What type of data do you get when you divide 2 integers using the // operator?
an integer
What is a string?
A series or list of characters.
Each character has an index, starting at 0.
Anything inside single (‘’) or double (“”) quotes
If name = “Marianne”, name[4] = ?
a
How do you concatenate two strings?
Use the “+” operator
How can you concatenate a number to some text?
Explicitly convert the integer to a string
e.g. “John is “ + str(6) + “ feet tall.”
Print “JohnJohnJohnJohn” without typing John more than once
print(“John” * 4)
How can you create multi-line strings?
Use three single (‘’’) or double (“””) quotes
How do you write ‘It’s a lovely day!’ without getting an error?
“It’s a lovely day!” or ‘It's a lovely day!’
How do you add a new line and a tab to a string?
\n for new line
\t for tab
What is a string method?
An action that Python can perform on a string
Change the value of variable “name” so that each word begins with a capital letter
name.title()
Change the value of variable “name” so that all letters are lowercase
name.lower()
Change the value of variable “name” so that every letter is uppercase
name.upper()
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)
With name = “John”, use f-strings to print “Happy birthday, John”
f”Happy birthday, {name}!”
What are whitespaces?
nonprinting characters (space, tab, end-of-line symbol)
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()
What are booleans?
True or False values
What Python objects are mutable?
list, set, dict
What Python objects are immutable?
bool, int, float, tuple, str, frozenset
What is the difference between mutable and immutable objects?
Mutable objects can be changed after they are created, while immutable objects can’t
Capitalize the first letter of ‘string’, with all other characters lowercased
string.capitalize() # doesn't modify the original string
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
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.
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’)
Return True if variable ‘proposition’ is a question.
proposition.endswith(‘?’)
Return the index of first occurrence of substring ‘and’ in string variable ‘names’. If not found, return -1
names.find(‘and’)
Return True if all the letters in string variable ‘name’ are in lowercase, else return False
name.islower()
Return True if all characters in string variable ‘name’ are in uppercase, else return False
name.isupper()
Take variable names = “John, Mary, Rebecca, Oliver” and make a list of these names, called names_list.
names_list = names.split(“,”)
Turn the value “John and Rebecca” of variable names into “John or Rebecca” without using the assignment operator (‘=’)
names.replace(‘and’, ‘or’)