Python + Flashcards

1
Q

Use _________ to determine which operations are performed first

A

Parenthesis

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

Computers can’t store ______ completely accurately, which can lead to bugs.

A

Floats

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

How do you include a character that can’t be directly included in a string? Ex: a double quote

A

Use a backslash

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

How do you prompt the user for input?

A

input(‘prompt’)

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

User input is automatically returned as a ______. (With the contents automatically escaped.)

A

String

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

As with integers and floats, _______ in Python can be added, using a process called __________.

A

strings, concatenation

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

To add a string that contains a number to an integer you must first __________________.

A

Convert the string to an integer

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

You can multiply a string by a float. (T or F)

A

False

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

what is the output of this code:

print(3 * ‘7’)

A

777

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

How do you convert the string “83” into the integer 83?

A

int(“83”)

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

How do you convert the integer 7 into the float 7.0?

A

float(7)

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

What is the output of this code and why? \n\n\nint(‘6’+’3’)

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you convert user input (which is automatically a string) into an integer?

A

int(input(“prompt:”))

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

Variables can be reassigned as many times as you want. (T or F)

A

True

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

You can assign a variable to an integer and then later assign that same variable to a string. (T or F)

A

True

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

The only characters allowed in a variable name are:

A

Letters, Numbers, Underscores

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

is 13_Hab an acceptable variable name?

A

No, variable names cannot begin with numbers

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

Variable names are _____ sensitive.

A

case

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

How do you delete a variable?

A

del variable name

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

You can also take the value of the variable from user input. (T or F)

A

True

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

In–place operators allow you to write code like \n’x = x + 3’ more concisely, as:

A

x + = 3

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

What is the output of this code?
x = 3
num = 17
print(num % x)

A

2 (remainder)

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

What is the Boolean equal operator in python?

A

==

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

What is this Boolean operator? !=

A

Not equal to

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

Is this a true or false statement?\n\n\n1 != 1

A

False

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

Is this a true or false statement?\n\n\n2 != t

A

True

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

What are the Boolean operators for greater than / less than?

A

>

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

What are these Boolean operators?

<= >=

A

Greater than / less than (or equal to)

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

You can use an __ ________ to run code if a certain condition holds

A

if statement

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

Python uses ______ to delimit blocks of code

A

Indentation

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

The end of an if statement should contain a _.

A

:

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

If statements can be _______ to perform more complex checks. This is one way to see if ______________ are met.

A

nested, multiple conditions

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

What is the output of this code?\nnum = 7\nif num > 3: \n print(“3”) \n if num < 5: \n print(“5”) \n if num ==7: \n print(“7”)

A

3, because since 7 is not less than 5 the next nested statement is never reached

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

An else statement follows __________.

A

An if statement

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

An else statement contains code that is called when _____________________.

A

The if statement evaluates to false

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

(T or F)

Else statements must be followed by a :

A

True

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

The _____ statement is a shortcut to chaining if else statements.

A

elif

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

The and operator takes two arguments and evaluates as true if _________.

A

both arguments are true

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

The or operator takes two arguments and evaluates to true if ______________.

A

Either (or both) arguments is/are true.

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

Each character inside of a sting has a number, this number is called the _____

A

index

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

what is the output of:\n\n\n’python’[2]

A

t (always begin counting from 0)

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

what does the string function .len() do?

A

Gives you the number of characters in the string.\n(length)

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

write a line of code that gets rid of all capitalization in the variable named parrot

A

parrot.lower()

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

What will this line of code do?”parrot”.upper()

A

Make all letters in the string “parrot” uppercase

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

str() turns _______ into ________

A

non–strings into strings

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

Methods using dot notation (such as “String”.upper() ) only work with _________.

A

Strings

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

T or F) \n \nWhen you want to print a variable inside of a string, the best way is concatenation

A

False
(name = “Eric” age = 74)
f”Hello, {name}. You are {age}.”)

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

use the % operator to replace the__ placeholders with the variables in parentheses.

A

%s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q
According to operator precedence what is the output of this code?
if 1 + 1 * 3 == 6:    
print("Yes")
else:   
print("No")
A

No

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

What are the 5 basic categories of statements that programming languages have.

A
Conditional
Looping
Function
Assignment 
Declaration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

______ = 5.0 / 3.0 * 6.0

A

10.0

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

When a function is executed, what happens when the end of the function statement block is reached?

