PythonBasics Flashcards

1
Q

print working directory

A

pwd

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

my computer’s network name

A

hostname

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

make directory

A

mkdir

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

change directory

A

cd

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

list directory

A

ls

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

what should you do if you get lost in terminal?

A

write “pwd” to print working directory and next “cd ~ ” to get back to home

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

How do you create a folder with multiple words in the name?

A

write mkdir for creating a folder and then put “” around the name. For example,…. Mkdir “that’s pretty cool”.

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

remove directory

A

rmdir

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

What does “cd ..” do?

A

it takes you back one folder from for example /desktop/temp/stuff to /desktop/temp, This can also be done for multiple folders back with ../../.. (3 folders back)

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

What does CLI mean?

A

Command line interface

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

What does GUI mean?

A

graphical user interface

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

What will “ls -1R” do?

A

it will show you an overview of all folders below your current position.

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

push directory

A

pushd (go to new location)

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

pop directory

A

popd (return to saved location)

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

what does “mkdir -p” do?

A

lets you create an entire directory at once aka. Multiple folders in a tree. Mkdir -p kap/jokap/chakap/ja/sådan/er/det

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

copy a file or directory

A

cp

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

move a file or directory

A

mv

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

What can be the problem if you are not allowed to rmdir a directory?

A

First of all there could be files within that folder you try to delete. Secondly, if you are 100 % sure that there is nothing, it may be due to the Mac OSX issue of an “.DS_Store” file, which you can delete by going to the folder and write “rm -rf .DS_Store”

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

page through a file

A

less

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

print the whole file

A

cat

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

What will
mkdir something
cp awesome.txt something/
Do?

A

first we create a new folder/directory. Next we make a copy of the file awesome.txt from current pwd to this new folder called “something”

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

create a new file

A

touch (write…. “touch iamcool.txt” for example)

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

How do we copy a file?

A

go to the directory of the file and write “cp filename.txt newfilesname.text”

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

execute arguments

A

xargs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
find files
find
26
how do you rename a file?
use mv (move a file) so you write “mv filename.doc newfilename.doc”. you can check the name change after by listing the directory content with ls
27
how can you see the content of a file?
write “less filename.txt” and it will show you the content. To quit, just type q
28
How can you remove a FILE?
just write “rm filename.doc” (you can also remove multiple files at once by listing them)
29
How can you remove a folder/directory?
rmdir foldername
30
How can you delete a file within a folder?
rm foldername/filename.txt
31
How can you delete a folder that contains files?
rm -rf foldername
32
Whats the “execute” command?
xargs
33
find things inside files
grep
34
read a manual page
man
35
find what man page is appropriate
apropos
36
look at your environment
env
37
print some arguments
echo
38
export/set a new environment variable
export
39
exit the shell
exit
40
What is the command to become a super-user?
sudo makes you a super-user and lets you force through more commands
41
Which command can you use to change the access permissions to a file?
chmod
42
How can you change the file owner?
use chown
43
How do you run a python file from terminal?
Just write “python filename.py”
44
**What does # do in python and what is it called?**
A hash. If you put a hash (#) in front of a line of code, it will not print / go in effect so you can use it for making comments
45
How can you print a hash/octothorpe? “#”
you put it inside strings; print “we have a # inside of the strings now” # the hash inside the strings will be printed but this comment after the second # will not be printed.
46
What is / called and what is it?
slash and it is the division sign
47
What is \* called and what is it?
asterisk and it is the multiplier sign
48
How do you make additions?
with “+” for example “print 3 + 2” will output 5
49
How do you make subtractions?
with “-” for example “print 3 - 4” will output -1
50
What is “%” and what does it do?
percent and it works as ‘X divided by Y with J remaining’. For example, “100 divided by 16 with 4 remaining” so print “100 % 16” will be 4. While 100 % 10 will be 0.
51
What are floating point number?
Essentially just numbers that include decimals such as 5.5 or 0.02
52
What output will print “20/3” give?
6… if you want the precise output including decimals you need to write print “20.0/3.0) and you will get 6.666666666666667
53
how do you write less-than-equal?
\<=
54
how do you write greater-than-equal?
\>=
55
What is the order of mathematical operations?
PEMDAS, Parentheses, exponents, multiplication, division, addition, subtraction
56
How do you create a variable in python?
you basically just write out with a “=” sign in-between so for example, “Cars = 100” # creates a variable that ‘cars’ is equal to 100 “cars\_not\_driven = cars – drivers” # creates a variable for available cars (cars\_not\_driven)
57
What is the difference between = (single-equal) and == (double-equal)?
The = (single-equal) assigns the value on the right to a variable on the left. The == (double-equal) tests if two things have the same value.
58
Can we write x=100 instead of x = 100?

You can, but it’s bad form. You should add space around operators like this so that it’s easier to read.
59
**How do you define number and character variables?**
Examples… My\_name = “Lars Horsbol” And My\_age = 23
60
How do you insert variables inside printed text?
you use %s for character-variables and %d for number variables. For example, Print “Let’s talk about %s” % my\_name And Print “He’s %d centimeters tall.” % my\_age
61
**How should you refer to a number-variable inside printed text?**
With %d Print “He’s %d centimeters tall.” % my\_age
62
**How should you refer to a character-variable inside printed text?**
With %s Print “Let’s talk about %s” % my\_name
63
**How do you add variables together inside printed text?**
You just write them out in the %d. print "If I add %d, %d, and %d I get %d." % (my\_age, my\_height, my\_weight, my\_age + my\_height + my\_weight) here we are trying to add the variables together
64
**How does the %r format work?**
If you use %r, it will print no matter what, meaning that it will both print a character-variable (normally %s) and a number-variable (normally %d). print "and with both character %r AND number %r" % (height, hair) # with %r we will end up printing both the character-variable AND the number-variable and with both character 192 AND number 'Dark-blond'
65
**What are the different conversion types in Python and which ones are most common?**
%d = signed integer decimal %s = character-variables %r = ”print this no matter what”
66
What’s the point of %s and %d when you can just use %r?

The %r is best for debugging, and the other formats are for actually displaying variables to users.
67
Can I make a variable like this: 1 = 'Zed Shaw'?
No, the 1 is not a valid variable name. They need to start with a character, so a1 would work, but 1 will not.
68
**How can we insert a number inside “x = “There are %d types of people” ?**
We write “% 10” x = "There are %d types of people." % 10 # here we write a string and insert a variable with % outside the string
69
**How can you combine two strings such as:** ## Footnote **w = "this is the left side of..."** **e = "a string with a right side." ??**
just write; print w + e
70
How can I round a floating point number?
You can use the **round()** function like this: **round(1.7333)** or round(weight / pounds\_to\_kilo)
71
Can I use single-quotes or double-quotes to make a string or do they do different things?
In Python either way to make a string is acceptable, although typically you’ll use single-quotes for any short strings like 'a' or 'snow' and double-quotes for sentences.
72
Should I use %s or %r for formatting?

You should use %s and only use %r for getting debugging information about something. The %r will give you the “raw programmer’s” version of variable, also known as the “representation.”
73
**Why do you put** **'** **(single-quotes) around some strings and not others?**
Mostly it’s because of style, but I’ll use a single-quote inside a string that has double-quotes. Look at line 10 to see how I’m doing that. print "I also said: '%s'." % y
74
What will; print “.” \* 10 do?
this will result in 10\*. So the output will be; ……….
75
**If, end1 = "C"** **end2 = "h"** **end3 = "e"** **end4 = "e"** **end5 = "s"** **end6 = "e"** **end7 = "b"** **end8 = "u"** **end9 = "r"** **end10 = "g"** **end11 = "e"** **end12 = "r",** **what will;** **print end1 + end2 + end3 + end4 + end5 + end6** **print end7 + end8 + end9 + end10 + end11 + end12** **result in?**
Just. Cheese burger On two different lines because there is no comma after “end6”.
76
**Why do I have to put quotes around “one” but not around** **True** **or** **False****?**
That’s because Python recognizes True and False as keywords representing the concept of true and false. If you put quotes around them, then they are turned into strings and won’t work right.
77
**If, end1 = "C"** **end2 = "h"** **end3 = "e"** **end4 = "e"** **end5 = "s"** **end6 = "e"** **end7 = "b"** **end8 = "u"** **end9 = "r"** **end10 = "g"** **end11 = "e"** **end12 = "r",** **what will;** **print end1 + end2 + end3 + end4 + end5 + end6,** **print end7 + end8 + end9 + end10 + end11 + end12** **result in?**
Just. Cheese burger On two different lines because there is a comma after “end6” it stays on the same line in the printed output.
78
What will; print "Its fleece was white as %s." % 'snow' result in?
Its fleece was white as snow.
79
**If you have; formatter = "%r %r %r %r" , what will happen if you write print formatter % (formatter, formatter, formatter, formatter)**
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
80
**If you have; formatter = "%r %r %r %r" , what will happen if you write;** **print formatter % (** **"I had this thing.",** **"That you could type up right.",** **"But it didn't sing.",** **"So I said goodnight."** **)**
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
81
Why do I have to put quotes around “one” but not around True or False?
That’s because Python recognizes True and False as keywords representing the concept of true and false. If you put quotes around them, then they are turned into strings and won’t work right.
82
I tried putting Chinese (or some other non-ASCII characters) into these strings, but %r prints out weird symbols.

Use %s to print that instead and it’ll work.
83
**Why does** **%r** **sometimes print things with single-quotes when I wrote them with double-quotes?**
Python is going to print the strings in the most efficient way it can, not replicate exactly the way you wrote them. This is perfectly fine since %r is used for debugging and inspection, so it’s not necessary that it be pretty.
84
Why do the \n newlines not work when I use %r?

