1/74
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What will the output of the following code be?
x = 7if x > 10: print("Greater than 10")elif x > 5: print("Greater than 5 but less than or equal to 10")else: print("5 or less")
greater than 5 but less then or equal to 10
What will the following code output?
x = 8y = 12if x < 10 and y > 10: print("Condition met")else: print("Condition not met")
condition met
will this evaluate to True or False: 5>3 and not (6<1)
True
What will the following code print?
x = 21if x % 2 == 0: print("x is even")else: if x % 3 == 0: print("x is magically odd") else: print("x is odd")
x is magically odd
(Challenging) What will the following code output?
a = 4
b = 15
if (a % 2 == 0 or b % 2 == 0) and not (a > 10 or b < 5):
print("Condition 1")
elif a * b > 40 and (a % 3 == 0 or b % 3 == 0):
print("Condition 2")
else:
print("Condition 3")
Condition 1
which list method will add a single element to the end of the list
append()
What will be the output of the following code?
my_string = "Hello"new_string = ""for i, char in enumerate(my_string): if i % 2 == 0: new_string += char.upper() else: new_string += char.lower()print(new_string)
HeLlO
What will be the result of running this code?
numbers = [1, 2, 3, 4]numbers.append([5, 6])print(numbers)
[1, 2, 3, 4, [5, 6]]
Read this code and determine the output of running this code?
my_list = [1, 2, 3]new_list = my_listnew_list[2] = 6print(my_list)
[1, 2, 6]
(Challenging) Read this code carefully and determine the output of running this code?
my_list = [1, 2, 3]new_list = my_listnew_list = [4, 5, 6]print(my_list)
[1, 2, 3]
what is the primary different between a for loop and a while loop
A for loop runs a fixed number of times, while a while loop runs indefinitely until a condition becomes false
which of the following conditions would case a while loop to execute indefinitely
while true with no break statement and while x > 0 where x is never modified
when does the break statement exit a loop
immediately upon encountering the break statement
What happens when the continue statement is executed in a while loop?
The loop continues with the next iteration, skipping the rest of the current iteration.
What is the output when the following code executes?
x = 0
while x < 5:
if x == 2:
break
x += 1
print(x)
2
what will the following code do?
x = 10
while x > 0:
x -= 2
if x % 3 == 0:
continue
print(x)
prints all values of x as it decrease by 2, skipping the values where x is divisible by 3
which Path method can be used to check if a Path object exists as a file or directory in the file system
exists()
Given the following code, what does it return?
from pathlib import Pathp = Path('data.txt')print(p.is_dir())
False if data.txt is a file and true if data.txt is a directory
Given the following code, what will be printed?
from Pathlib import Path
testfile = Path('example.txt')with open(testfile, 'w') as f: f.write('Hello, World!')with open(testfile, 'r') as f: print(f.read())
‘Hello, World!’
which of the following is a correct way to open a file for reading in Python
file = open(‘example.txt’, ‘r’)
which file handle method lets you read all the lines from a file into a list
readlines()
What does the with context manager do when working with files? (Select all correct answers)
it ensures the file is properly closed after its context block of code finishes and it opens the file
what is the correct syntax for defining a function that takes parameters in python
def name(parameters)
what does the return statement do in a function
it stops the function and returns a value
What will the following code output?
def greet():
return "Hello, World!"
greet()
nothing (it outputs nothing)
boolean
expressions either evaluate to True or False
True in boolean
1
Flase in boolean
0
do strings evaluate to true or false in boolean
true
do empty strings and set evaluate to true or false in boolean
False
equality operator
== - checks if the values are the same (true) or different (false)
inequality operator
! =, checks values not equal
assignment operator
=, makes x equal to a value
short circuit evaluation
or- only looks till a true, and- only looks till there is a false
in and not in operators
case sensitive and returns true or false
are == and ! = before or after not, and, or
before
what order do and, not, or evaluate in
not, and, or
nested conditionals
if statement inside an if statement
chained conditionals
elif statements
nested vs chained conditionals which is better
based on readability
mutable
can be changed after it is created, list
immutable
cannot be changed after it is created, strings, tuples
object
something a variable can refer to in memory
reference
when you assign the variable to an object and you create a reference to that object
Id() function
shows the memory location of the variable
is operator
returns true if they have the same memory location and returns false if they dont
aliasing
if two variables refer to the same thing, changing one can change both
cloning
making a copy of a list to not mess up the OG list
slice operator
the best way to copy a list [:]- takes the whole list
.copy()
copies to a new object, same id
.append()
adds the argument to the end of the list
.insert()
adds the second argument in the index indicated by the first argument .insert(1,2) adds 2 in the first spot
.reverse()
reverses the order of the list in place
.sort()
sorts the list items in place (numerical or alphabetical)
.remove()
removes an item at the given index
.pop()
returns and removes the value of the last item in the list
why is this bad code
sorted_list = orginal_list.sort()
it destroys the list as the sort function returns nothing
what is the difference between append and + when talking about lists
append combines elements and + adds the list together [1,2,3[4,5,6]] vs [1,2,3,4,5,6]
.sorted()
creates a copy of the sorted list
why are string methods not mutable
cause strings are not mutable
.split()
returns list of strings based on your parameters
.upper()
all uppercase
.lower()
all lowercase
.strip()
removes the white space (tabs, /n)
.replace()
replaces all occurrences of a substring with a new substring
example of .format()
greeting = 'hello my name is {} and i am a {}'. format(name,job)
print(greetings)
why should you not change the length of a list when iterating over it
can lead to skipping values or an infinite loop
definite iteration
stops at a certain number
indefinite iteration
keeps going ‘forever’ till it reaches a stop (false)
infinite loops
a while loop that will never become false
listener loop
repeat indefinitely till a particular input is given- make sure use input is valid
sentinel value
special value that signals the end of a loop