A

Execution returns to the statement that called it.

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

For a function to be used in an expression, it must _____________ .

A

Return a Value

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

A __________ is the part of a program in which a variable may be accessed.

A

Scope

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

while in a loop, a _____________ command allows to to exit before the completion of the loop

A

Break

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

A simple conditional statement taught in this course starts with the key word _____ .

A

if

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

What are two keywords used to start loop (repeating) statements in Python?

A

For and While

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

A loop that repeats a specific number of times is know as a(n)_________________.

A

Count controlled loop

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

What value will be printed after this code in run?
x = 12\ny = 10\ny += 15
myvar = x + y
print myvar

A

37

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

______________ gives us this ability to choose among outcomes based off what else is happening in the program.

A

Control flow

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

evaluate

not 41>40

A

False

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

evaluate

not 65 < 50

A

True

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

evaluate

not not 50 != 50

A

False

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

What is the order of operations for boolean operators?

A

not
and
or
(in?)

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

an __/_____ statement says “If this expression is true, run this indented code block; otherwise, run this code after the else statement.”

A

if/else

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

use _________ to determine if a string is letters.

A

STRING.isalpha()

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

Create a line of code that checks if user input has more than 0 characters AND is all letters

A

if len(INPUT) > 0 and INPUT.isalpha()

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

when selecting a section of a string that should include the end, for the second index you can use .len() or you can ______.

A

Leave second index blank

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

How do you use pi?

A

from math import pi

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

How do you use the sleep function?

A

from time import sleep

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

Write some code that will print the current date and time

A

from datetime import datetime\nnow = datetime.now()\nprint now

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

How do you print a float to the second decimal place using string formatting?

A

%.2f

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

How do you insert a float using string formatting?

A

%f

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

What are the 3 general parts of a function?

A

Header (includes ‘def’)
Comment (optional)
body (indented)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
75
Q
what is n?
def square(n):
A

a parameter, which acts as a variable name for a passed in argument.

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

T or F You can call another function from within a function

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q
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
A

when cube is called it will return the cube of the number.\n\n\nwhen by_three is called it will return the number cubed IF the number is divisible by three

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

Write a line of code that prints the square root of 25 using built in functions

A

import math

print math.sqrt(25)

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

How do you import just one function from a module?

A

from module import function

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

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?

A

rom math import *\n(Universal import)

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

Why is it not always a good idea to use a universal import?

A

if you have functions named the same as the functions in the module it can confuse the program.

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

What will this code return?

max( 2 , 6 , 9, –10)

A

9

83
Q

What is the absolute value function?

A

abs()

84
Q

what does the type() function do?

A

returns the data type of the argument:\n(str, int, float)

85
Q

(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

A

True

86
Q

What will this code do?

from random import randint(1,99)

A

give you a random integer between 1 and 99

87
Q

%d prints ______ as %s prints strings

A

Integers

88
Q

A list goes between _______ separated by ______

A

Brackets , commas

89
Q

sorting a list

A

sorted() returns a new list
sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]

90
Q

.sort()

A

The sort() method sorts the elements of a given list in a specific ascending or descending order. The syntax of the sort() method is: list.sort(key=…, reverse=…)

Modifies existing list

91
Q

How do you assign a new element to a specific index in list?

A

list_name[desired index] = entry

92
Q

what does the append() function do when used in a list?

A

Adds the appended element to the end of the list (eg. append(listmember))

93
Q

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)

A

portion = letters[1:3]

94
Q

what can you use to find the index of a certain element in a list?

A

list_name.index(“element”)

95
Q

How would you insert the item “Blue” as the second item in the list named colors?

A

colors.insert(1,”Blue”)

96
Q

if you want to do something with every item in a list you can use a ___ ______.

A

For loop

97
Q

How do you use a for loop for every item in a list?

A

for item in list_name:

98
Q

When accessing a value from a dictionary you use a ___ instead of an index

A

Key

99
Q

(T or F) When accessing a value from a dictionary you use curly brackets.

A

False, use [] brackets as in a list

100
Q

(T or F)\n\n\nYou can put lists inside of dictionaries

A

True

101
Q

How is a dictionary formatted?

A

{ ‘Key’ : value , ‘Key2’ : value }

102
Q

How do you add a new key–value pair to a dictionary?

A

dictionary_name[‘Key’] = Value

103
Q

