META BACK-END DEVELOPER PROFESSIONAL CERTIFICATE

Course 2 – Programming in Python

Week 1: Getting Started with Python

Coursera Study Guide

Click to Enroll in Coursera Meta Back-End Professional Certificate

CONTENT

Get started with the Python programming language and associated foundational concepts.

Learning Objectives

  • Recognize common applications of the Python programming language.
  • Explain foundational software engineering concepts.
  • Use operators to program a simple output in Python.
  • Use control flow and loops to solve a problem

KNOWLEDGE CHECK – WELCOME TO PYTHON PROGRAMMING

1. Is a string in Python a sequence?

  • Yes (CORRECT)
  • No

Correct: Correct! A string in Python is a sequence of characters.

2. Does the profile photo display in the right column of the page?

  • # (CORRECT)
  • //

Correct: Correct! The hash symbol is used for adding comments in code.

3. What type will be assigned to the following variable: x = 4?

  • str – String
  • int – Integer (CORRECT)
  • float – Float
  • list – List

Correct: Correct! The variable will be assigned the type Integer.

4. Python allows for both implicit and explicit data type conversions?

  • True (CORRECT)
  • False

Correct: Correct! Python allows for both implicit and explicit data types.

5. A variable called name is assigned the value of “Testing”. What will the output of the following equal – print(len(name));

  • Testing
  • Error
  • str
  • 7 (CORRECT)

Correct: Correct! The output will be 7. The len function will get the length of the string Testing which is equal to 7 characters.

SELF-REVIEW: USE CONTROL FLOW AND LOOPS TO SOLVE A PROBLEM

1. Python for loops work on any type of sequence data type including strings.

  • True (CORRECT)
  • False

Correct: Correct! Python for loops work on any type of sequence data type including strings.

2. The enumerate function is used to provide the index of current iteration of a for loop.

  • True (CORRECT)
  • False

Correct: Correct! An enumerate function is used to provide the index of current iteration of a for loop.

3. A break statement can be used to exit out of a for loop based on a certain condition being satisified.

  • True (CORRECT)
  • False

Correct: Correct! A break statement is used to exit out of a for loop based on a certain condition being satisified.

MODULE QUIZ: GETTING STARTED WITH PYTHON

1. Python is a dynamically typed language. What does this mean?

  • Python requires that you specify the type of variable before it being assigned.
  • Python supports both functional and object oriented programming.
  • Python requires you to explicitly set the correct data type and value before assigning a variable.
  • Python does not require a type for a variable declaration. It automatically assigns the data type at run time. (CORRECT)

Correct: Correct! When variables are declared in Python they are automatically assigned a data type.

2. How do you create a block in Python?

  • A block is created using a colon following by a new line and indentation (CORRECT)
  • A block is created by a new line
  • A block is created using a semi colon and a new line
  • A block is created using a semi colon and indentation

Correct: Correct – A block of code created by using a colon, new line and indentation.

3. When declaring variable in Python, can a variable name contain white space?

  • Yes
  • No (CORRECT)

Correct: Correct! A variable must not contain white space.

4. How can a variable be deleted in python?

  • The remove keyword
  • The del keyword (CORRECT)
  • The def keyword
  • A variable cannot be deleted

Correct: Correct! The del keyword is used to delete a variable by typing del variable name.

5. In Python, how can you convert a number to a string?

  • enumerate()
  • float()
  • int()
  • str() (CORRECT)

Correct: Correct! To convert a number to a string you need to use the str() function. This will change an int like 8 to “8”.

6. An Integer – int in Python can be converted to type Float by using the float function?

  • True (CORRECT)
  • False

Correct: Correct! The float function can be used to covert a type int to a float for better precision.

7. What is the purpose of break in a for loop in Python?

  • The break keywork is used to debug a for loop.
  • It controls the flow of the loop and stops the current loop from executing any further. (CORRECT)
  • The break statement will suspend the code until continue is run.
  • To terminate the code

Correct: Correct! The break keyword will stop the loop from executing and transfer the control to the next block of code.

8. An enumerate function is used to provide the index of the current iteration of a for loop.

  • True (CORRECT)
  • False

Correct: Correct! An enumerate function is used to provide the index of current iteration of a for loop.

9. What will be the output of the code below:

a = isinstance(str, "aa")
print(a)  
  • It will throw an error. (CORRECT)
  •  “aa”
  • False 
  • True

Correct: Correct ! It will throw a TypeError as the correct format here must be

 isinstance("aa", str)  

10. Select all the valid input() formats among the following.

Select all that apply 

  •  “” = input(“My name is: ” + name) 
  •  input()  (CORRECT)
  • name = input(“What is your name? “)  (CORRECT)
  • input(“”)  (CORRECT)

Correct: Correct! The input() can work even without assignment to some variable. 

Correct: Correct! This is the standard format for using input() 

Correct: Correct! The input() can work even without assignment to some variable and an empty prompt 

11. What is programming? 

  • A set of instructions that a computer uses to perform a specific function. (CORRECT)
  • A set of questions to ask the end-user.
  • A sequence of decimal numbers.

Correct: You are correct! Programming is a set of instructions in a language the computer understands, to perform a specific function.

12. In which year was Python released? Select the correct answer.

  • 1991 (CORRECT)
  • 2001
  • 2011
  • 2021

