General Python Coding questions Flashcards
How do you determine if a number is divisible by three
num % 3 == 0
Round a number up
math.ceil(n)
How do you square a number
5**2
how to run a math equation?
eval(“1+3==4”)
how to replace a string character with a different character
“xyz”.replace(‘z’,’q’)
result: “xyq”
How to extract a list value into another list
[*[1,2,3,4],5]
How to write a sting in lower case
‘ABC’.lower()
What does sub() do
finds all substrings where the regex pattern matches and then replace them with a different string
what does subn() do
similar to sub but retuns the new sting along with the number of replacements
How to remove values from a python array
pop() and remove()
pop return the value removed
remove does not return a value
How to import modules in python
import array #importing using the original module name
import array as arr # importing using an alias name
from array import * #imports everything present in the array module
What is polymorphism in python
Polymorphism means the ability to take multiple forms. for instance if the parent class has a method named ABC then the child class also can have a method with the same name ABC having its own parameters and variables. Python allows this
What is used for floor division
//
What is the maximum possible length of an identifier
Identifiers can be of any length
how to tell if a file or directory exists?
os.path.exists(‘path’)