How would you delete the key–value pair with the key “Blue” in the dictionary “Colors”

A

del colors[‘Blue’]

104
Q

How do you remove an item from a list?

A

list_name.remove(‘item’)

105
Q

(T or F)\nyou can use a for loop on a dictionary.

A

True

106
Q

(T or F)

You can access the key and the value of a dictionary with a for loop

A

True

107
Q

You can use a for loop on a string to perform an action on ____________ in the string.

A

every character

108
Q
What does this code do?
def compute_bill(food): 
total = 0 
for n in food: 
total += prices[n] 
return total
A
  1. ) defines a function called compute_bill that takes an argument called ‘food’
    2) sets the variable ‘total’ to 0
  2. ) 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
  3. ) prints result
109
Q

What will this code print?
my_dict = {“shirts”: 5, “pants”: 4, “shoes”: 2}
print my_dict[“pants”]

A

4

110
Q

What does this .pop() function do?listname.pop(x)

A

removes the item at index x and returns list

111
Q

(T or F) items in a list must be inside of quotes

A

False

112
Q

When you divide an integer by an integer the result is always a ______ _________ down

A

integer rounded

113
Q

what will this code do?
letters = [‘a’ , ‘b’ , ‘c’]
print ‘ ‘.join(letters)

A

will print the letters list with a space in place of the quotation marks and commas

114
Q

what is the function which removes the item at the given index from the list but does not return the list?

A

del(listname[index])

115
Q

Besides using a for loop, how can you iterate through items in a list?

A

Using the range function

116
Q

use the range function to perform the same task on a list as the ## for item in list: function

A

for i in range(len(list)):

117
Q

(T or F) \n\n\nThe range function must be attributed to a certain list name when used in a for loop

A

False

118
Q

what will this code do?
letters = [‘a’ , ‘b’ , ‘c’]
print ‘ ‘.join(letters)

A

will print the letters list with a space in place of the quotation marks and commas

The join() method provides a flexible way to create strings from iterable objects. It joins each element of an iterable (such as list, string, and tuple) by a string separator (the string on which the join() method is called) and returns the concatenated string.

119
Q

(T or F) The range function must be attributed to a certain list name when used in a for loop

A

False

120
Q

how do you select an element from a list within a list?

A

listname[outer_list_index][inner_list_index]

121
Q

What will this code return?
my_list = [8, 6, 4, 2]
my_list.pop(1)

A

[8, 4, 2]

122
Q

What will this code return?

my_list = [1, 3, 5, 7] \nmy_list.remove(3) [1, 5, 7]

A

[1, 5, 7]

123
Q

What will this code return?
my_list = [1, 2, 3, 5]
my_list[2] = my_list[1] + 2

A

[1, 2, 4, 5]

124
Q

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]
A

range(0, len(x),2)

125
Q

A while loop executes _____________________.

A

WHILE a condition holds true

126
Q

What is one method for using a while loop so that it does not become infinite and iterates a certain amount of times?

A

Use an increment count

127
Q

(T or F) You can use a while / else loop

A

True

128
Q

When looping over the dictionary do you get the key or the value?

A

you get the key which you can use to get the value.

129
Q

What built in function allows you to iterate (gentage) over two lists at once?

A

the zip function

130
Q

(T or F) The zip function can handle 4 lists at once

A

True

131
Q

(T or F) The zip function will stop at the end of the longer list

A

False, it will stop at the end of the shorter list

132
Q

Write an opening line of code that uses the zip function on a list called list_a and list_b

A

For a , b in zip(list_a , list_b):

133
Q

how do you separate a sentence (string) into a list with each word?

A

string.split()

The split() method splits a string into a list.

134
Q
What will this code do?
word = 'apples'
print len(word)
A

return the length of the word ‘apples’

135
Q

What does the zip function do?

A

It creates pairs of elements when passed two lists
fx
number_list = [1, 2, 3]
str_list = [‘one’, ‘two’, ‘three’]

Output: {(2, ‘two’), (3, ‘three’), (1, ‘one’)}

136
Q

What function can be used to supply an index to each element of a list as you iterate through it?

A

enumerate()

137
Q

Besides datetime what is another function you can use to obtain current time functions?

A

strftime

138
Q

Write a line of code that will check the amount of keys in a dictionary

A

len(dictionary.keys())

139
Q