Correct: You are correct! Python was released in 1991.

13. True or false: You must select the most recent version of Python to ensure that it works correctly on your operating system and with your chosen Integrated Development Environment (IDE).

  • No
  • Yes (CORRECT)

Correct: That’s correct! While the recommended interpreter is usually the correct one it’s important that you double-check that it’s the latest version of Python that you have installed on your operating system.

14. When you start using Python it’s crucial to ensure that it works correctly on your operating system and with your chosen Integrated Development Environment (IDE). To do this you must select the most recent version of Python as the interpreter. True or false?

  • True (CORRECT)
  • False

Correct: That’s correct! While the recommended interpreter is usually the correct one it’s important that you double-check that it’s the latest version of Python that you have installed on your operating system.

15. Why is it better to run Python in Visual Studio Code compared to running it in the Python shell or in the command line or terminal? Check all that apply.

  • VS Code is especially useful for running and testing small scripts.
  • VS Code has whitespace and indentation helpers. (CORRECT)
  • VS Code has a code syntax and highlighting feature. (CORRECT)
  • VS Code has an auto-completion feature. (CORRECT)
  • VS Code has a debugging feature. (CORRECT)

Correct: That’s right. Other VS Code features include an auto-completion feature, code syntax and highlighting and debugging.

Correct: That’s right. Other VS Code features include debugging, an auto-completion feature, and whitespace and indentation helpers.

Correct: That’s right. Other VS Code features include debugging, code syntax and highlighting and whitespace and indentation helpers.

Correct: That’s right. Other VS Code features include an auto-completion feature, code syntax and highlighting and whitespace and indentation helpers.

16. While programming in Python, a colleague approaches you and asks you if the following line of Python code is correct:

x      =      1      +      5

What would your answer be?

  • Yes (CORRECT)
  • No

Correct: That’s correct! Any amount of whitespace or indentations on a line is fine, but keep in mind that if you are combining it with additional lines, then you will need to give clear indicators of where a new line has occurred.

17. Like with any programming language, the naming convention you choose for variables is important when using Python. Why is this so? Choose all that apply.

  • When working together with other developers it can become difficult to know what a variable refers to unless it has a meaningful label. (CORRECT)
  • A generic label gives no information about a variable or the context in which it’s used. (CORRECT)
  • It can be challenging to remember why you used a specific name for a variable when you have to interact with your code months after it was initially written. (CORRECT)

Correct: That’s right! Meaningful names can help other developers to avoid guesswork and understand variable references more readily.

Correct: That’s right! Generic labels mean that other developers have to check the associated values manually, which wastes time.

Correct: That’s right! Descriptive names can help you to recall variable references quickly.

18. Which of the following are sequence data types? Check all that apply.

  • Float
  • Integer
  • String (CORRECT)
  • List (CORRECT)
  • Tuples (CORRECT)

Correct: Strings, lists, and tuples are all sequence data types.  Floats and integers are numeric data types.

Correct: Strings, lists, and tuples are all sequence data types.  Floats and integers are numeric data types.

Correct: Strings, lists, and tuples are all sequence data types.  Floats and integers are numeric data types.

19. You have the following variables with string values:

a = “my favorite food is “

b = “mashed potatoes”

You then print these strings together with this line:

print(a + b)

What is this an example of?

  • Multi-line string
  • Concatenation (CORRECT)
  • Array
  • Single-line string

Correct: That’s right! Concatenation is the joining of two separate strings.

20. A company wants to determine the general age of its customers. You help them by writing a program that uses a customer’s ID number (7512171283423) to determine his or her age. But the data set that you are provided with has the ID numbers saved as strings. What typecasting function can you use to solve the problem?

  • float(7512171283423)
  • str(7512171283423)
  • int(‘7512171283423’) (CORRECT)
  • set(7512171283423)

Correct: You are correct! You will use the integer function as it will output whole numbers with no decimal places.

21. Python’s print function can output:

  • Only string values.
  • Values of all data types. (CORRECT)
  • Only integer values

Correct: You are correct!. Python’s print function can output values of all data types.

22. Which of the following are examples of logical operators?  Check all that apply.

  • subtractions
  • multiplication
  • and (CORRECT)
  • or (CORRECT)
  • not (CORRECT)

Correct: That’s correct, And, OR and NOT are all logical operators.

23. Which of the following are examples of conditional flow controls?  Check all that apply.

  • while loop
  • for loop
  • if (CORRECT)
  • else (CORRECT)
  • elif (else if) (CORRECT)

Correct: Yes! The If, else and elif (else if) statements are conditional flow controls.

24. A match statement compares a value to several different conditions until one is met. True or false?

  • True (CORRECT)
  • False

Correct: That’s right! A match statement compares a value to several different conditions until one is met.

25. Looping is used to iterate through the sequence and access each item inside the sequence. True or false?

  • True (CORRECT)
  • False

Correct: That’s right! Looping is used to iterate through the sequence and access each item inside the sequence.

26. True or false: The inner loop performs the first iteration during the nested loop process.

  • False (CORRECT)
  • True

Correct: Yes, it’s the outer loop that performs the first iteration during the nested loop process.

Subscribe to our site

Get new content delivered directly to your inbox.

Liking our content? Then, don’t forget to ad us to your BOOKMARKS so you can find us easily!