Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
222 Cards in this Set
- Front
- Back
Line of code that will output: Hello, World! |
print ('Hello, World') |
|
Use _________ to determine which operations are performed first |
Parenthesis |
|
Using a / for division produces a _______. |
Float |
|
A float is a number with a _______. |
Decimal |
|
Computers can't store ______ completely accurately, which can lead to bugs. |
Floats |
|
What are 3 ways a float can be produced? |
float() Running an operation on two floats Running an operation on a float and an integer |
|
How do you perform exponentiation? |
Two asterisks - (Base**Exponent) |
|
Create a sting by putting _______ between ' or " marks. |
Text |
|
How do you include a character that can't be directly included in a string? Ex: a double quote |
Use a backslash |
|
\n represents a ___ ______. |
New Line |
|
___ is automatically put in the output, where you press Enter. IF you put your string inside of _ sets of quotes. |
\n , 3 |
|
A program takes _____ to produce ______ |
Input, output |
|
You can use the ____ function to produce output. |
|
|
How do you prompt the user for input? |
raw_input('prompt') |
|
User input is automatically returned as a ______. (With the contents automatically escaped.) |
String |
|
As with integers and floats, _______ in Python can be added, using a process called __________. |
strings, concatenation |
|
To add a string that contains a number to an integer you must first __________________. |
Convert the string to an integer |
|
You can multiply a string by an integer. (T or F) |
True |
|
You can multiply a string by a float. (T or F) |
False |
|
what is the output of this code: print(3 * '7') |
777 |
|
How do you convert the string "83" into the integer 83? |
int("83") |
|
How do you convert the integer 7 into the float 7.0? |
float(7) |
|
How do you convert the integer 83 into the string '83' |
str(83) |
|
What is the output of this code and why? int('6'+'3') |
63 , because the operation inside the parenthesis is done first and while '6' and '3' are inside the parenthesis they are strings so their sum is '63', which is THEN converted into the integer 63 |
|
How do you convert user input (which is automatically a string) into an integer? |
int(raw_input("prompt:")) |
|
A _______ allows you to store a value by assigning it to a name. |
Variable |
|
To assign a variable use an ____ sign |
= |
|
Variables can be reassigned as many times as you want. (T or F) |
True |
|
You can assign a variable to an integer and then later assign that same variable to a string. (T or F) |
True |
|
The only characters allowed in a variable name are: |
Letters, Numbers, Underscores |
|
is 13_Hab an acceptable variable name? |
No, variable names cannot begin with numbers |
|
Variable names are _____ sensitive. |
case |
|
How do you delete a variable? |
del variable name |
|
You can also take the value of the variable from user input. (T or F) |
True |
|
In-place operators allow you to write code like 'x = x + 3' more concisely, as: |
x + = 3 |
|
What is the output of this code? >>> x = 3 >>> num = 17 >>> print(num % x) |
2 (remainder) |
|
What are the two Boolean values in python? |
True and False |
|
What is the Boolean equal operator in python? |
== |
|
What is this Boolean operator? != |
Not equal to |
|
Is this a true or false statement? 1 != 1 |
False |
|
Is this a true or false statement? 2 != t |
True |
|
What are the Boolean operators for greater than / less than? |
> < |
|
What are these Boolean operators? <= >= |
Greater than / less than (or equal to) |
|
(T or F) The > < operators can be used to compare two strings lexicographically |
True |
|
You can use an __ ________ to run code if a certain condition holds |
if statement |
|
Python uses ______ to delimit blocks of code |
Indentation |
|
The end of an if statement should contain a _. |
: |
|
If statements can be _______ to perform more complex checks. This is one way to see if ______________ are met. |
nested, multiple conditions |
|
What is the output of this code? num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") |
3, because since 7 is not less than 5 the next nested statement is never reached |
|
An else statement follows __________. |
An if statement |
|
An else statement contains code that is called when _____________________. |
The if statement evaluates to false |
|
(T or F) Else statements must be followed by a : |
True |
|
The _____ statement is a shortcut to chaining if else statements. |
elif |
|
elif statements can have a final ________. |
Else statement |
|
The and operator takes two arguments and evaluates as true if _________. |
both arguments are true |
|
The or operator takes two arguments and evaluates to true if ______________. |
Either (or both) arguments is/are true. |
|
The operator not only takes one argument and _________ it. |
Inverts |
|
Each character inside of a sting has a number, this number is called the _____ |
index |
|
what is the output of: 'python'[2] |
t (always begin counting from 0) |
|
what does the string function .len() do? |
Gives you the number of characters in the string. (length) |
|
write a line of code that gets rid of all capitalization in the variable named parrot |
parrot.lower() |
|
What will this line of code do? "parrot".upper() |
Make all letters in the string "parrot" uppercase |
|
str() turns _______ into ________ |
non-strings into strings |
|
Methods using dot notation (such as "String".upper() ) only work with _________. |
Strings |
|
(T or F)
When you want to print a variable inside of a string, the best way is concatenation |
False |
|
What is the output of this code? string_1 = "Camelot" string_2 = "place" print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2) |
Let's not go to Camelot. 'Tis a silly place. |
|
use the % operator to replace the__ placeholders with the variables in parentheses. |
%s |
|
what is missing? a=apples b=titties print "do you prefer %s or %s ?" ___ ( a , b ) |
% |
|
According to operator precedence what is the output of this code? if 1 + 1 * 3 == 6: print("Yes") else: print("No") |
No |
|
What are the 5 basic categories of statements that programming languages have. |
1.) Conditional 2.) Looping 3.) Function 4.) Assignment 5.) Declaration
|
|
______ = 5.0 / 3.0 * 6.0 |
10.0 |
|
When a function is executed, what happens when the end of the function statement block is reached? |
Execution returns to the statement that called it. |
|
For a function to be used in an expression, it must _____________ . |
Return a Value |
|
A __________ is the part of a program in which a variable may be accessed. |
Scope |
|
while in a loop, a _____________ command allows to to exit before the completion of the loop |
Break |
|
A simple conditional statement taught in this course starts with the key word _____ . |
if |
|
What are two keywords used to start loop (repeating) statements in Python 2.7? |
For & While |
|
A loop that repeats a specific number of times is know as a(n)_________________. |
Count controlled loop |
|
What value will be printed after this code in run?x = 12 y = 10 y += 15 myvar = x + y print myvar |
37 |
|
______________ gives us this ability to choose among outcomes based off what else is happening in the program. |
Control flow |
|
evaluate not 41>40 |
False |
|
evaluate not 65 < 50 |
True |
|
evaluate not not 50 != 50 |
False |
|
What is the order of operations for boolean operators? |
1.) not 2.) and 3.) or |
|
an __/_____ statement says "If this expression is true, run this indented code block; otherwise, run this code after the else statement." |
if/else |
|
use _________ to determine if a string is letters. |
STRING.isalpha() |
|
Create a line of code that checks if user input has more than 0 characters AND is all letters |
if len(INPUT) > 0 and INPUT.isalpha() |
|
when selecting a section of a string that should include the end, for the second index you can use .len() or you can ______. |
Leave second index blank |
|
(T or F) False or not 3 <= 5 * 2 |
False |
|
How do you use pi? |
from math import pi |
|
How do you use the sleep function? |
from time import sleep |
|
Write some code that will print the current date and time |
from datetime import datetime now = datetime.now() print now |
|
How do you print a float to the second decimal place using string formatting? |
%.2f |
|
How do you insert a float using string formatting? |
%f |
|
What are the 3 general parts of a function? |
1.) Header (includes 'def') 2.) Comment (optional) 3.) body (indented) |
|
what is n?
def square(n):
|
a parameter, which acts as a variable name for a passed in argument. |
|
T or F You can call another function from within a function |
True |
|
What does this code do? def cube(number): return number**3 def by_three(number): if number % 3 == 0: return cube(number) else: return False |
when cube is called it will return the cube of the number. when by_three is called it will return the number cubed IF the number is divisible by three |
|
Write a line of code that prints the square root of 25 using built in functions |
import math print math.sqrt(25) |
|
How do you import just one function from a module? |
from module import function |
|
What if you want all of the math functions but don't want to keep typing in ' math. ' before calling each function, what can be done? |
from math import * (Universal import) |
|
Why is it not always a good idea to use a universal import? |
if you have functions named the same as the functions in the module it can confuse the program. |
|
What will this code return? max( 2 , 6 , 9, -10) |
9 |
|
What is the absolute value function? |
abs() |
|
what does the type() function do? |
returns the data type of the argument: (str, int, float) |
|
abs() is part of the math module (T or F) |
False |
|
(T or F) it is acceptable to call a function with a different variable name than the one used when the function was originally created |
True |
|
What will this code do? from random import randint randint(1,99) |
give you a random integer between 1 and 99
|
|
%d prints ______ as %s prints strings |
Integers |
|
A list goes between _______ separated by ______ |
Brackets , commas |
|
(T or F) A list is a data type |
True |
|
You can access a certain element of a list by its ________ |
Index |
|
Computer scientists love to start counting from ____ |
0 |
|
How do you assign a new element to a specific index in list?
|
list_name[desired index] = entry |
|
what does the append function do when used in a list? |
Adds the appended element to the end of the list |
|
How do you append an element to a list? |
list_name.append("New Element") |
|
How do you take just the portion of this list between the second and third positions? letters = ['a', 'b', 'c', 'd', 'e'] *remember to begin counting at 0 and that the number after the colon is NOT included |
portion = letters[1:3] |
|
what can you use to find the index of a certain element in a list? |
list_name.index("element") |
|
How would you insert the item "Blue" as the second item in the list named colors? |
colors.insert(1,"Blue") |
|
if you want to do something with every item in a list you can use a ___ ______. |
For Loop |
|
How do you use a for loop for every item in a list? |
for item in list_name: |
|
how can you alphabetically or numerically sort a list? |
list_name.sort() |
|
Dictionaries are enclosed in __. |
{} |
|
When accessing a value from a dictionary you use a ___ instead of an index |
Key |
|
(T or F) When accessing a value from a dictionary you use curly brackets. |
False, use [] brackets as in a list |
|
(T or F) You can put lists inside of dictionaries |
True |
|
How is a dictionary formatted? |
{ 'Key' : value , 'Key2' : value } |
|
How do you add a new key-value pair to a dictionary? |
dictionary_name['Key'] = Value |
|
How would you delete the key-value pair with the key "Blue" in the dictionary "Colors" |
del colors['Blue'] |
|
How do you remove an item from a list? |
list_name.remove('item') |
|
(T or F) you can use a for loop on a dictionary. |
True |
|
(T or F) Dictionaries are oredered |
False, dictionaries are unordered |
|
(T or F) You can access the key and the value of a dictionary with a for loop |
True |
|
You can use a for loop on a string to perform an action on ____________ in the string. |
every character |
|
What does this code do?
def compute_bill(food): total = 0 for n in food: total += prices[n] return total |
1.) defines a function called compute_bill that takes an argument called 'food' 2.) sets the variable 'total' to 0 3.) for every item in the list 'food' it will find that item by key in the dictionary 'prices' and add its value to the other items in the 'food' list 4.) prints result |
|
What will this code print?
my_dict = {"shirts": 5, "pants": 4, "shoes": 2} print my_dict["pants"] |
4 |
|
List_name[a:b] will return the portion of list_name starting ____ the index a and ending ____ the index b. |
With, before |
|
(T or F)
items in a list must be inside of quotes |
False |
|
When you divide an integer by an integer the result is always a ______ _________ down |
integer rounded |
|
What does this .pop() function do? listname.pop(x) |
removes the item at index x and returns list |
|
what does this .remove() function do? listname.remove(x) |
remove the actual item x if it finds it |
|
what is the function which removes the item at the given index from the list but does not return the list? |
del(listname[index]) |
|
Besides using a for loop, how can you iterate through items in a list? |
Using the range function |
|
use the range function to perform the same task on a list as the ## for item in list: function |
for i in range(len(list)): |
|
(T or F) The range function must be attributed to a certain list name when used in a for loop |
False |
|
what will this code do? letters = ['a' , 'b' , 'c'] print ' '.join(letters) |
will print the letters list with a space in place of the quotation marks and commas |
|
how do you select an element from a list within a list? |
listname[outer_list_index][inner_list_index] |
|
What will this code return? my_list = [8, 6, 4, 2] my_list.pop(1) |
[8, 4, 2] |
|
What will this code return? my_list = [1, 3, 5, 7] my_list.remove(3) |
[1, 5, 7] |
|
What will this code return? my_list = [1, 2, 3, 5] my_list[2] = my_list[1] + 2 |
[1, 2, 4, 5] |
|
Replace the line in the function so that the functions prints every other list item. def print_list(x): for counter in ________________: print x[counter] |
range(0, len(x),2) |
|
A while loop executes _____________________. |
WHILE a condition holds true |
|
What is one method for using a while loop so that it does not become infinite and iterates a certain amount of times? |
Use an increment count |
|
(T or F) You can use a while / else loop |
True |
|
What will putting a comma after a print statement do? |
Prevent the next output from going to the next line |
|
When looping over the dictionary do you get the key or the value? |
you get the key which you can use to get the value. |
|
What built in function allows you to iterate over two lists at once? |
the zip function |
|
(T or F) The zip function can handle 4 lists at once |
True |
|
(T or F)
The zip function will stop at the end of the longer list |
False, it will stop at the end of the shorter list |
|
Write an opening line of code that uses the zip function on a list called list_a and list_b |
For a , b in zip(list_a , list_b): |
|
how do you separate a sentence (string) into a list with each word? |
string.split() |
|
what will this code do? word = 'apples' print len(word) |
return the length of the word 'apples' |
|
What does the zip function do? |
It creates pairs of elements when passed two lists |
|
What function can be used to supply an index to each element of a list as you iterate through it? |
enumerate() |
|
Besides datetime what is another function you can use to obtain current time functions? |
strftime |
|
Write a line of code that will check the amount of keys in a dictionary |
len(dictionary.keys()) |
|
What does the continue function do? |
Continues with the next iteration of the loop |
|
(T or F) when using the .items() function on a dictionary, it will print the items (keys and values) in the order they are in the dictionary |
False |
|
Write a line of code that will return all the keys for a dictionary in no particular order |
dictionary.keys() |
|
Write a line of code that will return all the values for a dictionary in no particular order |
dictionary.values() |
|
what is the format for taking only certain elements from a list? |
[start:end:stride] |
|
How do you print a list in reverse? |
make the stride negative [::-1] |
|
What is a lambda? |
an anonymous function |
|
what is the syntax for using a lambda? |
filter(lambda x: CONDITION , list_to_filter) |
|
If you want to write a number in binary what must it begin with? |
0b |
|
how do you convert an integer into binary? |
bin() |
|
What is XOR? |
Will return a true (1) if EITHER of the compared values are a one, but NOT BOTH |
|
What is a class? |
a way of organizing and producing objects with similar attributes and methods. |
|
How do you initialize the object(s) of a class? |
def __init__(self): |
|
What should you use as the first parameter when instantiating a class? |
self |
|
You can access an attribute of a class using ___ notation |
dot |
|
how can you give all objects of a class the same variable without actually initializing it? |
inside of the class, before __init__(), create a new variable and set it equal to the desired value |
|
What do you call a function inside of a class? |
A method |
|
(T or F) When creating a method, you do not have to pass in the self argument if you already did so in __init__() |
False |
|
Write a line of code that calls the method 'description' on the object 'hippo' and prints it |
print hippo.description() |
|
what is inheritance? |
the process by which one class takes on the attributes and methods of another |
|
What does this code do? class Triangle(Shape): |
opens a class called 'Triangle' that Inherits the class 'Shape' |
|
when using dot notation, does the object or the class name come first? |
object |
|
What does this class inherit? class ClassName(object): |
the properties of an object, which is the simplest, most basic class |
|
What is a variable that is only available to a certain class called? |
A member / member variable |
|
An instance of a class automatically has access to what variables? |
Member variables |
|
This is the correct way to print the 'condition' of 'my_car' class Car(object): ____condition = 'new' my_car = Car() print my_car.condition |
True |
|
This is the correct way to print the 'condition' of 'my_car' class Car(object): ____condition = 'new' my_car = Car() print condition.my_car() |
False |
|
What should the first argument passed to __init__() be? |
self |
|
This is the correct way to initialize a class def __init__(self, new_var): ----new_var = self.new_var |
False |
|
This is the correct way to initialize a class def __init__(self, new_var): ----self.new_var = new_var |
True |
|
What is the format for calling a method inside of a class? |
instance = ClassName(att1, att2, att3) print instance.method() |
|
What will the following print to the console? my_list = range(10) print filter(lambda x: x**2 / 3 < 5, my_list) |
[0, 1, 2, 3] |
|
What is this list comprehension equal to? [i for i in range(10) if i % 3 == 1] |
[1, 4, 7] |
|
When does python write data to a file? |
When you close the file |
|
How do you call a method from another method that is from the same class? |
self.method_name() |
|
What is file I/O? |
File input / output |
|
Create a variable, my_file, and set it equal to calling the open() function on output.txt. In this case, pass "r+" as a second argument to the function so the file will allow you to readand write to it! |
my_file = open('output.txt', 'r+') |
|
What does the use of 'w' do when opening a file? |
opens in write-only mode |
|
How do you open a file in read only mode? |
my_file = open('filename.txt', 'r') |
|
How do you open a file in read and write mode? |
my_file = open('filename.txt', 'r+') |
|
How do you open a file in append mode? |
my_file = open('filename.txt', 'a') |
|
What MUST you do in order for python to write to the file properly? |
Close the file when done writing to it |
|
my_file.write() will only take what as an argument? |
strings |
|
How do you close the file when you are done writing to it? |
my_file.close() |
|
What do you have to do before reading or writing to a file? |
open it (determine how you would like to) |
|
write a line of code that prints the result of reading my_file |
print my_file.read() |
|
How do you read one line of a file at a time? |
.readline() |
|
What is buffered data? |
data that it is held in a temporary location before being written to the file. |
|
what is the benefit of using with and as? |
python will automatically close the file for you |
|
What is the syntax for using with and as? |
with open("file", "mode") as variable: |
|
what does the .closed attribute do? |
returns True if the file is closed and False otherwise |
|
What function is required for classes to initialize objects? |
__init__() |
|
How does using a for loop on a file work? |
for line in f: |
|
(T or F) Lists can be added and multiplied |
True |
|
What will this code output?
list1= [1, 2, 3] print(list1 + [4, 5, 6]) |
[1, 2, 3, 4, 5, 6] |
|
How does the 'in' operator work? |
Checks if an item is in something (like a list or string) and then returns True if it is and False if it is not. |