(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

A

False

140
Q

Write a line of code that will return all the keys for a dictionary in no particular order

A

dictionary.keys()

141
Q

what is the format for taking only certain elements from a list?

A

list[start:end:stride]

142
Q

How do you print a list in reverse?

A

make the stride negative

[::–1]

143
Q

What is a lambda?

A
an anonymous function
fx: 
def myfunc(n):
  return lambda a : a * n

mydoubler = myfunc(2)

print(mydoubler(11))
Output: 22

144
Q

what is the syntax for using a lambda? filter

A

(lambda x: CONDITION , list_to_filter)

145
Q

If you want to write a number in binary what must it begin with?

A

0b

146
Q

how do you convert an integer into binary?

A

bin()

147
Q

What is XOR?

How do you initialize the object(s) of a class? def __init__(self):

A

XOR, also known as “exclusive or”, compares two binary numbers bitwise. If both bits are the same, XOR outputs 0. If the bits are different, XOR outputs 1.

148
Q

What is a class?

A

a way of organizing and producing objects with similar attributes and methods.

Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor, or a “blueprint” for creating objects.

149
Q

How do you initialize the object(s) of a class?

initialize = to set (something, such as a computer program counter)

A

def __init__(self):

150
Q

What should you use as the first parameter when instantiating a class?

A

self

151
Q

You can access an attribute of a class using ___ notation

A

dot

152
Q

how can you give all objects of a class the same variable without actually initializing it?

A

inside of the class, before __init__(), create a new variable and set it equal to the desired value

153
Q

What do you call a function inside of a class?

A

A method

154
Q

(T or F) When creating a method, you do not have to pass in the self argument if you already did so in __init__()

A

False

155
Q
Write a line of code that calls the method 'description' on the object 'hippo' and prints it	
what is inheritance?	the process by which one class takes on the attributes and methods of another
What does this code do?\n\n\nclass Triangle(Shape):	opens a class called 'Triangle' that Inherits the class 'Shape'
A

print hippo.description()

156
Q

what is inheritance?

A

the process by which one class takes on the attributes and methods of another

157
Q
What does this code do?
class Triangle(Shape):
A

opens a class called ‘Triangle’ that Inherits the class ‘Shape’

158
Q

when using dot notation, does the object or the class name come first?

A

object

159
Q
What does this class inherit?
class ClassName(object):
A

the properties of an object, which is the simplest, most basic class

160
Q

What is a variable that is only available to a certain class called?

A

A member / member variable

161
Q

An instance of a class automatically has access to what variables?

A

Member variables

162
Q
This is the correct way to print the 'condition' of 'my_car'
class Car(object):
\_\_\_\_condition = 'new' 
my_car = Car() 
print my_car.condition
A

True

163
Q
This is the correct way to print the 'condition' of  'my_car'
class Car(object):
\_\_\_\_condition = 'new' 
my_car = Car()
print condition.my_car()
A

False

164
Q

What is the format for calling a method inside of a class?

A

instance = ClassName(att1, att2, att3)\nprint instance.method()

165
Q
What will the following print to the console?
my_list = range(10) 
print filter(lambda x: x**2 / 3 < 5, my_list)
A

[0, 1, 2, 3]

166
Q

What is this list comprehension equal to?\n\n\n [i for i in range(10) if i % 3 == 1]

A

[1, 4, 7]

167
Q

When does python write data to a file?

A

When you close the file

168
Q

What is file I/O?

A

File input / output

169
Q

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!

A

my_file = open(‘output.txt’, ‘r+’)

170
Q

What does the use of ‘w’ do when opening a file?

A

opens in write–only mode

171
Q

How do you open a file in read only mode?

A

my_file = open(‘filename.txt’, ‘r’)

172
Q

How do you open a file in read and write mode?

A

my_file = open(‘filename.txt’, ‘r+’)

173
Q

How do you open a file in append mode?

A

my_file = open(‘filename.txt’, ‘a’)

174
Q

What MUST you do in order for python to write to the file properly?

A

Close the file when done writing to it

175
Q

my_file.write() will only take what as an argument?

A

strings

176
Q

How do you close the file when you are done writing to it?

A

my_file.close()

177
Q

What do you have to do before reading or writing to a file?

A

open it (determine how you would like to)

178
Q

write a line of code that prints the result of reading my_file

A

print my_file.read()

179
Q

How do you read one line of a file at a time?

A

.readline()

180
Q

What is buffered data?

A

data that it is held in a temporary location before being written to the file.

181
Q

what does the .closed attribute do?

A

returns True if the file is closed and False otherwise

182
Q

What function is required for classes to initialize objects?

A

__init__()

183
Q

How does using a for loop on a file work?

A

for line in f:

184
Q

T or F) Lists can be added and multiplied

