Data Science Flashcards
dictionaries
type of data type like list. uses { } is define it
The built-in datatypes in Python is called dictionary. It defines one-to-one relationship between keys and values. Dictionaries contain pair of keys and their corresponding values. Dictionaries are indexed by keys.
countries = {“afghanistan”: 30.55, “albania”:2.77, “algeria”: 40.33}
dict_name[key]
result: value
pandas
helps import data from csv into python as tabular form
loc in panadas
uses labels for slicing/filtering
print(cars.loc[[‘AUS’, ‘EG’]])
iloc in pandas
uses indexes for slicing/filtering
print(cars.iloc[2])
if then stmt
if condition:
expression
boolen ops for Numpy arrays
use np.logical_or, np.logical_and ,
np.logical_and
while loop
The while loop is like a repeated if statement. The code is executed over and over again, as long as the condition is True
while condition:
expression
What is the difference between list and tuples?
Lists are mutable i.e they can be edited. Syntax: list_1 = [10, ‘Chelsea’, 20]
Tuples are immutable (tuples are lists which can’t be edited). Syntax: tup_1 = (10, ‘Chelsea’ , 20)
Explain Inheritance in Python with an example
Inheritance allows One class to gain all the members(say attributes and methods) of another class. Inheritance provides code reusability, makes it easier to create and maintain an application. The class from which we are inheriting is called super-class and the class that is inherited is called a derived / child class.
Help() function:
The help() function is used to display the documentation string and also facilitates you to see the help related to modules, keywords, attributes, etc.
Dir() function
The dir() function is used to display the defined symbols.
What does this mean: *args, **kwargs?
We use *args when we aren’t sure how many arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function. **kwargsis used when we don’t know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments.
Write a one-liner that will count the number of capital letters in a file. Your code should work even if the file is too big to fit in memory.
with open(SOME_LARGE_FILE) as fh: count = 0 text = fh.read() for character in text: if character.isupper(): count += 1
We will now try to transform this into a single line. count sum(1 for line in fh for character in line if character.isupper())
How can you randomize the items of a list in place in Python?
from random import shuffle
x = [‘Keep’, ‘The’, ‘Blue’, ‘Flag’, ‘Flying’, ‘High’]
shuffle(x)
print(x)
[‘Flying’, ‘Keep’, ‘Blue’, ‘High’, ‘The’, ‘Flag’]
Write a sorting algorithm for a numerical dataset in Python.
list = [“1”, “4”, “0”, “6”, “9”]
list = [int(i) for i in list]
list.sort()
print (list)
NOTE: if list held integers then you can just to list.sort()
but here we first had to convert string into integer
How can you generate random numbers in Python?
import random
random.random
randrange(a, b): it chooses an integer and define the range in-between [a, b). It returns the elements by selecting it randomly from the range that is specified. It doesn’t build a range object.
What is pickling and unpickling?
Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.
How do you calculate percentiles with Python/ NumPy?
import numpy as np
a = np.array([1,2,3,4,5])
p = np.percentile(a, 50) #Returns 50th percentile, e.g. median
print(p)
Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1] ?
a) Error
b) None
c) 25
d) 2
c) 25