Module 3: Python Programming Fundamentals

Spread the love

INTRODUCTION – Python Programming Fundamentals

Here Python’s origins commence; with this module the grade of conditionality and branching is being acquired-the incorporation of iterations in looping over sequences, the implementation of functions for specific tasks, and the handling of exceptions in catching faults would be other advanced learning objectives. Besides the significant role that this module plays in introducing classes for object creation, it also provides sound bases on the essential competencies of learning Python.

Learning Objectives:

  • Classify conditions and branching by recognizing structured scenarios with different outputs.
  • Work with objects and classes.
  • Understand and explain objects and classes by identifying data types and creating a class.
  • Exception handling is used in Python to manage errors.
  • Understand the objective of functions and how to construct them through input and output.
  • Study how for loops and while loops work.
  • Work with condition statements in Python such as operators and branching logic.
  • Develop and work with loop statements in Python for performing iterative tasks.

PRACTICE QUIZ 1

1. What is the result of the following: 1=2

  • True
  • SyntaxError:can’t assign to literal (CORRECT)
  • False 

correct, Kindly state the sentence that has the syntactical error and I can assist in rewriting it to correct the 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,Only when all Boolean values are false is an OR statement false.

PRACTICE QUIZ 2

1. What will be the output of the following:

for x in range(0,3):     

print(x)

  • 0
  • 1
  • (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]) ?

  • (CORRECT)
  • 2
  • 4

Correct, The function will give 3 as output because the elements in the list include 3 individual numbers.

2. What does the following function return: len([sum([0,0,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, The list returned by the sorted function is not a sorted-up version of the original 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, The list returned by the sorted function is not a sorted-up version of the original list L.

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 strings.

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
  • (CORRECT)
  • ‘red’

Correct, This piece of code is called RedCircle.radius=1 which is set to modify 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
  • (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))

  • (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, the value of a=100 exists in the local scope of the function. Therefore the value of a=1 in the global scope is not used.

15. What is the output of the following few lines of code?

x=5

while(x!=2):

  print(x)

  x=x-1

  • 5
  • 4
  • (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

CONCLUSION – Python Programming Fundamentals

This module acts as an introduction to many important technicalities in the Python programming paradigm. You first look at conditions and branching, before moving on to the more complex activity of mastering how to loop: iterating over sequences.

In addition, you will know how to create functions to handle specific jobs, deal with exceptions to catch errors, and the relevance of the classes one to create an object from. Thus, on the successful completion of this module, you will begin to understand some of the fundamental skills required to achieve the proficient programming in Python.

Leave a Comment