A

True

185
Q

What will this code output?
list1= [1, 2, 3]
print(list1 + [4, 5, 6])

A

[1, 2, 3, 4, 5, 6]

186
Q

How does the ‘in’ operator work?

A

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.

187
Q
What is the output? 
sweet = "cookies"
savory = "pickles"
num = 100 
print (num, savory,"and",num, sweet)
print ("I choose the", sweet.upper())
A

100 pickles and 100 cookies

I choose the COOKIES

188
Q

Fastest way to make a list of numbers with range:

A

l1 = list(range(2,41))

print(l1)

189
Q

Fastest way to make a list with same numbers with for loop:

A

my_list = [1 for i in range(50)]

print (my_list)

190
Q

items()

A

The items() method returns a view object. The view object contains the key-value pairs of the dictionary, as tuples in a list.

191
Q

Abs()

A

Absolut value

192
Q

Try:
Pass:
Except:

A

The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.

We can thus choose what operations to perform once we have caught the exception.

In the above example, we did not mention any specific exception in the except clause.

This is not a good programming practice as it will catch all exceptions and handle every case in the same way. We can specify which exceptions an except clause should catch.

A try clause can have any number of except clauses to handle different exceptions, however, only one will be executed in case an exception occurs.

193
Q

File I/O

A

open() returns a file object

open(filename, mode)
mode (optional) describes how the file is used:
– ‘r’ for reading (default)
– ‘w’ for writing (overwrites existing file)
– ‘a’ for appending (appends to existing file)
– ‘r+’ for reading and writing

194
Q

How to reverse a list with a method

A

The reverse() method reverses the elements of the list.

list.reverse()

195
Q

Reverse a List Using Slicing Operator

A
# Operating System List
systems = ['Windows', 'macOS', 'Linux']
# Reversing a list	
reversed_list = systems[::-1] (eg. [start:stop:step] )
# updated list
print(reversed_list)
196
Q

How to round a float

A

round(float_num/variable, num_of_decimals)

197
Q

what does pow() do?

A

The pow() function returns the value of x to the power of y (xy).

x = pow(4, 3)

print(x)
Output = 64

198
Q

How to replace something

A

replace() - The replace() method returns a copy of the string where the old substring is replaced with the new substring. The original string is unchanged.

song = 'cold, cold heart'
# replacing 'cold' with 'hurt'
print(song.replace('cold', 'hurt'))
song = 'Let it be, let it be, let it be, let it be'
# replacing only two occurences of 'let'
print(song.replace('let', "don't let", 2))

Output:
hurt, hurt heart
Let it be, don’t let it be, don’t let it be, let it be

199
Q

What does the strip() func do?

A
The strip() method returns a copy of the string by removing both the leading and the trailing characters (based on the string argument passed).
If nothing passed it removes whitespaces
remember to leave a whitespace, when using it (eg. string.strip(" a") 

string = ‘ xoxo love xoxo ‘

# Leading and trailing whitespaces are removed
print(string.strip())

All ,x,o,e characters in the left & right of string are removed:

print(string.strip(‘ xoe’))

# Argument doesn't contain space
# No characters are removed.
print(string.strip('stx'))

string = ‘android is awesome’
print(string.strip(‘an’))

Output
xoxo love xoxo
lov
  xoxo love xoxo   
droid is awesome
200
Q

Write a line of code that will check the amount of keys in a dictionary

A

len(dictionary.keys())

201
Q

re.sub

A

This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. This method returns modified string.

fx: 
#Remove anything other than digits
num = re.sub(r'\D', "", phone)    
print "Phone Num : ", num
Output:
Phone Num :  2004959559
202
Q

what does dict.keys() do?

A

Python dictionary method keys() returns a list of all the available keys in the dictionary.

203
Q

Files = os.listdir(“texts”)
dicts = {}
for f in files:
text = open(“texts/”+f,”r”,encoding=’UTF8’)

A

os.listdir() This method returns the list of all files and directories in the specified path. The return type of this method is list
Next a for loop to loop through files in folder