General Python Coding questions Flashcards

(37 cards)

1
Q

How do you determine if a number is divisible by three

A

num % 3 == 0

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

Round a number up

A

math.ceil(n)

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

How do you square a number

A

5**2

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

how to run a math equation?

A

eval(“1+3==4”)

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

how to replace a string character with a different character

A

“xyz”.replace(‘z’,’q’)
result: “xyq”

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

How to extract a list value into another list

A

[*[1,2,3,4],5]

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

How to write a sting in lower case

A

‘ABC’.lower()

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

What does sub() do

A

finds all substrings where the regex pattern matches and then replace them with a different string

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

what does subn() do

A

similar to sub but retuns the new sting along with the number of replacements

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

How to remove values from a python array

A

pop() and remove()
pop return the value removed
remove does not return a value

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

How to import modules in python

A

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

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

What is polymorphism in python

A

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

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

What is used for floor division

A

//

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

What is the maximum possible length of an identifier

A

Identifiers can be of any length

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

how to tell if a file or directory exists?

A

os.path.exists(‘path’)

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

How to open a file

A

with open(‘file path’, ‘r’) as f:
—x = f.read()

With automatically closes the file

17
Q

how to write a file

A

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.

18
Q

How to turn a pandas column into a list

A

df[“column”].tolist()

19
Q

how to change pandas column type

A

df[‘column’].astype(‘str’)

20
Q

In a pandas loc statement what symbol reverses true?

21
Q

List comprehension

A

[x for x in [values] if x > 3]

22
Q

Finding quick general stats about a pandas dataframe

A

df.describe()

23
Q

Find the dataframes datatypes

24
Q

List number of rows and columns in a pandas dataframe

25
list columns in a pandas dataframe
df.columns
26
list general information about a pandas dataframe
df.info()
27
how to get ride of duplicate rows
df.drop_duplicates()
28
Finding the total number of null values in a dataframe
loadmatch.isna().sum() loadmatch.isna().any()
29
getting rid of a panda series rows
df.series.dropna()
30
methods for filtering data that is greater than or less than a specific number.
.le() .ge() ex: df.loc[df.column.ge(2000)]
31
filter dataframe where column contains certain values
.isin() df.loc[df.column.isin(['van','Reefer')]
32
symbols for AND and OR in pandas loc
&, |
33
Pandas, Count the frequency a value appears in a column
.value_count() ex: df['column'].value_count()
34
How to group and aggreagate data
groupby() and aggregate() df.groupby(["Mode", "MonthName"]).agg({"Rate":"sum"})
35
Pandas Example of creating a new column
df.loc[loadmatch.Rate.le(100), "RateCategory"] = "Low" This will add low to where the filter is set.
36
Pandas how to inner join columns by their indexes
.join() ex: df.join(df2, how="inner") ----these will join based on the index set.
37
Pandas how to combine data frames by specifying the columns
pd.merge() ex: pd.merge(dataset1, dataset2, on="Loadnum", how="inner) ex: pd.merge(dataset1, dataset2, left_on=["loadnum", "ActivityDate", right_on=["Loadnum", "ActivityDate"], how="left")