Week 5: Data Visualization Flashcards
(29 cards)
Function calls
“calling” function = using the function
- takes in arguments
- outputs a return value
method = function
What to think about in function calls
- what are the input arguments?
- what does it do?
- what is the output?
round (34.34534, 2)
input: (34.34534, 2)
rounds 34.34534 to 2 decimal places
output: 34.47
abs (5-9)
input: -4
computes abs value
output: 4
len ([4, 5, 6])
input: [4, 5, 6]
calculates length
output: 3
print (‘hello’)
input: ‘hello’
prints input
output: None (doesn’t return)
‘abc’.replace (‘b’, ‘d’)
input: ‘b’, ‘d’
calculates what ‘abc’ will be like if we replace all the ‘b’s with ‘d’s
output: ‘adc’
def function_name (inputs)
blah blah…
return outputs
function_name: function header
inputs: parameter(s)
blah blah…: function body
outputs: returns
Ex:
def double (x)
return x*2
‘ABcD5’.lower()
input: None
calculates lower case version
output: abcd5
User-defined Function
You write a function yourself
- chunk of code you can reuse
- good practice in coding: modular and reusable
- everything in function is indented (so python knows it’s a function)
Parameters
input variables in functions
- arguments are what values you sent in for those variables
Arguments
parameters by either position or keyword
- the order of things in argument matter
- my_function(arg1, arg2, …)
- my_function(par1 = arg1, par2= arg 2) is same as my_function(par2 = arg2, par1= arg 1)
- Ex: round(12.45, 1) different from round(1, 12.45)
Optional parameter
Followed by its default value
- function definition starts with def my_function(par_default = arg_default)
- call my_function() using default value
- call my_function(par_default = arg) using specified value
Variable scope
portion of a program in which the name is valid
Consequences:
- parameters are local
- function call cannot prevede function header
- functions defined inside a function are local
Local
anything defined inside a function
Global
anything defined outside a function
Conditional statemet
Stuff that is dependent on other things
How to compare two things in python
a < b
- returns True if a<b (otherwise False)
a>b
- returns True/False
a==b
- returns True if a is equal to b
a!=b
- returns True if a not equal to b
a<=b
- returns True if a less than/equal to b
a>= b
- returns True if a greater than/equal to b
if name.lower() == ‘messi’:
print (‘I guess your parents like soccer!’)
If your name is Messi –> will print comment
Not Messi –> nothing
To call on function:
- name = ‘Messi’
- name.lower() –> messi
- then use in function
if len(surveyAnswer) == 0
print (‘No answer submitted’)
else:
print(surveyAnswer)
2 potential outcomes:
- print NA
- print the answer
printing whether number is positive, negative, zero
if x<0:
print (‘N’)
elif x>0:
print(‘P’)
else:
print(‘Zero’)
elif
else if
can add more if you want more outcomes
Conditionals
- always & only need to have 1 if statement
- can have as many elif as you want
- only 1 else statement (but don’t need it)
- code runs if condition that is met is indented
- can nest conditional statements
Graph Basics
- each plot needs title
- x & y axis label
- if multiple plots –> need a legend
- make plots look nice w/ clear message