Python Flashcards

(31 cards)

1
Q

Name some features of tuples? As well provide an example of how one is constructed?

A

A tuple is a function that can return multiple values. Similar to a list but the values cannot be modified. An example of how one is constructed shown below:
even_nums = (2, 4, 6)

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

How does list slicing work in python?

A

List slicing has the following structure: [start (inclusive):end(exclusive)]
For example the command List[3:5] will return the elements 3 and 4 in the list but not the 5th element

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

What is the syntax used for the round function?

A

round(number to round, #decimal digits to round to)
For example: round( 1.68, 1)
equals 1.7

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

What are methods in python

A

Functions that belong to objects. Called using the following syntax:
object.method(). For example in order to find the index for a particular element in a list the following would be used:
List.index(element)

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

What does the append method do?

A

Adds a new element to an existing list. The new element is added to the end of the list.

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

How do you import just a single function from a library?

A
Use the following command at the beginning of the sub-procedure:
from library (numpy) import  function (array)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you turn a list into an array?

A

1.) Import numpy as np
2.) Convert a list such as “height” using the following syntax:
np_height=np.array(height)

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

What is the command for importing Matplotlib

A

import matplotlib.pyplot as plt

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

How to plot lists on a line graph?

A

plt.plot( List1 (x-axis), List2 (y-axis))

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

What method is used to display the graph?

A

The show method used as follows plt.show()

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

How to plot a histogram ?

A

Use the following command: plt.hist( list, bins = x)

the number of bins is equal the amount of bars in the histogram.

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

What are the commands for the labeling the x and y axis as well as the title?

A

plt. xlabel( ‘x axis label’)
plt. ylabel(‘y axis label’)
plt. title(‘graph title’)

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

How is a dictionary data structure constructed?

A

list = { “key1” : value1, “key2”: value2, “key3”:value3}
In order to access a value use the following command?
list[“key1”] = value1

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

How to create a data frame in python?

A
  1. ) import pandas as pd

2. ) variable =pd.dataframe(dict)

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

How to change index values in a dataframe?

A

dataframe.index = [“index1”, “index2”, “index3”……]

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

How to convert a CSV file into a dataframe?

A
  1. ) Import pandas as pd

2. ) dataframe_name = pd.read_csv(“path/to/filename.csv”)

17
Q

How do you access a particular row or column in pandas dataframe using index names?

A

dataframe.loc[“index1”]

18
Q

How do you access several rows in a pandas dataframe using index names?

A

dataframe.loc[[“index1”, “index2”, “index3”]]

19
Q

How do you access several columns in a pandas dataframe using index names?

A

dataframe.loc[:, [“index1”, “index2”, “index3”]]

20
Q

How do you access rows in a pandas dataframe using index numbers?

A

dataframe.iloc[1]

21
Q

What is the syntax for the while looping construct?

A

while condition :

expression

22
Q

What is the syntax for a for loop construct

A

for var in seq:

expression

23
Q

What is the syntax used for constructing a for loop for dictionary data structure?

A

for key, value in dictionary.items():

expression

24
Q

What is the syntax used for constructing a for loop for numpy array data structure?

A

for val in np.nditer(array):

expression

25
What are some data structures that would be classified iterators?
Types of data structures that would be considered to be interators lists, strings, dictionaries,file connections. Applying the iter() method to an interator creates iterable.
26
How do you iterate over file connections?
1.) Open file: file = open('file.txt') 2.) Create iterable:it = iter(file) 3.) Use the next function: print(next(it)) - 'This is the first line' print(next(it)) - 'This is the second line'
27
What is the syntax for a list expression?
[expression for item in list if conditional]
28
How do you create a database engine in python
1. ) from sqlalchemy import create_engine | 2. ) engine = create_engine('sqlite:///databasename.sqlite')
29
Provide an example of how all steps that would need to be taken in order to create SQL Query?
from sqlalchemy import create_engine import pandas as pd engine = create_engine('sqlite:///databasename.sqlite') con = engine.connect() rs = con.execute("SELECT * From Orders") df = pd.DataFrame(rs.fetchall()) con.close()
30
Show an example of how to use the context manager
from sqlalchemy import create_engine import pandas as pd engine = create_engine('sqlite:///databasename.sqlite') with engine.connect() as con: rs = con.execute("SELECT OrderID, OrderDate, ShipName from Orders") df =pd.DataFrame(rs.fetchmany(size=5)) df.columns = rs.keys()
31
Show an example of how to use an inner join function in pandas
df = pd.read_sql_query("SELECT Order ID, CompanyName From Orders Inner Join Customers on Orders.CustomerID = Customers.CustomerID", engine)