COURSE 6 – PYTHON FOR DATA SCIENCE, AI & DEVELOPMENT

Module 3: Python Programming Fundamentals

IBM APPLIED AI PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

Last updated:

INTRODUCTION – Python Programming Fundamentals

This module covers the fundamentals of Python, beginning with the concepts of conditions and branching. As you progress, you will learn how to implement loops to iterate over sequences, create functions to perform specific tasks, and handle exceptions to catch errors. Additionally, the module explains the importance of classes in creating objects, providing a comprehensive foundation in essential Python programming techniques.

Learning Objectives

  • Classify conditions and branching by identifying structured scenarios with outputs.
  • Work with objects and classes.
  • Explain objects and classes by identifying data types and creating a class.
  • Use exception handling in Python.
  • Explain what functions do.
  • Build a function using inputs and outputs.
  • Explain how for loops and while loops work.
  • Work with condition statements in Python, including operators and branching.
  • Create and use loop 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,an or statement is only False if all the Boolean values 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, when you call the function the second time the value of c is reassigned.

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, 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
  • 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

CONCLUSION – Python Programming Fundamentals

In conclusion, this module provides a comprehensive foundation in essential Python programming techniques. You will begin with the concepts of conditions and branching, followed by mastering the implementation of loops to iterate over sequences.

You will also learn to create functions for specific tasks, perform exception handling to catch errors, and understand the importance of classes for creating objects. By the end of this module, you will be well-equipped with the fundamental skills necessary for effective Python programming.