Python Basics Flashcards
2 types of functions?
built in functions and custom made
how to create function
define it. def functionname(arguments).
function
function is some stored code we use. we take some input and create output
implicent
when you multipy int and float in implicently becomes float.
converting datatypes
newint = int(oldval)
cant convert string to inv if not number
function
def print_lyrics(): print ("I'm a lumberjack, and I'm okay.”) print ('I sleep all night and I work all day.’)
function argument
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
parameter
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.
return
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.
return is
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
parameter order and number
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
void
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”
what is a list?
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.
describe list
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([]) []
simple for loop examples
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!’)