That’s how %r formatting works; it prints it the way you wrote it (or close to it). It’s the “raw” format for debugging.
85
**What are two ways to make a string that goes across multiple lines?**
1. You can use \n è "nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" 2. You can use a triple double quote “”” ………… “””
86
**What will making three double-quotes allow you to do?** **“”” “””**
Write a lot of text within the “”” here “”” on multiple different lines print """ There's something going on here. With the three double-quotes. We'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6. """
87
**How do you write some text that is tabbed in?** **Like this?**
You use; \t so for example Print "\tI'm tabbed in."
88
**How do you write a string of text and then split the text on different lines?**
You use \n so for example… Print "I'm split\non a line." Will become; I’m split On a line
89
**What will; print "I'm \\ a \\ cat." ; result in?**
I’m \ a \ cat So it removes one of the backlashes
90
**How can you produce a list of points?**
91
What does the escape sequences \\ do?
92
**What does the escape sequence** \’ do?
93
What does the escape sequence; \a do?
It inserts an extra space. So; Print “hey man\a whats up” Becomes Hey man whats up
94
What does the escape sequence \b do?
95
What does the escape sequence \n do?
96
What does the escape sequence \r do?
97
What does the escape sequence \t do?
98
When I use a %r format none of the escape sequences work.
That’s because %r is printing out the raw representation of what you typed, which is going to include the original escape sequences. Use %s instead. Always remember this: %r is for debugging; %s is for displaying.
99
**What is the function of the comma in this:** **print "How old are you?",** **age = raw\_input()**
the function of the comma is to make sure that the output is on the same line. With comma we get; How old are you? 23 Without comma we get: How old are you? 23
100
**What is raw\_input in python and what is it useful for?**
Raw\_input lets you interact with the outside world to get input. The raw\_input() function waits for the user to type some input and press return. It then gets whatever was typed. (called input() in python 3 but here it also tries to convert the input whereas rawinput from python 2 simply takes the raw input which is usually easier to work with/safer)
101
**How can you insert prompts for raw\_input requests?**
Just write questions or prompts inside the string of the raw\_input() like this; height = raw\_input("How tall are you? Fx. 1.82 m")
102
**How can you look up what any function in python does from within the terminal?**
103
**Why would I use %r over %s?**
Remember, %r is for debugging and is “raw representation” while %s is for display. I will not answer this question again, so you must memorize this fact. This is the #1 thing people ask repeat- edly, and asking the same question over and over means you aren’t taking the time to memorize what you should. Stop now, and finally memorize this fact.
104
**What do you also call a “.py” file?**
A script
105
**What are modules?**
Modules are features you can add in your python script. Some people also called modules for libraries. It could for example be: sys and argv
106
Python filename.py arg1 arg2 arg3 So for example Python e13.py I want snacks Will give… The script is called: e13.py Your first variable is: i Your second variable is: want Your third variable is: snacks
107
**What happens if you input less** **arguments for argv when trying to run in the command line?**
108
What’s the difference between argv and raw\_input()?
The difference has to do with where the user is required to give input. If they give your script inputs on the command line, then you use argv. If you want them to input using the keyboard while the script is running, then use raw\_input().
109
**How can you use raw\_input prompts for multiple places in your script?**
110
**Does txt = open(filename) return the contents of the file?**
No, it doesn’t. It actually makes something called a “file object.” You can think of it like an old tape drive that you saw on mainframe computers in the 1950s or even like a DVD player from today. You can move around inside them, and then “read” them, but the file is not the contents
111
**What does from sys import argv mean?**
For now, just understand that sys is a package, and this phrase just says to get the argv feature from that package. You’ll learn more about these later.
112
**Why is there no error when we open the file twice?**
Python will not restrict you from opening a file more than once, and in fact sometimes this is necessary.
113
**Which command will let you close files?**
Close
114
**Which command will let you read files?**
Read
115
Which command will let you open files?
Open
116
Which command will let you read just one line?
Readline
117
Which command will let you empty a file for its content? (be careful)
Truncate
118
**Which command will let you write stuff into your file?**
Write(stuff)
119
**What could we write to open a file? (existing or not)**
120
**What could we write to open and read a file?**
121
**What does the file modes “r” do in python?**
This is the default mode. It Opens file for reading.
122
**What does the file modes “w” do in python?**
This Mode Opens file for writing. If file does not exist, it creates a new file. If file exists it truncates the file.
123
**What does the file modes “x” do in python?**
Creates a new file. If file already exists, the operation fails.
124
**What does the file modes “a” do in python?**
Open file in append mode. If file does not exist, it creates a new file.
125
**What does the file modes “t” do in python?**
This is the default mode. It opens in text mode.
126
**What does the file modes “b” do in python?**
This opens in binary mode.
127
**What does the file modes “+” do in python?**
This will open a file for reading and writing (updating)
128
How can you write a simple script that will open a file specified in command/terminal?
129
If you open the file with 'w' mode, then do you really need the target.truncate()?
No. It doesn’t make any difference as the “w-mode” will overwrite the existing file so emptying that file first makes no difference.
130
**Does just doing open(filename) open it in 'r' (read) mode?**
Yes, that’s the default for the open() function.
131
**What can the “import” statement do?**
With the import statement we can import new modules to python. Some modules/functions are existing in your python program already, but sometimes you may want to add new functions. Here, it can be smart to import code written by others than to innovate yourself. You can import such new modules with the import statement.
132
Why do you have to do output.close() in the code when the file is already closed?
It is good practice to always close files so you avoid later commands working with those files from reading descriptions from those closed files.
133
**What does the len() function do?**
It gets the length of the string that you pass to it and then returns that as a number. Play with it.
134
**What do functions do? (put simply)**
1. They name pieces of code the way variables name strings and numbers. 2. They take arguments the way your scripts take argv. 3. Using #1 and #2, they let you make your own “mini-scripts” or “tiny commands.” Think of “function” as a mini-script
135
**How do you create a function in python?**
Write; def (for define)
136
The first function is overly complicated. It takes the look of when we work with args and does uses \*args inside print\_two. On the line below, it unpacks those args to finally print it. There is no need to do this unpacking. We can simply put arg1, arg2 inside the function from the beginning.
137
**How should you start a function?**
1. With; def (for definition) 2. And then open paranteheses right after the function name 3. And list the arguments comma separated 4. End the parantehese AND add a colon: def print\_three(arg1, arg2, arg3):
138
**How should your function name be?**
It should only use characters AND underscore so for example Print\_two\_again
139
**How many spaces should the lines of code in a function be indented?**
4 characters. No more, no less. This usually happens automatically in your script-writing program
140
**How do you end a function? (stop adding more to it)**
141
**What does it mean to run a function?**
The same as to “use” or to “call” a function
142
**What does it mean to call a function?**
The same as to “run” or to “call” a function
143
**What does it mean to call a function?**
The same as to “use” or to “use” a function
144
**What’s allowed for a function name?**
Just like variable names, anything that doesn’t start with a number and is letters, numbers, and underscores will work
145
**What does the \* in \*args do?**
That tells Python to take all the arguments to the function and then put them in args as a list. It’s like argv that you’ve been using, but for functions. It’s not normally used too often unless specifi- cally needed.
146
**Are functions and variables connected?**
No. The functions you write in your script are not connected to the variables.
147
it will print: We can just give the function numbers directly: You have 20 cheeses! You have 30 boxes of crackers! Man that's enough for a party! Get a blanket. As it will first “We can just give the function numbers directly:” and then all the content of the function (def) Cheese\_and\_crackers below it.
148
**How should you write up an user-input-variable making sure that it will be an integer result?**
Put raw\_input inside int() for integer For example Eaten\_packages = int(raw\_input(“How many packages did you eat by now? “))
149
**Is there a limit to the number of arguments a function can have?**
It depends on the version of Python and the computer you’re on, but it is fairly large. The practical limit, though, is about five arguments before the function becomes annoying to use.
150
**What does f.seek do?**
The seek-function takes the “cursor” (or point of the mouse so to say) to a certain place in a file. For example, we take it to the very beginning with f.seek(0) so we are at the start of the file again
151
**What does += do?**
`+=` adds another value with the variable's value and assigns the new value to the variable. `+=` adds a number to a variable, changing the variable itself in the process (whereas `+` would not). It adds the right operand to the left. `x += 2` means `x = x + 2`
152
**What does -= do?**
`+=` subctracts a number from a variable, changing the variable itself in the process (whereas `-` would not). It subtracts the right operand from the left. `x -= 2` means `x = x - 2`
153
**What does \*= do?**
`*=` multiplies a number with a variable, changing the variable itself in the process (whereas `*` would not). It multiplies the right operand with the left. `x *= 2` means `x = 2x`
154
**What does /= do?**
`/=` divides a variable with a number, changing the variable itself in the process (whereas `/` would not). It divides the right operand with the left. `x /= 2` means `x = x/2`
155
**What does %= do?**
`%=` finds modulus of a variable with a number, changing the variable itself in the process (whereas `%` would not). Modulus = the remainder when dividing. It finds modulus so: `x %= 3` means `x = modulus of x/3`
156
**How does readline() know where each line is?**
Inside readline() is code that scans each byte of the file until it finds a \n character, then stops reading the file to return what it found so far. The file f is responsible for maintaining the current position in the file after each readline() call, so that it will keep reading each line.
157
**What does the return statement do/how does it work?**
The RETURN statement writes out what it does to the user AND can also be used by other functions The PRINT statement simply writes out what’s inside but cannot be used by other functions _Example explanation:_ Def returnFunction(num): Num = num \*2 Return num Def printFunction(num): Num = num \*2 Print(num) If we write… returnFunction(4) we get an output of; 8 if we write…. printFunction(4) we get an output of; 8 So as such they look the same. But.. heres the difference. X = returnFunction(4) X #notice this one 8 Y = printFunction(4) 8 So the return function gives us “x” but the print-function doesn’t give us anything so we cannot use it later. If we in python now ask for x we will get 8 but if we ask for y we won’t get anything.
158
**How can I use raw\_input() to enter my own values?**
Remember int(raw\_input())? The problem with that is then you can’t enter floating point, so also try using float(raw\_input()) instead.
159
**What does “return” mean?**
It’s the same as “output”. Synonyms.
160
**How do you write comments in python?**
Write # and everything here after the hashtag will not be shown. It is called HASH or and OCTOTHORPE But you can still write an octothorpe inside strings like: Print “here we # like to go on Instagram”
161
**How do you print some text that is followed by maths?**
Remember to put a comma… Print “Hens”, 25 + 5 Will return Hens 30 Or Print “What is 3 + 2?”, 3 +2 Will return What is 3 + 2? 5
162
**How do we create a variable in python?**
Simple use = For example Cars = 100
163
**What does %r do?**
If you use %r, it will print no matter what, meaning that it will both print a character-variable (normally %s) and a number-variable (normally %d). print "and with both character %r AND number %r" % (height, hair) # with %r we will end up printing both the character-variable AND the number-variable and with both character 192 AND number 'Dark-blond'
164
**How can we repeat a printed string?**
Just write asterisk and the number of times you want to repeat it. For example: Print “.” \* 20 Will return ………………..
165
**How do you write a list of things but on different lines?**
Use \n For example: Months = 'Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug' Print “Here are the months: “, months
166
**How can you tab in some printed text?**
Use \t So for example Print “”” I’ll do a list: \t\* Sugar \t\* Spice \t\* And everything nice Will return I’ll do a list * Sugar * Spice * And everything nice
167
**How can you ask users to input information/answer questions?**
Use raw\_input() For example… Print “How old are you?”, Age = raw\_input() remember the comma after print
168
**What do you need to do before you can use arguments?**
Import argv by writing… From sys import argv
169
**Can you write strings inside raw\_input()?**
Yes. This is called a prompt. Help for the answer For example Print “What’s your birthday? “, Raw\_input(“DD-MM-YYYY” )
170
**How do you define what your arguments are?**
Write first the argument inoputs such as… Script, filename = argv #script name and filename is defined here.
171
**How can we create AND open a file at the same time?**
Open(filename, “w”) #note… you must define filename.
172
**How do you delete the contents of a file?**
Target.truncate()
173
**How do you write something into a file through your script?**
Target.write()
174
**How do you close a file?**
Target.close()
175
**How do you create a line break?**
\n
176
**How we get the number of bytes a file is long?**
Len()
177
**How do we open and read a file in the same line of code?**
open(from\_file, “r”) #note you must define from\_file
178
**How do you tell python that there is a function inside a certain file?**
First you open command and run “python” Then you “import file1” (whatever file it is) And then tell python to run a certain function within “file1” by adding “dot” and the function’s name. For example: E25.print\_last\_word(sorted\_words)
179
**How do you incorporate help statements in your python functions and later display them in command?**
180
**break\_words**(stuff)
This function will break up word for us.
181
**print\_first\_and\_last**(sentence)
Prints the first and last words of the sentence.
182
print\_first\_and\_last\_sorted(sentence)
Sorts the words then prints the first and last one.
183
**print\_first\_word**(words)
Prints the first word after popping it off.
184
**print\_last\_word**(words)
Prints the last word after popping it off.
185
**sort\_sentence**(sentence)
Takes in a full sentence and returns the sorted words.
186
**sort\_words**(words)
Sorts the words.
187
**What is a list?**
lists - consist of a countable number of ordered values - fruits = [’orange ’ , ’apple ’ , ’pear ’ , ’banana ’ , ’kiwi’, ’apple’, ’banana’]
188
**What is a tuple?**
* Immutable: Cannot be changed * The advantage is that you will know the position of the elements better and thus also make it easier for the computer to locate elements: provides for speed * You use it when you know that a variable will not change (e.g. days of the week) * Values are separated by commas * v = (’a’, ’b’, ’c’)
189
What is a set?
sets * unordered collection * NO duplicate elements * basket = {’apple’, ’orange’, ’apple’, ’pear’, ’orange’, ’banana’}
190
What is a dictionary?
dictionary * a set of keys: value pairs in which each key is unique * tel = {’jack’: 4098, ’john’: 4139}
191
What are classes and objects/instances?
classes are used to create new user-defined data structures that contain arbitrary information about something. Class is generic. * Class: Dog * species = ’mammal ’ an object/instance is a copy of the class with actual values (thus you have 1 class and multiple instances). Object/instance is specific. * Instance/object: * def \_\_init\_\_(self , name, age): self .name = name * self .age = age
192
Will these two turn out the same results?
193
What is the difference between for- and while loops?
* For-loops you know in advance the number of times it will execute. This is unknown with while-loops as it executes as long as the Boolean condition is True * Use a for-loop for definite iterations = if you know the maximum number of times that you need to execute the code * Use a while-loop if you require to repeat some computation until a condition is met that you cannot calculate in advance (needed for probabilistic results and human intervention)
194
what does a break statement do?
a break statement is used to immediately finish a loop. (this is considered bad practice though)
195
what does a continue statement do?
a continue statement causes to skip the processing of the rest of the code for a loop BUT the loop continues with the subsequent iterations of the loop (this is considered bad practice though)
196
What does “syntax error” refer to?
* Missing punctuation characters, such as parentheses, quotation marks or commas
197
What does “TypeError” refer to?
* when you try to combine to incompatible items
198
What does “NameError” refer to?
* Normally when you have used a variable before assigning it a value
199
What does “ValueError” refer to?
* When the value passed to a function is not compatible with the function. * in the example as string inserted as if it was to be converted to an integer
200
What is the standard format to ask for help in programming communities?
TIRTBV * Title * Introduce * Reproduce (others should be able to reproduce) * Tags * Background * Version
201
How can you take the sqrt of numbers in python?
202
What is the difference between “a = 2” and “a == 2”?
A = 2 is defining a as 2 A == 2 is asking whether a is equal to 2 or not
203
What are 3 good rules for naming variables?
* Names can not start with a number * Use _ when using space * Do not use capital letter (best practice is lowercase)
204
What are the 3 types of variables?
* Float * Integer * Boolean
205
How do you make addition, subtraction, multiplication, diversion, power functions and modulo?
* Addition (i.e. +) * Substraction (i.e. -) * Multiplication (i.e. \*) * Division (i.e. /) * Power functions (i.e. \*\*) * Modulo (i.e. %)
206
What is a possible reason why (0.1+0.2-0.3) equals 5.551115123125783e-17 instead of 0?
The numbers have not been rounded and are only showing with one decimal. To turn to 0 write: round(0.1+0.2-0.3)
207
How do you get the number of characters in a string?
Count number of characters in "Hello world" my\_text = 'Hello world' len(my\_text) 11
208
What will…. my\_text = 'Hello world' print(my\_text[7]) return?
O H = 0, e = 1, 2 = l, 3=l, o=4, “ “ = 5, w=6, o=7
209
If, my\_text = ‘Hello world’, how can you write it out as both capital letters, small-case letters and as two different strings?
210
How can you reverse the order of a list?
211
**If…** **a = {'a','b','c','d'}** **b = {'b','c','e'}** **find all the elements in a and b result={'a', 'b', 'c', 'd', 'e'}**
a | b
212
If.. my\_list = [1,2,3,4,2,2,2,2,5,5,5,5,9,9,9] how do we find the number of unique elements and how do we create set of these unique numbers?
len(set(my\_list)) 6 set(my\_list) {1, 2, 3, 4, 5, 9}
213
If… a = {1, 2, 3} b = {2, 3, 4} how do you find the symmetric difference and what does it mean?
print(a ^ b) #symmetric difference is everything that is not in the intersection, everything that’s not in common {1, 4}
214
What will… for i in range(1, 10): if i%2 == 0: print(i) return?
2 4 6 8 (A is the initial value, B is how far it should go and C is the steps) (2, 10, 2) will turn out as 2, 4, 6, 8 (but not 10) You can do this much more efficiently… for i in range(2, 10, 2): print(i) #this version is much more efficient as it will not spent time on #1, 3, 5, 7, 9 so we save CPU power
215
How can you print all multiplication tables from 1 to 10? (nested for-loops)
for x in range(1, 11, 1): for y in range(1, 11, 1): print((x\*y), end=" ") print()
216
List comprehension:…. number = [1, 2, 3, 4], return their square value when \> 8
list\_Numbers = [1, 2, 3, 4] l = [i\*\*2 for i in list\_Numbers if i\*\*2 \> 8] l with list comprehension you create a NEW list so you have [] around it and then here I have defined the new list containing 9, 16 as "l" you can add the new list to an existing list with .append list\_Numbers.append(l) list\_Numbers [1, 2, 3, 4, [9, 16]]
217
What do while-loops do?
Now let’s talk about WHILE-LOOPS, which executes an unknown number of times, as long as the Boolean condition is True
218
219
Write a loop that prints string characters one by one
220
How can you…x^2: x in {0,1,2,...10}?
221
Implement a x^4 in two nested comprehesion (x^2... then x^2)
222
Create a function that prints a hello and a name of the person
223
Write a program that outputs the first recurring character in a string. myString = "ABCDEBC"
224
What is the standard way to define a function in python (5 elements)?
225
How can you adequately open a python file and read it?
with open(‘data.txt’, ‘r’) as f for line in f: print(line) this will read the file line by line #you can also do it this way…. with open(‘data.txt’, ‘r’) as f: data = f.read() however, this reads everything in one line so if there are no line shifts in the file you will get one looooooong line
226
How can you cope with consecutive errors in your program?
With try and except try: run my freaking code here and except: if it does not work then try to do this thing to fix it
227
How you close a file?
write the specified name.close()
228
How do you search for files that match a particular pattern, such as ‘.csv’?
You can use… endswith() startswith() fnmatch.fnmatch()
229
Retrieve all files and directories starting with ‘Py’
230
What is the “finally” clause used for?
231
What is NumPy?
**NumPy** is a Python module. It stands for ’Numerical Python’. It is a library consisting of multidimensional array objects and a collection of functions to process them.
232
What is an array?
* An array is a data structure consisting of a collection of values, each one identified by at least one positive index (can have more than one dimension; matrixes) * all values have the same type (int, float….) * (array = objects, ndarray = class name of these objects) * each element within this is a “dtype” (data-type) element in NumPy
233
What are 5 great rules for writing clean code?
* clear variable and function names * consistency in styling; ‘ vs “ * Duplication vs. abstraction * Group like items in your code so that it is more reusable * Break long program into different files
234
What's the purpose of R and Python respectively?
* R * Focuses on better, user friendly data analysis, statistics and graphical models * Academics, researchers, data scientists * Python * Object based and emphasizes productivity and code reliability * Programmers, data scientists
235
How do you convert a list into ndarray?
x = [1, 2, 3] a = np.asarray(x) [1, 2, 3] or… a = np.asarray(x, dtype = float) [1. , 2. , 3.]
236
What are the parameters in array slicing? [start:stop:step]
237
How do you slice ndarrays with more than one dimension?
238
How do you multiply two arrays element wise in NumPy?
np.multiply(a,b). Multiply two arrays element wise
239
How do you perform a matrix multiplication in NumPy?
np.matmul(a,b). Performs a matrix multiplication
240
How do you retrieve the largest element in NumPy?
**np.amax(a).** Retrieve the largest element
241
What is a series in Panda?
A series is a one-dimensional labeled array and can be created using the following code
242
What is the difference between syntax errors and exceptions?
* **Syntax errors** occur when the parser detects an incorrect statement * **An exception** error occurs whenever syntactically correct python code results in an error
243
How can you insert an exception?
244
What are assertions and how can they be used?
Assertions are a systemic way to check that the inputs in a program are as expected by the program to avoid running into exception errors. Often useful for: * checking parameter types, classes or values, * checking “can’t happen” situations such as dividing by 0 * after calling a function to make sure that its return is reasonable assert condition, error\_message
245
What is the “finally” clause for in python?
To implement some sort of action to clean up after executing a code.
246
What is a DataFrame in Panda?
247
How do you remove a column in Panda?
df = df.drop(columns = [‘ColumnName1’, ‘ColumnName2’])
248
What does “applymap()” do in pandas?
The function applymap() applies a function to each element of a DataFrame (two-dimensional data structure) apply() # by itself will only add to the specific column.. and apply(\_\_\_\_\_\_, axis = 1) # will add to a row
249
What is “nan” in pandas?
nan = Not a Number and is the result you get when applying functions to a DataFrame that has null values. To detect missing values in your dataframe you can use the isnull() and notnull() functions.
250
How do the different merge operations in Pandas look visually? * Natural join * full outer join * left outer join * right outer join
251
What will…. for i in range(2, 10, 2): print(i) result in?
252
you can also rename the function at the same time… from fibo import fib\_print as fibonacci
253
How can you Retrieve all files and directories starting with ‘Py’?
254
How can you Retrieve all .py files?
255
How could study the efficiency of your code? Use the “time()” function in python to check the time it takes for your code to run.
256
How does a NumPy array of (3.7) look AND how can you make it? NumPy takes rows FIRST and SECONDLY columns.
257
What will happen if you try to create an array from the following python list… x = [(1, 2, 3), (4, 5)]?
it basically fails as the second list only contains two elements.
258
Slicing multidimensional array (NumPy). How would you get [5, 6] from the following array?
a[-1][-2:] returns… array ([5, 6]) why? you basically have…
259
How can you define an array with values from 0 to 100 and filter out odd numbers?
y = np.arange(101) y[y%2==0] array([0, 2, 4, 6, 8, 10, 12 ………. 100)] note that arange only has one r.
260
What will np.arange(3)+5 return?
this is broadcasting… we have an array with 3 elements starting from 0 and we add 5. array([5, 6, 7])
261
What will np.ones((3, 3)) + np.arange(3) return?
this is broadcasting… Here we first create np.ones((3,3)) which is 1, 1, 1 1, 1, 1 1, 1, 1 and then np.arange(3) on top of it so it is increasing by adding 0, then 1, then 2.
262
What will np.arange(3).reshape((3, 1))+np.arange(3) return?
this is broadcasting… np.arange(3) is.. 0, 1, 2 BUT…. it is reshaped to 3 rows, 1 column… so… 0 1 2 and then we add another np.arange(3) to it… 0, 1, 2 and combine to… 0, 1, 2 1, 2, 3 2, 3, 4
263
How can you edit an array and raise all numbers to the power of 2?
say… a = np.array([1, 2, 3, 4]) for x in np.nditer(a): print (x\*x) 1, 4, 9, 16 note that you use “nditer”
264
What are three classic functions used to manipulate ndarray elements?
* # transpose; permutes the dimensions of an array * # concatenate; joins a sequence of arrays along an existing axis * # append; appends the values to the end of an array
265
What will happen if you use np.transpose(a) on “a = np.array([[1, 2], [3, 3]])
it starts as.. array([[1, 2], [3, 4]]) and transpose will change the dimensions of the array (flip rows and columns) so after np.transpose(a) we have… array([[1, 3], [2, 4]])
266
How do you join two different arrays by rows and by columns respectively? say you have.. a = np.array([[2, 2], [3, 3]]) b = np.array([[4, 4], [6, 6]])
by rows you would…. np.append(a, b, axis = 0) #axis 0 for rows result… array([[2, 2], [3, 3], [4, 4], [6, 6]]) and for columns… np.append(a, b, axis = 1) #axis 1 for columns. result…. array([[2, 2, 4, 4], [3, 3, 6, 6]])
267
How can you create an array of ALL zeros?
a = np.zeros((3, 3)) print(a) [[0, 0, 0] [0, 0, 0] [0, 0, 0]]
268
How can you create an array that has “42” in each element?
x = np.full((3, 3), 42) print(x) [[42, 42, 42] [42, 42, 42] [42, 42, 42]]
269
How can you create an array with random values?
e = np.random.random((2, 2))
270
if we have x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), what will… x[-3: 3: -1] return?
first we find -3, which is number 7 then we go from -3 until 3 which happens to be number 3 as it is in the 4th location in the row (0, 1, 2, 3) and lastly we return the values in OPPOSITE order because of -1 array([7, 6, 5, 4])
271
What is the key difference to remember between NumPy and Pandas?
* NumPy = First, ROW, Second, COLUMN * Pandas = First, COLUMN, Second ROW
272
How can you select multiple rows in Pandas?
Multiple rows can be selected using ’:’ operator as in numpy arrays df[1:3] just remember that in Pandas, you pick COLUMN FIRST, and row second
273
How can you get information about a file in pandas?
if the file name is titanic.csv titanic.info() will return
274
How can you get descriptive statistics?
if the file name is titanic.csv titanic.describe()
275
What are the components in making a histogram?
* titanic.hist( * bins = # the width of bars (lower is wider) * figsize=(16, 9) #is the proportions of the figures * grid=False #whether you want a grid or not * plt.show() #and then show the histogram
276
How do you make a histogram of the data within a specific column?
titanic. Age.hist(bins = 50, figsize=(12, 6), grid=True) plt. xlabel(‘Age’) plt. ylabel(‘Number of people’) plt. show()
277
Is NaN == NaN?
No… NaN == NaN is False NaN is special in that it doesn't have a real value, so comparing it to itself doesn't return true. Essentially, NaN is equal to nothing, not even NaN Because of this, the only safe way to tell whether or not a value is missing in a DataFrame is by using the isnull() function. Indeed, this function can be used to filter rows with missing values; edu[edu["Value"].isnull()]
278
How can be said about columns and rows in pandas?
* COLUMNS * are Series, which consists of several values, where each value has an index * ROWS * have a specific index to access them and be any name or value
279
If we just define a series as, c = pd.Series([1956, 1967, 1989, 2000]), what will the index be?
when not specifying the index, they will simply be a sequence of integers starting from 0. 0, 1, 2, 3 in this case
280
How can we specify the index for the following series, c = pd.Series([1956, 1967, 1989, 2000])?
281
What are the ‘head’ and ‘tail’ methos for?
Helps show the first (by default = 5) rows in a dataset so we can see how the data looks. If you insert a number in head(), you will get that number of rows instead of the default 5 rows. The tail() method is the opposite in the way that it gives you the LAST 5 rows.
282
How can you get the names of the columns and rows in a DataFrame?
filename.columns and filename.index
283
What is the difference between edu[10:14] and edu.loc[10:14]?
edu[10:14] will provide the rows from 10th to 13th position BUT this does not take your index labels into account. Only the position. edu.loc[10:14] will return the values of all the rows between the row labeled 10 and the row labede 14. This could be 10,11,12,13,14 but it could just as well be only one value OR it could be many values. It could also be a row which is indexed 100 if that row has been placed between the rows labeled 10:14.
284
what is the difference between writing edu.max(axis=0) and edu.max(axis=1)?
axis = 0 finds the maximum value for each column (finding max within the rows for each column) while axis = 1 find the maximum value for the row (finding max within the column for each row)
285
What is the difference between pandas and pythons interpretation of NaN?
In Python ,NaN values propagate through all operations without raising an exception. In contrast, Pandas operations exclude NaN values representing missing data. For example, the pandas max function excludes NaN values, thus they are interpreted as missing values, while the standard Python max function will take the mathematical interpretation of NaN and return it as the maximum:
286
What is a lambda function?
It is essentially just a function without a name. It is written in-line. For example…
287
What is the following doing? edu["ValueNorm"] = edu['Value']/edu['Value'].max() edu.tail()
we are defining a new column called ‘ValueNorm’ by taking ‘Value’/the highest number in the ‘Value’ column lastly we ask to see the 5 last rows of the updated DataFrame using tail()
288
How can you insert a new row at the bottom of a DataFrame?
using the append function, which receives as argument the new row represented as a dictionary where the keys are the name of the existing columns and the values the associated values. It is important to the the ignore\_index flag to True, otherwise the index 0 is given to this new row which will produce an error if it already exists (may potentially overwrite if not set to True)
289
How can you easily delete the last row in a Dataframe?
simply use the drop and max functions. Why max? because then we will be taking the last row if the index is ordered at least.
290
How can we fill in NaN values?
using fillna() eduFilled = edu.fillna(value={‘Value’:0}) will fill in the NaN with 0 instead but be careful about doing so will change the statistics of your data when using pandas.
291
What will the following give us? edu. sort\_values(by='Value', ascending= False,inplace=True) edu. head()
here we are using the sort function to sort the data by ‘Value’ and NOT in ascending order (then it must be descending order), meaning that we will get the 5 highest values as we use edu.head() which by default give 5 rows.
292
What will the following give us?
here we are using groupby to group some data. we are clearly interested in GEO (countries) and the values. We are grouping by Geo (country) and asking for the mean of values for each country. next, we ask pandas to sort this data by values in a descending order.
293
What is the following doing?
1. first we are filtering the data with a boolean expression asking for data dated later than 2005 from the TIME column 2. secondly, we use the pd.pivot\_table function to rearrange our data such that the index/rows become GEO/countries and columns will be equal to time (the time values newer than 2005 as we are using the filtered data) 3. lastly we ask to see the first 10 rows
294
Say we have x = 9, y = 3,.... then we write x + 10 and python returns 19 How do we get the previous result + y?
obviously we could just write 19 + y and get 22 we could also press the arrow key up in command and pick up x + 10 and add + y to get 22 the most correct and simplest way though is: \_ + y #underscore + y 22
295
#say name = 'Youtube' name #what will this give name[1:4]
‘out’
296
#say you have a list of numbers numbers = [33, 44, 55, 66, 77, 88] #how can you add 79 to the list?
297
#what are the two ways that we can remove 44 from the list? [33, 44, 55, 66, 77, 79, 88, 79]
298
#how can you add multiple values to an existing list?
299
How do you define a list, a set and how do you define a tuple?
* list =[] * most flexible and can be both numbers and strings * set = {} * like list BUT… will not maintain sequence and does not support duplicate values * tuple = () * tuples you cannot change the values the same way as lists. Thus, use only tuples for things that are fixed. * tuples are though useful because they are faster for python to run
300
What are the different data types in Python?
* None * Numeric * Int, float, complex, bool * List [] * Tuple () * set {} * string “” * range (2,10,2) * Dictionary
301
If x = 2, what will be the result of x \*= 3?
2 \* 3 = 6
302
If x = 2, what will be the result of x \*\*= 3?
2^3 = 8
303
Why is bin(25) = ‘11001’ for a computer?
computers think binary.. 11001 1\*(2^4), 1\*(2^3), 0\*(2^2), 0\*(2^1), 1\*(2^0) = 25
304
#How do you swap the values of the #two variables below using formula? a = 5 b = 6
305
#How do you swap the values of the two variables below in the easiest way? c = 10 d = 15
306
What are python’s bitwise operators?
* complement (∼) (‘tilde’ symbol) * and (&) * Or (І) * XOR (^) * Left shift (\<\<) * right shift (\>\>)
307
Why is 10 \<\< 2 returning 40?
because.,.., 10 is in binary…. 1010 meaning that it is essentially 1010.000000000 (infinite number of 0s after the decimal) now we shift 2 to the left so we get 101000.00000 and thus 101000 1\*32, 0\*16, 1\*8, 0\*4, 0\*2, 0\*1 = 40
308
why is 100 \>\> 3 returning 12?
because… 100 is in binary 1100100 meaning that it is essentially 1100100.000000(all the 0 in the world if needed) now we shift 3 to the right so it is now.. 1100.100 and thus 1\*8, 1\*4, 0\*2, 0\*1 = 12
309
what are math.floor() and math.ceil() for?
using math.floor and math.ceil (after importing math, import math to python) is essentially round numbers to the nearest integer floor or ceiling. for 2.7, the floor is 2 and ceiling is 3 for 100.14 the floor is 100 and ceiling is 101
310
how can you print ejendomsinvestoren 5 times?
311
How can you print each even number from 0 to 100?
312
how can you print all numbers from 1 to 100 but excluding those that are divisible by 3 and 5?
313
What is the difference between ‘continue’ and ‘break’?
if i in range(5): if i == 3: break print(“Hello “, i) with break it will stop printing so we only get; Hello 0, Hello 1, Hello 2 if i in range(5): if i == 3: continue print(“Hello “, i) with break it will CONTINUE printing so we only get; Hello 0, Hello 1, Hello 2, Hello 4,
314
How would you create the following: #### #### #### #### ?
315
How would you create the following: # ## ### #### ?
316
How would you create the following: #### ### ## # ?
317
What does it mean to use FOR ELSE?
To make a for-loop in which you have an if-elif-else statement WHERE the “else” IS NOT indented.
318
#say we have a list of numbers nums = [12, 15, 18, 23, 26] #how can we return the first number divisible by 5? #note; ONLY one number. and print "not found" if none
319
What is TypeCode used for?
You use TypeCode to specify which type the values are of for an ARRAY import array array.array(‘b’, [5, 7, 8, 10])
320
#how can you ask a user to enter values to an array?
321
Why do we need numPy?
For multi-dimensional arrays
322
What are the six ways to create arrays with NumPy?
1. array() 1. arr = array([1, 2, 3, 4, 5]) 2. linspace() 1. arr = linspace(0, 16, 32) 1. start, stop and step/parts (BUT.. step is splitting up into parts here) 1. from 0 to 16 and split into 32 numbers. 2. if not specifying the number of parts it is 50 by default 3. logspace() 1. arr = logspace(1, 40, 5) 1. start, stop and parts/step 1. split up to 5 numbers between 1 and 40 according to log 4. arrange() 1. arr = arange(1, 15, 2) 1. will return… 1, 3, 5, 7, 9, 11, 13 2. start, stop and STEP 5. zeros() 1. arr = zeros() 1. array of ALL 0s 2. fx arr = zeros(5) will return [0, 0, 0, 0, 0] 6. ones() 1. arr = ones() 1. array of ALL 1s
323
copying an array in python SAME ID
324
#how can you add two arrays?
325
#how can you make two arrays into one long array?
326
copying an array in python DIFFERENT ID
327
How can you check how many dimensions an array contain?
print(arr1.ndim)
328
How can you check the shape of an array?
print(arr.nshape)
329
How can you check the size of an array?
print(arr.size)
330
multi-dimensional arrays... #how can you convert a 2d-array into 1d-array?
331
multi-dimensional arrays... #how can you convert a 2d-array into 3d-array?
332
define a simple function and call it “greet”
333
define a function that can add two numbers
334
Create a function that can sum an unknown amount of numbers
335
What is the difference between a local and global variable?
336
#How can you pass a LIST to a function in Python and count #odd and #even?
337
#How can you create a Fibonacci Sequence?
338
#How can you make a function that find the Factorial of a given number?
339
What does recursion mean?
340
#How can we make factorial using recursion?
341
What is a lambda function?
essentially an anonymous function = a function with no name
342
filter map reduce how can you sort this list for even numbers using lambda? nums = [3, 2, 6, 4, 3, 9, 11]
343
what is \_\_name\_\_ and why is it useful?
#it is a special variable in python used to avoid running an entire #module when trying to call a function from an external library/module #that you have imported
344
what is the better word to use for “function”?
METHOD
345
How do “class” and “methods” work together?
Class are overall.. class Computer: and then the methods/functions are part of that class and must be called with… computer.config(com1) just as we write numpy.something but.. you can actually also write… com1.config() which is more likely what you will see (calling the method FROM the object)
346
#What is object oriented programming?
thinking in objects just like everything we need to do in real life we need "objects" for such as a laptop, a camera, a frying pan etc. in programming we need to build objects also and put them together what matters is DESIGNING objects, not manufacturing them as that can be done anywhere, and in the computer world they can be replicated billions of times quickly.
347
What are the two types of variables?
348
What is inheritance in python?
Class A: Class B(A): Class C(B): but we can also say that C is inheriting from A and B (mom and dad) who are unrelated by… Class A: Class B: Class C(A, B)
349
What does "polymorphism" mean and how can it be implemented?
polymorphism = poly(many) morphism(forms) = objects can take many forms. It can behave in multiple ways. duck typing operator overloading method overloading method overriding
350
What is # method overloading?
method overloading: if you have two methods with the same name, def average(a,b) and def average(a, b, c) it is method overloading one takes two parameters and one takes three
351
What is # method overriding?
two methods with the same name AND the same number of parameters also, if class B(A): inherits what is in A, but B has something itself, it will override the information from A. Say class A: contains the car of your dad Say class B(A): contains nothing, then your father's car is yours(inheritance) but if you have your own car within B, it will be that one.
352
What is a logical error?
when your code returns a value but the value is wrong. if 3+2 returns 4, you have made a logical error
353
What is a runtime error?
when the code does not work during part of the run time… this is for example if people put in a string and you only allow for integers, float when only integers allowed, if a negative value or if divided by 0 and so forth.
354
How do you handle exceptions?
use the try: except: #can use multiple excepts except: except: finally: setup….
355
Why is multi-threading important?
356
Is python a compiled or interpreted language?
It is actually both… * Compiled language * the computer does not understand the language so we compile the code we write for the computer to understand in binary form (0101010101010110110) * Interpreted language * also interpreted as… * source code =\> compiled =\> byte code =\> interpreted by python virtual machine =\> machine learning * it is taking things line by line
357
How do you swap the values; a = 8 and b = 10?
a = 8 b = 10 then… a = c # now c is 8 b = a #now a is 10 c = b #now b is 8 a = 10 b = 8
358
Say you have the list… areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50] find the # Sum of kitchen and bedroom area and call it: eat\_sleep\_area
359
If.. areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]..... update the area of the bathroom area to be 10.50 square meters instead of 9.50. AND Make the areas list more trendy! Change "living room" to "chill zone"
360
If.. areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]..... now You decide to build a poolhouse and a garage. Add this to the list..
361
Say you have… areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0, "bedroom", 10.75, "bathroom", 10.50, "poolhouse", 24.5, "garage", 15.45]... how do we remove the poolhouse?
del(areas[-4:-2])
362
What is wrong here?
The problem we have is that area\_copy = areas is not explicit enough in copying the areas list. Thus, when we change the values of areas\_copy[] to 5 we are in fact also changing the value [0] in the original areas list. To avoid changing the original list when making changes in the copy, we must use either list(areas) or areas[:]
363
Say you have. first = [11.25, 18.0, 20.0] second = [10.75, 9.50] now merge the two lists and write them in descending order…
364
How do you check the components of an in-built python function?
you can always search the internet but otherwise use.. help(function\_name) so for example help(max), help(sorted) and so forth
365
What is important to remember about methods?
there are different methods available to different types of objects. also, sometimes the same method is available to different types of objects but behave differently,. #... METHODS call functions on objects fam.index()
366
Call the method “upper” on place = "poolhouse
place\_up = place.upper() print(place) print(place\_up) poolhouse POOLHOUSE
367
you have.. areas = [11.25, 18.0, 20.0, 10.75, 9.50], how can you add 24.5 and 15.45 to the list?
areas. append(24.5) areas. append(15.45)
368
What is two of the reasons the NumPy ARRAY is better than LIST?
* the ARRAY is able to perform calculations on the entire set of values * You can also use boolean operators to obtain results from arrays.
369
What are some of the disadvantages of ARRAY compared to LIST?
Array cannot contain different value types. If having different value types, type coercion will take place
370
In NumPy, # Print out the 50th row of np\_baseball
print(np\_baseball[49])
371
In NumPy, # Select the entire second column of np\_baseball and call it np\_weight\_lb
np\_weight\_lb = np\_baseball[:, 1]
372
In Numpy, Create numpy array np\_height\_in that is equal to first column of np\_baseball. * Print out the mean of np\_height\_in. * Print out the median of np\_height\_in.
NOTE that the format is…. np.builtinfunction(nameofarray) and… rememebr that NumPy has ROW first, column second so [:, 0]
373
In Numpy, # Print out correlation between first and second column. Replace 'None' corr = None print("Correlation: " + str(corr))
374
Say you have two np.rrays... np\_heights = np.array(heights) np\_positions = np.array(positions) Now define the heights of goalkeepers (GK), gk\_heights and the height of other players, other\_heights
Heights of the goalkeepers: gk\_heights gk\_heights = np\_heights[np\_positions == 'GK'] Heights of the other players: other\_heights other\_heights = np\_heights[np\_positions != 'GK']
375
Say you have two np.arays… Heights of the goalkeepers: gk\_heights gk\_heights = np\_heights[np\_positions == 'GK'] Heights of the other players: other\_heights other\_heights = np\_heights[np\_positions != 'GK'] How can we get the median for each of them and print it?
376
How should you import matplotlib.pyplot?
import matplotlib.pyplot as plt
377
How do you make a line plot and a scatter plot in matplotlib.pyplot?
plt. plot(x, y) = line plot plt. scatter(x, y) = scatter plot (no lines, just data points)
378
How do you plot something on a logarithmic scale? say you have plt.scatter(gdp\_cap, life\_exp)
plt. xscale('log') #changing the x-axis to logarithmic plt. show()
379
What are the most basic arguments in creating a histogram?
import matplotlib.pyplot as plt plt.hist(dataname, bins = 10) #it is 10 by default
380
How do you add axis names to your pyplots?
plt. xlabel(‘price’) plt. ylabel(‘quantity’)
381
How do you add a title to your pyplots?
plt.title(‘my title here’)
382
How do you change the increments on x and y axis?
for example.. plt.yticks([0, 2, 4, 6, 8, 10])
383
Say your x-axis is logarithmic and go from 1,000 to 100,000, how can we simplify the x-axis ticks?
tick\_val = [1000, 10000, 100000] tick\_lab = ['1k', '10k', '100k'] plt. xticks(tick\_val, tick\_lab) plt. show()
384
Explain this… plt.scatter(x = gdp\_cap, y = life\_exp, s = np.array(pop) \* 2, c = col, alpha = 0.8)
* We are making a scatter plot * with gdp\_cap on the x-axis * and life\_exp on the y-axis * size is set to np.array(pop) \* 2 #doubling the sizes of the bubbles * c = col =\> colors are set acording to what has been defined in col which in this case is different colors depending on continent of origin * alpha = 0.8 sets the opacity/transparency of the bubbles. 0 = 100 % opacity
385
How can we mark China and India on the plot?
We can add text with plt. text(1550, 71, ‘India’) plt. text(5700, 80, ‘China’) we obviously need to know these values first.
386
Dictionary… check which keys are in europe
print(europe.keys()) dict\_keys(['spain', 'france', 'norway', 'germany'])
387
Dictionary… called “europe”, print the value that belongs to the key, ‘norway’
print(europe['norway']) oslo
388
When would you use list vs. dictionary?
List * If you want to easily access values in an ordered matter (using indexes) Dictionary * When you want more freedom and have a unique table in which the unique keys can take different forms
389
dictionary… say we have.. europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' } Now add Italy to it. and check if it has been added...
europe['italy'] = 'rome’ now ask.. europe(‘italy’ in europe) True
390
Somebody thought it would be funny to mess with your accurately generated dictionary. An adapted version of the europe dictionary is available in the script on the right. europe = {'spain':'madrid', 'france':'paris', 'germany':'bonn', 'norway':'oslo', 'italy':'rome', 'poland':'warsaw', 'australia':'vienna' } Can you clean up?
391
Dictionaries: europe = { 'spain': { 'capital':'madrid', 'population':46.77 }, 'france': { 'capital':'paris', 'population':66.03 }, 'germany': { 'capital':'berlin', 'population':80.62 }, 'norway': { 'capital':'oslo', 'population':5.084 } } print out the capital of France:
print(europe['france']['capital'])
392
Dictionaries: europe = { 'spain': { 'capital':'madrid', 'population':46.77 }, 'france': { 'capital':'paris', 'population':66.03 }, 'germany': { 'capital':'berlin', 'population':80.62 }, 'norway': { 'capital':'oslo', 'population':5.084 } } print out the capital of France: * Create a dictionary, named data, with the keys 'capital' and 'population'. Set them to 'rome' and 59.83, respectively. * Add a new key-value pair to europe; the key is 'italy' and the value is data, the dictionary you just built.
393
Pandas: ‘Say you have a DataFrame ‘cars’, how can we change the index labels?
row\_labels = ['US', 'AUS', 'JPN', 'IN', 'RU', 'MOR', 'EG'] cars.index = row\_labels
394
Pandas, say you have a file, cars.csv, how can you import it as a DataFrame?
cars = pd.read.csv(‘cars.csv’) #remember ‘’ around the file name print(cars)
395
Pandas: What is the difference between the ouput of: cars[‘cars\_per\_cap’] and cars[[‘cars\_per\_cap’]]
The single bracket version gives a Pandas SERIES while the double bracket version gives a Pandas DataFrame.
396
Pandas.. What is the difference between loc and iloc?
loc = label-based meaning that you have to specify rows and columns based on their row and column labels. iloc = integer index based so you have to specify rows and columns by their integer index starting from 0.
397
Pandas.. What does this do….. cars.iloc[[3, 4], 0]
We ask for the rows indexed 3 and 4, and then the column indexed 0. we get it as a DataFrame because of [[]]
398
Pandas.. Print out the drives\_right value of the row corresponding to Morocco (its row label is MOR
399
Pandas..Print out a sub-DataFrame, containing the observations for Russia and Morocco and the columns country and drives\_right.
Print sub-DataFrame print(cars.loc[['RU', 'MOR'], ['country', 'drives\_right']])
400
Pandas.. from ‘cars’, # Print out drives\_right column as DataFrame
print(cars.loc[:,['drives\_right']]) can also be done with iloc if we know the index number of the drives\_right column
401
What will “alpha” \<= “beta” return and why?
True Because Python determines the relationship based on alphabetical order when working with strings.
402
How can you use AND, OR, NOT operators in NumPy?
Arrays work slightly differently so you need to use the AND, OR, NOT equivalent functions in NumPy, which are: * np.logical\_and() * np.logical\_or() * np.logical\_not() Example: np.logical\_and(my\_house \< 11, your\_house \< 15)
403
If we have… areas = [11.25, 18.0, 20.0, 10.75, 9.50] How can we print the values one by one AND the index values?
use for loop + enumerate
404
If we have… house = [["hallway", 11.25], ["kitchen", 18.0], ["living room", 20.0], ["bedroom", 10.75], ["bathroom", 9.50]] Write a for loop that goes through each sublist of house and prints out the x is y sqm, where x is the name of the room and y is the area of the room.
405
How can you iterate over key values in a dictionary?
Key values in dictionary * for key, val in my\_dict.items(): * #use .items
406
how can you iterate over all elements in a numpy array?
elements in an array * for val in np.nditer(my\_array): * #use np.nditer
407
how can you iterate over all elements in a pandas DataFrame?
for pandas Dataframes * for lab, row in brics.iterrows(): * #use .iterrows()
408
How can you iterate over key values in a dictionary and how can you iterate over all elements in a numpy array? and for Pandas DataFrames?
Key values in dictionary * for key, val in my\_dict.items(): * #use .items elements in an array * for val in np.nditer(my\_array): * #use np.nditer for pandas Dataframes * for lab, row in brics.iterrows(): * #use .iterrows()
409
If we have… europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo', 'italy':'rome', 'poland':'warsaw', 'austria':'vienna' } Write a for loop that goes through each key:value pair of europe. On each iteration, "the capital of x is y" should be printed out, where x is the key and y is the value of the pair.
410
We have… import pandas as pd cars = pd.read\_csv('cars.csv', index\_col = 0) and need to make the he first iteration print out "US: 809", the second iteration "AUS: 731", and so on. “country: cars\_per\_cap”
(already set as “lab” and “row”)
411
In a DataFrame, how can you create a new column by calling a function on another column?
use .apply very important feature. .apply
412
random; How can you roll a dice?
413
Why is it generally better to use “return” rather than “print” for functions?
Returning values is generally more desirable than printing them out because, as you saw earlier, a print() call assigned to a variable has type NoneType.
414
How can you return multiple values from a function?
Using tuples!!
415
Explain this code:
416
What are the different function scopes?
* GLOBAL scope * defined in the main body of a script * LOCAL scope * defined inside a function * BUILT-IN scope * names in the pre-defined built-ins module (such as ‘print’) * NOTE; you need tim ‘import builtins’ to use this. * writing ‘dir(builtins)’, you can see all builtin functions Python ALWAYS search for a value in LOCAL scope first, second GLOBAL scope and lastly BUILT-in scope.
417
How do you set a default parameter?
Here echo=1 =\> echo is set to 1 unless otherwise specified. Thus, in line 13, we do not specify echo but it will return 1 time, while in line 15, we ask it to return 5 times
418
How do you create a variable-length argument?
Use (\*args) #single \* It will turn a tuple of values. def\_sums(\*args):
419
How do you create a variable-length keyword argument?
Use (\*\*kwargs) #double \*\* It will turn a dictionary of values def report\_status(\*\*kwargs):
420
How would you write a lambda function add\_bangsthat adds three exclamation points '!!!' to the end of a string a? and How would you call add\_bangs with the argument 'hello'?
421
Convert this function to a lambda function
422
what is happening here?
423
If… fellowship = ['frodo', 'samwise', 'merry', 'pippin', 'aragorn', 'boromir', 'legolas', 'gimli', 'gandalf'] Using lambda, create a new list that contains only strings that have more than 6 characters…
424
What are the ways to write in error messages?
try: return: except: and… if ‘something’: raise \_\_\_\_\_Error(‘explanation’) try: return except: \_\_\_\_\_Error
425
How can you select and print the retweets ‘RT’ from a Twitter DataFrame;
426
What’s the difference between iterators and iterables?
Iterable * An object with an associated iter() method * Examples: lists, strings, dictionaries, file connections * Applying iter() to an iterable creates an iterator Iterator * Produces next value with next()
427
How can you iterate at ONCE?
use.. print(\*it)
428
What does the “enumerate()” function do?
It returns an enumerate object that produces a sequence of tuples, and each of the tuples is an index-value pair.
429
In this exercise, you are given a list of strings mutants and you will practice using enumerate() on it by printing out a list of tuples and unpacking the tuples using a forloop. Now, create a list and unpack the tupls using a for loop:
430
What does zip() do?
It takes any number of iterables and returns a zip object that is an iterator of tuples. If you want to print the values of a zip object, you need to convert it to a list and then print it.
431
Say we have mutants and powers, how do you create a zip object? AND then unpack it again?
432
Why may one want to load data in chunks and how is it done?’
Because there can be too much data to hold in memory. pandas function: read\_csv() * specify the chunk with… chunksize read\_csv(‘filename’, chunksize=xxxx)
433
How can you iterate over the file tweets.csv in small portions and adding new words to a dictionary and counting the number of times each word is in that dictionary?
434
Say we have… doctor = [‘house’, ‘cuddy’, ‘chase’, ‘thirteen’, ‘wilson’] How would a list comprehension that produces a list of the first character of each string in doctor look like?
435
How can you add a condition to a list comprehension?
436
What are generators?
uses () Great for very big numbers as it does not store everything in memory (as a list comprehension would do). While a list comprehension produces a list as output, a generator produces a generator object.
437
Create a generator object that will produce values from 0 to 30. Assign the result to result and print it using a for loop
438
What is the basic format of a list comprehension?
439
The lists2dict() function has already been preloaded, together with a couple of lists, feature\_names and row\_lists. feature\_names contains the header names of the World Bank dataset and row\_lists is a list of lists, where each sublist is a list of actual values of a row from the dataset.
440
Write a list comprehension to generate a list of values from pops\_list for the new column 'Total Urban Population'. The output expression should be the product of the first and second element in each tuple in pops\_list. Because the 2nd element is a percentage, you also need to either multiply the result by 0.01or divide it by 100. In addition, note that the column 'Total Urban Population' should only be able to take on integer values. To ensure this, make sure you cast the output expression to an integer with int().
441
How do you read a file? (best practice)
442
What will “! ls” do?
! ls will display the contents of your current directory.
443
How do you open a file using the “with”-method?
444
What is flat files?
* Text files containing records * Table data * Record: row of fields or attributes * Column: feature or attribute Flat files often have headers .csv, .txt (tab-delimited),
445
What are the main three ways to import data to NumPy?
* np.loadtxt(filename, delimiter=‘,’) * digits = np.loadtxt(file, delimiter=', skiprows = 1') * ‘,’ for comma-separated * ‘\t’ for tab-delimited * skiprows = 1 will skip the first row if we have headers for example * usecols =\> lets you specify which rows wish to keep * np.genfromtxt(‘filename’, delimiter = ‘,’, names=True, dtype=None) * To be used when you have different data-types (strings, float, etc). * names = True =\> means there is a header * np.recfromcsv() * Behaves similarly to np.genfromtxt BUT has default dtype=None, default ‘,’ as delimiter and default names=True.
446
What is pandas providing to data scientists that Numpy does not?
* Two-dimensional labeled data structures * Columns of potentially different types * Manipulate, slice, reshape, groupby, join, merge * Perform statistics * Work with time series data * Pandas is for data analysis and modeling.
447
From the file, digits.csv, import the first 5 rows to a DataFrame, build a numpy array from this DataFrame and print the datatype
448
How is delimiter defined in pandas import?
pd.read\_csv(filename, **_sep = ‘\t’_**, comment = ‘#’, na\_values = ‘Nothing’)
449
What are pickled files?
* A file type native to python * Not readable by humans but readable by python/machines * JSON files are an option if you want it readable by humans * Motivation; many datatypes for which it isn’t obvious how to store (such as lists, dictionaries etc.) * ‘rb’ means read only binary
450
How do you import Excel files to pandas?
figureo ut what the sheets in excel are called by print(data.sheet\_names) here as the file has been set to ‘data’ and to make dataframes of those sheets, use .parse and the sheet name OR sheet index
451
Import the file, battledeath.xlsx and display the sheet names
452
What is happening here?
453
What are SAS used for?
very important for data science used for: * advanced analytics * multivariate analysis * business intelligence * data management * predictive analytics * standard for computational analysis
454
How do you import a SAS file?
455
How do you import a Stata file?
456
What is a HDF5 file and how do you import it?
* Hierarchical Data format Version 5 * Standard for storing large quantities of numerical data. * hundreds of gigabytes or terabytes (can scale to exabytes)
457
what is a MATLAB file and how do you import it?
* Matrix Laboratory * Industry standard in engineering and science scipy. io.loadmat() = read .mat files scipy. io.savemat() = write .mat files
458
What is a relational database?
* Based on relational model of data * Rows are ordered and the columns are attributes for the information in the rows * Each row need a primary key column that has a unique entry for each row that we can call information from (such as OrderID, CustomerNumber, EmployeeNumber etc.)
459
How can you create a database engine with SQLAlchemy?
460
What does querying mean?
get data out of the database
461
How do you create an engine, connect to it, make a basic query, create a database and close it again?
1. Import create\_engine 2. import pandas as pd 3. Create the engine 4. Connect to the engine 5. Write a query (select \* =\> imports all columns) 6. Turn the query into a DataFrame using fetchall() 7. Close the connection
462
How do you specify columns that you want to import AND specify the number of rows you want to import in a query + set the DataFrame’s column names to the corresponding names of the table columns?
463
How do you make a query that selects all “EmployeeID”s from “Employee” that is greater than or equal to 6?
464
How can this be done much smarter with Pandas?
465
How do you import data from a url?
It is possible to skip the step of saving it locally (line 8-9) and just plug in the url directly in pd.read\_csv so: df = pd.read\_csv(url, sep=';')
466
What does URL stand for?
Uniform/Universal Resource Locator
467
How do you send a GET request to a certain URL and afterwards read the response? (http.client.HTTPResponse)
468
How do you make HTTP requests for an URL using the requests package?
469
What is the BeautifulSoup package for?
* Parse and extract structured data from HTML * “Make tag soup beautiful and extract information”
470
use the BeautifulSoup package to parse, prettify and extract information from HTML. The URL of interest is url = 'https://www.python.org/~guido/'.
471
What does API mean and what is it?
* Application Programming Interface * Protocols and routines that build and interact with software applications * allows two software programs to communicate with each other
472
What is JSON?
* JavaScript Object Notation * Real-time server-to-browser-communication * It is readable by humans * loads as a dictionary {}
473
Load the JSON 'a\_movie.json' into the variable json\_data, which will be a dictionary. Next, explore the JSON contents by printing the key-value pairs of json\_data to the shell.
474
How do you connect to an API?
475
Import and print the text from the URL, http://www.omdbapi.com/ with the these two argument in the query string; apikey=72bc447a and t=the+social+network
476
Which function do you use to decode a JSON to a dictionary?
the json() method so… url = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=pizza' Package the request, send the request and catch the response: r r = requests.get(url) Decode the JSON data into a dictionary: json\_data json\_data = r.json()
477
What are some of the most common data problems?
* Inconsistent column names * Missing data (NaN) * Outliers * Duplicate rows * Untidy * Need to process columns * Column types can signal unexpected data values
478
What are some of the most important functions in pandas to check out your data?
* df.columns() #to get column names * df.head() #first 5 rows by default * df.tail() #last 5 rows by default * df.shape() #shape of dataframe with (rows,columns) * df.info() #various info about the dataframe, non-missing values, data type etc. * df.describe() #getting basic statistics ONLY on numeric columns
479
How can you get the frequency counts for each unique value in a column?
whether the data is numeric or not, you can use .value\_counts(dropna=False) dropna=False is important to add to see missing values (NaN) note that it is value\_counts with s
480
What are great plotting tools to inspect your data and its outliers?
import matplotlib.pylot as plt * df['Existing Zoning Sqft'].plot(kind='hist', rot=70, logx=True, logy=True) #here setting both x and y axis as logarithmic * df.boxplot(column='initial\_cost', by='Borough', rot=90) # comparing inital\_cost column across different values of the Borough column * great for numeric values to be compared across different categories * df.plot(kind='scatter', x='initial\_cost', y='total\_est\_fee', rot=70) * great for visiualizing two numeric columns plt.show()
481
What are the 3 principles of tidy data?
* Columns represent separate variables * Rows represent individual observations * Observational units form tables Tidy data makes it easier to fix common data problems
482
What is this doing?
* We are melting columns except ‘month’ and ‘day’ * we call the value-column = reading * and the variable column = measurement * Melting turns columns into rows
483
What does pivoting do?
turn unique values into separate columns why: * analysis friendly shape to reporting friendly shape While melting takes a set of columns and turns it into a single column, pivoting will create a new column for each unique value in a specified column.
484
What is this doing?
Pivoting such that * we do not change the two columns ‘month’ and ‘day’ * we change the ‘measurement’ column and split the unique values in this column into new columns * and take the values from ‘reading’ and plug them into those rows
485
How would you split a column containing gender and age values in one such as m014, m65, f65, f4554 and so forth?
486
How would you split a column with values such as Cases\_Guinea, Deaths\_Guinea, Cases\_Liberia, Deaths\_Liberia?
\_ serves as a delimiter we can use .split()
487
How do you concatenate data?
#concatenating ROWS pd.concat([firstset, secondset, thirdset, etcset], axis=0) #Concatenating COLUMNS pd.concat([firstset, secondset, thirdset, etcset], axis=1) NOTE… Data MUST be in a list to concatenate DataFrames.
488
What is globbing about?
pattern matching for file names wildcards: \*? * any csv file: \*.csv * any single character: file\_?.csv
489
How do you merge data?
pd.merge(left=dd, right=dd\_d, on=None, left\_on=’something’, right\_on=’something) example: two dataframes; visited and site with columns that are the same called “site” and “name” o2o = pd.merge(left=site, right=visited, left\_on='name', right\_on='site') Many-to-many data merge ; The final merging scenario occurs when both DataFrames do not have unique keys for a merge. What happens here is that for each duplicated key, every pairwise combination will be created.
490
How can you see the data types in your DataFrame?
print(df.dtypes)
491
How do you convert an object column to categorical?
This will lower the memory need
492
How do you convert a column to numeric?
493
How can you make the computer interpret $17.00 with escape sequences?
494
How can you make the computer interpret $17.89 with escape sequences? #make sure it only takes two decimals for the monetary value
495
How do make a pattern matching for a us phone number in the format xxx-xxx-xxxx?
496
How can you make python extract the numbers found in a string? 'the recipe calls for 10 strawberries and 1 banana'
497
How could you write a pattern matching sequence to make '$123.45' turn True?
498
How can you remove duplicate data?
df.drop\_duplicates() Important to delete duplicate values as they take up a lot of memory and cause unneeded calculations to be performed when processing data
499
How can you fill missing values?
.fillna()
500
How do you make an assert statement?
501
What is this doing?
1. We have a dataset, ‘gapminder’ with 780 rows and 218 columns 2. each column represent a year with life expectancy data 3. but the very first column [0] has the country names 4. we then melt the data such together such that we get rows instead of columns = one row will have a year value, the life expectancy and the associated country 5. we make sure not to touch the country life by putting ‘life expectancy’ in as id\_vars 6. we then rename the columns to ‘country’, ‘year’ and ‘life\_expectancy’ 7. and print the head of it
502
How do you convert the column ‘year’ in the df ‘gapminder’ to numeric AND asset that the change has been made?
503
Say you have two lists, ‘list\_keys’ and ‘list\_values’, how do you create a dictionary and then a dataframe?
504
How can you in a structured way rename the columns of your dataframe?
505
Say we have a list of cities in Pennsylvania, how can we create a dataframe from this list AND add a column with the value ‘PA’ for Pennsylvania in all rows?
506
What are the 3 ways to create a histogram?
1. df.plot(kind=’hist) 2. iris.plt.hist() 3. iris.hist() NOTE they have different syntax
507
How can you get the 5th and 95th percentile of a dataframe?
print(df.quantile([0.05, 0.95])
508
How can you downsample hourly data of ‘Temperature’ in df to six hours and to daily?
509
Say we have a df with the column ‘Temperature’ and we want to extract all of august 2010 and show the maximum daily temperature?
510
How can you convert a column, say ‘wind\_speed’ in df to numeric values?
df[‘wind\_speed’] = pd.to\_numeric(df[‘wind\_speed’], errors=’coerce’)
511
Filter rows of august\_2011 to keep days where the value exceeded august\_max. Store the result in august\_2011\_high.
august\_2011\_high = august\_2011.loc[august\_2011 \> august\_max] loc with boolean condition both august\_2011 and august\_max are already encoded as variables
512
What is the difference between selecting df[‘eggs’] and df[[‘eggs’]]?
df[‘eggs’] returns a series (one-dimensional labeled data) df[[‘eggs’]] returns a DataFrame
513
How can you assign a index name and overall column name to a DataFrame,df?
df. index.name = ‘MONTHS’ df. columns.name = ‘PRODUCTS’ just as you set the index names by.. df.index = [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’]
514
Say we have the dataframe, ‘sales’ and want to make both the ‘state’ and the ‘month’ column indexes, what will we have to do?
Set the index to be the columns ['state', 'month']: sales sales = sales.set\_index(['state', 'month']) Sort the MultiIndex: sales sales = sales.sort\_index() Print the sales DataFrame print(sales)
515
Can you pivot a dataframe that has multiindex?
No… you will have to unstack the multi-index by turning an index to a column first. df.unstack(level=’indexcolumnname’) or df.unstack(level=1) #to remove the second index
516
How can you switch around indexes in a multi-index?
swapped = df.swaplevel(0, 1) swapped.sort\_index()
517
How can we call a zscore on a dataFrame? FIRST… groupby ‘region’ and columns, ‘life’ and ‘fertility’
standardized = gapminder\_2010.groupby('region')['life','fertility'].transform(zscore)
518
How does idxmax() and idxmin() work?
* idxmax() * Row or column label where maximum value is located * idxmax(axis=’columns) #for columns * idxmin() * Row or column label where minimum value is located * idxmin(axis=’columns) #for columns
519
what type of plot do you get from usa\_medals\_by\_year.plot.area()?
520
You may have noticed that the medals are ordered according to a lexicographic (dictionary) ordering: Bronze \< Gold \< Silver. However, you would prefer an ordering consistent with the Olympic rules: Bronze \< Silver \< Gold. How can we change the ordering?
You can achieve this using Categorical types. Redefine the 'Medal' column of the DataFrame medals as an ordered categorical. To do this, use pd.Categorical() medals.Medal = pd.Categorical(values=medals.Medal, categories=['Bronze', 'Silver', 'Gold'], ordered=True)
521
Say you have a list of csv files, filenames = ['Gold.csv', 'Silver.csv', 'Bronze.csv'], how can we import all these?
use a loop…. Create the list of three DataFrames: dataframes dataframes = [] for filename in filenames: dataframes.append(pd.read\_csv(filename))
522
eu\_metro\_areas.drop("High Population", axis=1, inplace=True) eu\_metro\_areas What is the purpose of inplace = True?
inplace = True is by default false. When True, we change the current dataframe when not set, and thus false by default, we create a new dataframe with a column missing. so… we get multiple dataframes, which is slower and often quite useless.
523
What will this do? z.replace({"Nowhere":"A city"}, inplace=True) z
we are changing the str ‘Nowhere’ to ‘A city’ and overwrite the current DataFrame as we set inplace=True.
524
How do you convert a DataFrame to CSV?
eu\_metro\_areas.to\_csv('My\_metro\_areas.csv', sep=',')
525
How do you convert a DataFrame to Excel?
eu\_metro\_areas.to\_excel("My\_metro\_areas.xlsx")
526
what is rug=True doing? sns.distplot(eu\_metro\_areas["GDP"],bins=7,rug=True)
it will show small bars for each observation/datapoint in the graph so we understand how big the dataset is
527
What join does pd.concat take as default?
outer
528
How do you add a column to an existing dataframe, df?
df[‘newcolumnname’] = [‘list’, ‘of’, ‘values’, ‘for’, ‘new’, ‘column’]
529
When should you use .append(), pd.concat(), df.join() and pd.merge()?
* .append() * stacking vertically * pd.concat() * stacking many horizontally or vertically * simple inner/outer joins on indexes * df.join() * inner, outer, left, right joins on indexes * pd.merge() * many joins on multiple columns
530
What join does pd.merge\_ordered take as default?
outer