python theory Flashcards
Python’s governing body recommends breaking up multi-word
variables with
underscore (__)
another way to write age = age + 1 is
age += 1
In a list, these
values are known as
elements
enclosed in square brackets is what kind of data type
lists
Return keyword is used to show the results in or within using
functions, methods, lambdas
A list can contain various data types (True or False)
True
.append keyword for the list does what
add element to the end of the list
todays_tasks = todays_tasks + [“Washing the car”, “Cleaning the car”]
what is happening in the code
a way to add elements to the list using a list
if you want to add something new in the middle of the list what keyword would you use
insert
cities.insert(0, “New York”) what is happening here
using insert the work new york is places first in the list of cities.
in slicing the number following the colon is the index number of the element that
comes _____(when) the last element in the slice:
after
keywords used to remove items in a list
del and .remove
the .remove need what
the string value
to strike off an element of a list the keyword used is
.pop
what is similar to a list but elements are fixed unless the entire thing is redefined
tuple
We’re looping through the list y and assigning each element to the variable x. Code the first line.
for x in y:
what statement is used to stop the loop
break
contains an element less than 1, meaning
c < 1:
that you ____ provide a variable to hold the user’s input. If you omit
it python breaks
must
the default data type of a input by a user is
string
what does .lower() do
for a list, variable or anything it changes all to lower case
what does .title() do
The first letter of the word is capitalized like “Cheyenne” to “Cheyenne”
the data type that has ( ) brackets
tuples
the data type that has [ ] brackets
List
______ is a series of pairs of things generally the key and a value
Dictionary
The data type { } bracket is used with
dictionaries
do keys in dictionaries have to be string
false, no they can also be number
customer_29876[“city”] = “Toronto” what is happening here
for a dictionary a new pair with a key of city and value of Toronto is being added
can you define a dictionary with no key value pairs and if so what is it called
yes, its the empty dictionary
the index (in the list) or the key (in the dictionary) can be used to change the value of an element (True or False)
True
.values (): is used for and need in
looping dictionary values
within loops don’t forget to break it or else it will create
errors or stop functioning
can you loop through the keys of the dictionary (true or false)
true, use .keys() to do that
what keyword do you use to list all the elements in the dictionary and what must you make sure
the keyword is .items and you use make sure to use 2 variables to check in the for line
A _____ is a block of Python code that robotically does the same thing again
and again, whenever you invoke its name
function
what keyword is used to start a function
def
a function doesn’t do anything until it’s ____
called
numbers inside the parentheses of a function is known as an
argument add_numbers(53, 109)
A variable inside the parentheses in a function definition is known as a
parameter – def add_numbers(first_number, second_number):
Arguments like this are ______ arguments—arguments that are loaded into
function parameters in order, like a line of customers loaded into the cars of a
theme park ride
positional
for a dictionary what does this command do find_something(customers, 2, “last name”)
finds the last name of customer 2 in a dictionary named customers
def find_something(dict, inner_dict, target): 2 print(dict[inner_dict][target])
printing a specific part of the dictionaries within one
_________ arguments are arguments that need to be included in the proper position or order.
positional
in the following code, the first argument is a _____________ argument.
give_greeting(“Hello there”, first_name=”Al”)
positional
In the following code, the second argument is a _____________ argument.
give_greeting(“Hello there”, first_name=”Al”)
keyword
_____ arguments (or named arguments) are values that, when passed into a function, are identifiable by specific parameter names
keyword
______ arguments enable you to omit arguments for some parameters
Optional
How does the function handle the optional arguments? It puts them in a _______
dictionary
to have a function that displays additional information (optional arguments) you must use
** two asterisks
how do you assign a default parameter in the function
def analyze(qty = 0):
a function is a variable (True or False)
True
A ______ variable is one you define in the main body of your code—that is,
not in a function
global
A ______ variable is one that you define in a function.
local
def call_another_function() what is happening here
a function is defined by calling another function
how do you define a class
class name:
what keyword can be used when you do not have any code in the class or a function but want to write more code outside it
pass
what is a part of blueprint (class) that allows us to specify what should happen when the object is constructed
a constructor (aka. initializing an object)