COURSE 6 – PYTHON FOR DATA SCIENCE, AI & DEVELOPMENT

Module 1: Python Basics

IBM AI DEVELOPER PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

Last updated:

INTRODUCTION – Python Basics

This module provides a comprehensive introduction to the basics of Python programming. It begins with an in-depth exploration of various data types, such as integers, real numbers, and strings. As you advance through the module, you will learn how to effectively use expressions to perform mathematical operations and how to store values in variables.

Additionally, the module covers the numerous techniques available for string manipulation, ensuring you gain a solid understanding of how to handle and process text data efficiently. By the end of this module, you will have a strong foundation in Python and be well-equipped to tackle more complex programming concepts.

Learning Objectives

  • Demonstrate an understanding of types in Python by converting or casting data types such as strings, floats, and integers.
  • Interpret variables and solve expressions by applying mathematical operations.
  • Describe how to manipulate strings by using a variety of methods and operations.
  • Build a program in JupyterLab to demonstrate your knowledge of types, expressions, and variables.
  • Work with, manipulate, and perform operations on strings in Python.

PRACTICE QUIZ

1. What is the type of the following: 0 

  • float
  • int (CORRECT)

Correct, as there is no decimal the number is of type int. You can also use the type function to verify your results.

2. What is the type of the following number: 3.12323 

  • float (CORRECT)
  • int

Correct, as there is a decimal the number is of type float. You can also use the type function to verify your results.

3. What is the result of the following: int(3.99) 

  • 3 (CORRECT)
  • 3.99

Correct. In Python, if you cast a float to an integer, the conversion truncates towards zero, i.e.you just get rid of the numbers after the decimal place. So for 3.99 you just get rid of the “.99” leaving 3.

PRACTICE QUIZ

1. What is the result of the following operation:  11//2

  • 5.5
  • 5 (CORRECT)

this is correct, the symbol // means integer value. Therefore you must round the result down.

2. what is the value of x after the following is run:

x=4

x=x/2 

  • 2.0 (CORRECT)
  • 4.0

Correct: the value x=x/2 changes the value of x, if x is assigned to its self. It’s helpful to replace the value of x with its current value in this case 4 or x=4/2. We can also see that the result is a float.

PRACTICE QUIZ

1. What is the result of the following: Name[0]

riosD1T1KyLtY NaOLaD9nBBnguUqWbwKyc irWnsb7ax6xk1KSzuhNi5EXhPSeJTKupfPcoNSICreXFw5mglJgBUZFuI4ocl0l3uiS 5v STcIIIAp

  • “M” (CORRECT)
  • “i”
  • “n”

correct, The index 0 corresponds to the first index

2. What is the result of the following: Name[-1]

riosD1T1KyLtY NaOLaD9nBBnguUqWbwKyc irWnsb7ax6xk1KSzuhNi5EXhPSeJTKupfPcoNSICreXFw5mglJgBUZFuI4ocl0l3uiS 5v STcIIIAp

  • “M” (CORRECT)
  • “i”
  • “n”

correct, The index 0 corresponds to the first index

3. What is the output of the following: print(“AB\nC\nDE”)

  • ABC
  • DE
  • AB
  • CD
  • E
  • AB
  • C
  • DE (CORRECT)

correct when the print function encounters a \n it displays a new line 

4. What is the result of following? 

“hello Mike”.find(“Mike”) 

If you are unsure, copy and paste the code into Jupyter Notebook and check.

  • 6,7,8
  • 5
  • 6 (CORRECT)

correct, the method finds the starting index of a substring

MODULE 1 GRADED QUIZ

1. What is the value of x after the following lines of code?

  • x=2
  • x=x+2
  • 4 (CORRECT)
  • 2

Correct: the value x=x+2 changes the value of x, if x is assigned to its self. It’s helpful to replace the value of x with its current value in this case 2 or x=2+2.

2. What is the result of the following operation 3+2*2 ?

  • 3
  • 7 (CORRECT)
  • 9

Correct, Python follows the standard mathematical conventions

3. What is the result of the following code segment: type(int(12.3))

  • str
  • float
  • int (CORRECT)

correct, in this code, we first cast or convert the float to an integer, then use the type function to determine the type

4. What is the result of the following code segment: int(True)

  • 1 (CORRECT)
  • 0
  • error

correct, when you cast a boolean True to an integer you get a 1

5. In Python, what is the result of the following operation: ‘1’+’2′ ?

  • 3
  • ‘3’
  • ’12’ (CORRECT)

correct, the ‘+’ applied to strings does not add strings but concatenates them 

6. Given myvar = ‘hello’ , how would you return myvar as uppercase?

  • len(myvar)
  • myvar.find(‘hello’)
  • myvar.upper() (CORRECT)

7. What is the result of the following : str(1)+str(1) ?

  • ’11’ (CORRECT)
  • 2

correct, the integers are cast to a string, and the strings are concatenated 

8. What is the result of the following: “ABC”.replace(“AB”, “ab”) ?

  • ‘abC’  (CORRECT)
  • ‘ABc’

correct, the method replace returns a copy of the string with all occurrences of the old substring 

9. In Python 3, what is the type of the variable x after the following: x=1/1 ?

  • float (CORRECT)
  • int

correct, in Python 3, regular division always results in a float

10. What is the value of x after the following lines of code?

x=1

x=x+1

  • 1
  • 2 (CORRECT)
  • 4

Correct: the value x=x+1 changes the value of x, if x is assigned to its self. It’s helpful to replace the value of x with its current value in this case 1 or x=1+1.

11. What is the result of the following code segment: int(False)

  • 1
  • 0 (CORRECT)
  • error

correct, when you cast a boolean False to an integer you get a 0

12. What is the result of the following: ‘hello’.upper() ?

  • ‘HELLO’ (CORRECT)
  • ‘Hello’
  • ‘hello’

correct, upper returns a copy of the string in which all case-based characters have been converted to uppercase.

13. What is the result of the following operation 1+3*2 ?

  • 7 (CORRECT)
  • 12
  • 8

Correct, Python follows the standard mathematical conventions

14. What is the result of the following : str(1+1) ?

  • ‘2’ (CORRECT)
  • ’11’

correct, the argument is first evaluated  1+1=2, then the result is cast to a string.

15. What is the result of the following: “123”.replace(“12”, “ab”) ?

  • ‘ab3’ (CORRECT)
  • ‘123ab’

correct, the method replace returns a copy of the string with all occurrences of the old substring 

16. What is the type of the following “7.1”

  • float
  • string (CORRECT)

correct, the type is string

17. In Python 3, what is the type of the variable x after the following: x=2/2 ?

  • float (CORRECT)
  • int

correct, in Python 3, regular division always results in a float

CONCLUSION – Python Basics

By the end of this module, you will have a solid foundation in Python programming. You’ll be familiar with essential data types such as integers, real numbers, and strings, and proficient in using expressions for mathematical operations and variable storage.

Moreover, you’ll gain expertise in various string manipulation techniques, enabling you to handle and process text data effectively. With these skills, you will be well-prepared to delve into more advanced programming topics and apply your knowledge to a wide range of coding challenges.