Disjointed Courses Starts from Programming with Python and goes into conditions and branching and then proceeds on to Looping to iterate the sequence and create the functions to perform specific jobs, handling exceptions for error management and finally coming to class as being the major backbone in object creation giving the students a stronghold in the basics of required technique with Python.
Learning Outcomes:
Define and classify conditions and branching situations with structured output.
Work with and identify objects and classes.
Explain the theory of objects and classes, identify the types of data, and form a class.
Implement Exception Handling in Python.
Understand Reason for a Function and Its Working Nature.
Create functions by input-output.
Explain and Implement for&while loops.
Condition statements are such that they include operators and branching.
Create and efficiently use looping statements in Python.
PRACTICE QUIZ 1
1. what is the result of the following: 1=2
True
SyntaxError:can’t assign to literal (CORRECT)
False
correct, this statement is a syntax error
2. What is the output of the following code segment:
i=6
i<5
True
False (CORRECT)
SyntaxError: can’t assign to literal
3. What is the result of the following: 5!=5
False (CORRECT)
True
correct, this is the inequality operator
4. What is the output of the following code segment: ‘a’==’A’
False (CORRECT)
True
correct, the equality operator is case sensitive
5. in the video, if age=18 what would be the result
move on (CORRECT)
you can enter
6. in the video what would be the result if we set the variable age as follows: age= -10
go see Meat Loaf
move on (CORRECT)
you can enter
move on
7. what is the result of the following: True or False
True, an or statement is only False if all the Boolean values are False (CORRECT)
False
correct,Mark it as Yes . In Boolean semantics, an OR statement is true when one of the values is true. It is only false when all the Boolean values of that statement are false.
PRACTICE QUIZ 2
1. what will be the output of the following:
for x in range(0,3):
print(x)
0
1
2 (CORRECT)
0
1
2
3
2. what is the output of the following:
for x in [‘A’,’B’,’C’]:
print(x+’A’)
AA
BA
CA (CORRECT)
A
B
C
3. what is the output of the following:
for i,x in enumerate([‘A’,’B’,’C’]):
print(i,x)
0 A
1 B
2 C (CORRECT)
AA
BB
CC
PRACTICE QUIZ 3
1. What does the following function return: len([‘A’,’B’,1]) ?
3 (CORRECT)
2
4
Correct, the function returns the number of elements in the list, in this case 3.
2. What does the following function return: len([sum([0,0,1])]) ?
1 (CORRECT)
0
3
3. What is the value of list L after the following code segment is run :
L=[1,3,2]
sorted(L)
L:[1,3,2] (CORRECT)
L:[1,2,3]
L:[0,0,0]
Correct, sorted is a function and returns a new list, it does not change the list L
4. From the video what is the value of c after the following:
c=add1(2)
c=add1(10)
3
11 (CORRECT)
14
Correct, Normally, when a function in Python (and most programming languages) is called, the values of its local variables, like c, are refreshed every time the function is invoked.
5. What is the output of the following lines of code:
def Print(A):
for a in A:
print(a+’1′)
Print([‘a’,’b’,’c’])
a
b
c
a1
b1
c1 (CORRECT)
a1
correct, the function concatenates a string
PRACTICE QUIZ 4
1. Why do we use exception handlers?
Terminate a program
Write a file
Read a file
Catch errors within a program (CORRECT)
2. What is the purpose of a try…except statement?
Crash a program when errors occur
Catch and handle exceptions when an error occurs (CORRECT)
Executes the code block only if a certain condition exists
Only executes if one condition is true
PRACTICE QUIZ 5
1. What is the type of the following?
[“a”]
str
list (CORRECT)
Correct, the “a” is surrounded by brackets so it is a list.
2. What does a method do to an object?
Changes or interacts with the object (CORRECT)
Returns a new values
3. We create the object:
Circle(3,’blue’)
What is the color attribute set to?
2
‘blue’ (CORRECT)
4. What is the radius attribute after the following code block is run?
RedCircle=Circle(10,’red’)
RedCircle.radius=1
10
1 (CORRECT)
‘red’
Correct, the line of code RedCircle.radius=1 will change the radius.
5. What is the radius attribute after the following code block is run?
BlueCircle=Circle(10,’blue’)
BlueCircle.add_radius(20)
10
20
30 (CORRECT)
MODULE 3 GRADED QUIZ
1. What is the output of the following code?
x=”Go”
if(x==”Go”):
print(‘Go ‘)
else:
print(‘Stop’)
print(‘Mike’)
Go Mike (CORRECT)
Mike
Stop Mike
2. What is the result of the following lines of code?
x=1
x>5
True
False (CORRECT)
3. What is the output of the following few lines of code?
x=0
while(x<2):
print(x)
x=x+1
0
1 (CORRECT)
0
1
2
0
1
3
4
4. What is the result of running the following lines of code ?
class Points(object):
def __init__(self,x,y):
self.x=x
self.y=y
def print_point(self):
print(‘x=’,self.x,’ y=’,self.y)
p1=Points(1,2)
p1.print_point()
x=1;
x=1 y=2 (CORRECT)
y=2
5. What is the output of the following few lines of code?
for i,x in enumerate([‘A’,’B’,’C’]):
print(i,2*x)
0 AA
1 BB
2 CC (CORRECT)
0 A
1 B
2 C
0 A
2 B
4 C
6. What is the result of running the following lines of code ?
class Points(object):
def __init__(self,x,y):
self.x=x
self.y=y
def print_point(self):
print(‘x=’,self.x,’ y=’,self.y)
p2=Points(1,2)
p2.x=’A’
p2.print_point()
x= 1 y=2
x= A y=2 (CORRECT)
x=A, y=B
7. Consider the function delta, when will the function return a value of 1?
def delta(x):
if x==0:
y=1
else:
y=0
return(y)
When the input is anything but 0
When the input is 1 (CORRECT)
Never
When the input is 0
8. What is the output of the following lines of code?
a=1
def do(x):
return(x+a)
print(do(1))
2 (CORRECT)
1
NameError: name ‘a’ is not defined
correct, the value of a in the global scope will be used
9. Write a function name add that takes two parameter a and b, then return the output of a + b (Do not use any other variable! You do not need to run it. Only write the code about how you define it.)
Answer:
def add(a,b):
return(a+b)
10. Why is it best practice to have multiple except statements with each type of error labeled correctly?
Ensure the error is caught so the program will terminate
In order to know what type of error was thrown and the location within the program (CORRECT)
To skip over certain blocks of code during execution
It is not necessary to label errors
11. What is the result of the following lines of code?
x=1
x>-5
True (CORRECT)
False
12. What is the result of running the following lines of code ?
class Points(object):
def __init__(self,x,y):
self.x=x
self.y=y
def print_point(self):
print(‘x=’,self.x,’ y=’,self.y)
p1=Points(“A”,”B”)
p1.print_point()
x= A
y= B
x= A y= B (CORRECT)
13. What is the output of the following few lines of code?
for i,x in enumerate([‘A’,’B’,’C’]):
print(i+1,x)
1 A
2 B
3 C (CORRECT)
0 A
1 B
2 C
0 AA
1 BB
2 CC
14. What is the output of the following lines of code?
a=1
def do(x):
a=100
return(x+a)
print(do(1))
2
101 (CORRECT)
102
Correct, Yes, you are right! In programming, if a variable gets a value within a function, then it’s usually available in the local scope of that function-the local scope means that it is only valid within that function.
15. What is the output of the following few lines of code?
x=5
while(x!=2):
print(x)
x=x-1
5
4
3 (CORRECT)
5
4
3
2
the program will never leave the loop
16. What is the result of running the following lines of code ?
class Points(object):
def __init__(self,x,y):
self.x=x
self.y=y
def print_point(self):
print(‘x=’,self.x,’ y=’,self.y)
x=2 y=2 (CORRECT)
x=1 y=2
x=1 y=1
17. Consider the function step, when will the function return a value of 1?
def step(x):
if x>0:
y=1
else:
y=0
return y
if x is larger than 0 (CORRECT)
if x is equal to or less then zero
if x is less than zero
correct, the value of y is 1 only if x is larger than 0
3. What do symmetric encryption algorithms use to encrypt and decrypt information?
A single secret key (CORRECT)
A hash value
A public and private key pair
A digital certificate
CONCLUSION – Python Programming Fundamentals
This module, indeed, serves as a powerful building block in the primary skills relating to Python programming language. First, you shall understand the various conditions and branches, which could make your program run according to the different conditions applied to it. Although what you would do next is to find out about loops that will usually repeat an action and iterate through various sequences in the process: that’s how easy it can be.
Moreover, functions will be comprehended because you will modularize the codes for certain activities. Exception handling entails teaching how to manage errors in a better way to prevent the program’s failure in other occurrences. Furthermore, you will have an excellent knowledge concerning classes, which are of great importance while constructing and managing objects in object-oriented programming.
In a nutshell, after taking this module, you would acquire essential skills required to write a clear, efficient, and organized Python code-what is a little gateway into more advanced programming concepts on the way.