Python Basics Flashcards

1
Q

2 types of functions?

A

built in functions and custom made

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

how to create function

A
define it.
def functionname(arguments).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

function

A

function is some stored code we use. we take some input and create output

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

implicent

A

when you multipy int and float in implicently becomes float.

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

converting datatypes

A

newint = int(oldval)

cant convert string to inv if not number

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

function

A
def print_lyrics():
    print ("I'm a lumberjack, and I'm okay.”)
    print ('I sleep all night and I work all day.’)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

function argument

A

An argument is a value we pass into the function as its input when we call the function
We use arguments so we can direct the function to do different kinds of work when we call it at different times
We put the arguments in parentheses after the name of the function

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

parameter

A

A parameter is a variable which we use in the function definition. It is a “handle” that allows the code in the function to access the arguments for a particular function invocation.
lang is the parameter.

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

return

A

Often a function will take its arguments, do some computation, and return a value to be used as the value of the function call in the calling expression. The return keyword is used for this.

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

return is

A

A “fruitful” function is one that produces a result (or return value)
The return statement ends the function execution and “sends back” the result of the function

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

parameter order and number

A

We can define more than one parameter in the function definition
We simply add more arguments when we call the function
We match the number and order of arguments and parameters

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

void

A

When a function does not return a value, we call it a “void” function
Functions that return values are “fruitful” functions
Void functions are “not fruitful”

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

what is a list?

A

A List is a kind of Collection.
A collection allows us to put many values in a single “variable”
A collection is nice because we can carry all many values around in one convenient package.

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

describe list

A
List constants are surrounded by square brackets and the elements in the list are separated by commas
A list element can be any Python object - even another list
A list can be empty
.
print ([1, 24, 76])
[1, 24, 76]
>>> print (['red', 'yellow', 'blue’])
['red', 'yellow', 'blue']
>>> print(['red', 24, 98.6])
['red', 24, 98.6]
>>> print([ 1, [5, 6], 7])
[1, [5, 6], 7]
>>> print([])
[]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

simple for loop examples

A

friends = [‘Joseph’, ‘Glenn’, ‘Sally’]
for friend in friends :
print(‘Happy New Year:’, friend)
print(‘Done!’)

z = [‘Joseph’, ‘Glenn’, ‘Sally’]
for x in z:
print(‘Happy New Year:’, x)
print(‘Done!’)

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

ping element

A

friends = [ ‘Joseph’, ‘Glenn’, ‘Sally’ ]
»> print(friends[1])
Glenn

17
Q

strings vs list

A
Strings are “immutable” - we cannot change the contents of a string - we must make a new string to make any change
Lists are “mutable” - we can change an element of a list using the index operator
fruit = 'Banana'
>>> fruit[0] = ‘b'
Traceback 
lotto[2] = 101
>>> print (lotto) 
[2, 14, 101, 41, 63]
18
Q

len() function

A
The len() function takes a list as a parameter and returns the number of elements in the list
Actually len() tells us the number of elements of any set or sequence (such as a string...)
>>> greet = 'Hello Bob'
>>> print (len(greet))
9
>>> x = [ 1, 2, 'joe', 99]
>>> print (len(x))
4
19
Q

range()

A

The range function returns a list of numbers that range from zero to one less than the parameter
We can construct an index loop using for and an integer iterator
»> print (range(4))
[0, 1, 2, 3]
»> friends = [‘Joseph’, ‘Glenn’, ‘Sally’]
»> print (len(friends))
3
»> print (range(len(friends)))
[0, 1, 2]
»>

20
Q

2 more loop examples using range/len and for

A

friends = [‘Joseph’, ‘Glenn’, ‘Sally’]

for friend in friends :
print (‘Happy New Year:’, friend)

for i in range(len(friends)) :
friend = friends[i]
print (‘Happy New Year:’, friend)

21
Q

add lists togeter

A
We can create a new list by adding two existing lists together
a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print (c)
[1, 2, 3, 4, 5, 6]
>>> print (a)
[1, 2, 3]
22
Q

more list functions

A
List.count(element)
List.insert (index, element)
List. append(new element) #appends an element to the end of the list
List.remove(element)
List.sort()
del list[index]
23
Q

show list functions

A

a = [66.25, 333, 333, 1, 1234.5] # create a list
print a.count(333), a.count(66.25), a.count(‘x’)

a.insert(2, -1) #List.insert(index, element)
a.append(333) # appends an element to the end of the list.
a

a.index(333) #show the first found element’s index
a.remove(333) # removes first found elements of the same value.
a

a.sort()# ascending order. If its list of strings, it will show in alphabetically order.
a

a.pop()#list.pop(pos) pos needs to be a specified position that you want to remove. If no input of pos, then remove the last element.
a

24
Q

list vs. dictionary

A

List
- A linear collection of values that stay in order

Dictionary
- A “bag” of values, each with its own label

25
Q

Dictionaries

A

Dictionaries are Python’s most powerful data collection
Dictionaries allow us to do fast database-like operations in Python
Dictionaries have different names in different languages
- Associative Arrays - Perl / PHP
- Properties or Map or HashMap - Java
- Property Bag - C# / .Net

26
Q

describe picture of dictionary statement

A

Construct dictionary
food={‘ham’: ‘yes’, ‘egg’: ‘yes’}
a = dict(one=1, two=2, three=3) #use dict() function
We can use any types as value but no lists or dictionaries can be used for keys.
Use [key] to index the entries
»>food[‘ham’]
‘yes’
Add new element:
»>food[’spam’]=‘no’
»>food
food = {“ham” : “yes”, “egg” : “yes”, “spam” : “no” }

27
Q

change value of dictionary

A

Change the value
»> food = {“ham” : “yes”, “egg” : “yes”, “spam” : “no” }
»> food
{‘egg’: ‘yes’, ‘ham’: ‘yes’, ‘spam’: ‘no’}
»> food[“spam”] = “yes”
»> food
{‘egg’: ‘yes’, ‘ham’: ‘yes’, ‘spam’: ‘yes’}

28
Q

copy and merge dictionary.

A
A dictionary can be copied
food={'ham': 'yes', 'egg': 'yes'}
w=food.copy()
Clean
>>> w.clear()
>>> print w
{}
Merging 
>>> w={"house":"Haus","cat":"Katze","red":"rot”}
>>> w1 = {"red":"rouge","blau":"bleu"}
>>> w.update(w1)
>>> print w
{'house': 'Haus', 'blau': 'bleu', 'red': 'rouge', 'cat': 'Katze'}
29
Q

dif between list and dict

A
lst = list()
>>> lst.append(21)
>>> lst.append(183)
>>> print (lst)
[21, 183]
>>> lst[0] = 23
>>> print(lst)
[23, 183]
ddd = dict()
>>> ddd['age'] = 21
>>> ddd['course'] = 182
>>> print(ddd)
{'course': 182, 'age': 21}
>>> ddd['age'] = 23
>>> print(ddd)
{'course': 182, 'age': 23}
30
Q

find something in dict

A
It is an error to reference a key which is not in the dictionary
We can use the in operator to see if a key is in the dictionary
>>> ccc = dict()
>>> print(ccc['csev'])
Traceback (most recent call last):
  File "", line 1, in 
KeyError: 'csev'
>>> 'csev' in ccc
False
31
Q

gets method

A

We can use get() and provide a default value of zero when the key is not yet in the dictionary - and then just add one
Counts = dict()
Names = [‘bush’, ‘cwen’, ‘zhen’, ‘raja’, ‘cwen’, ‘raja’]
for name in Names :
Counts[name] = Counts.get(name, 0) + 1
print(counts)

32
Q

count words.

A

counts=dict()
A = raw_input(‘Please enter a line of text: ‘)

words = A.split()

print ‘Words:’, words

print ‘Counting…’
for word in words:
counts[word] = counts.get(word,0) + 1
print counts

33
Q

why numpy?

A

ndarray: vectorized arithmetic operations and broadcasting capabilities
Standard mathematical functions for fast operations on entire arrays of data. No need to write loops.
Tools for reading/writing array data
Linear algebra, random number generation and so on

34
Q

how does mapreduce work?

A

MapReduce works by breaking the processing into two phases: the map phase and
the reduce phase. Each phase has key-value pairs as input and output, the types of
which may be chosen by the programmer. The programmer also specifies two
functions: the map function and the reduce function.