General Python Coding questions Flashcards
(37 cards)
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’)
How to open a file
with open(‘file path’, ‘r’) as f:
—x = f.read()
With automatically closes the file
how to write a file
with open(‘file path’, ‘w’) as f:
—-f.write(‘text string here’)
this will create a new file if it doesn’t exist and over write that file if it already exists.
How to turn a pandas column into a list
df[“column”].tolist()
how to change pandas column type
df[‘column’].astype(‘str’)
In a pandas loc statement what symbol reverses true?
~
List comprehension
[x for x in [values] if x > 3]
Finding quick general stats about a pandas dataframe
df.describe()
Find the dataframes datatypes
df.dtypes
List number of rows and columns in a pandas dataframe
df.shape