COURSE 2: GET STARTED WITH PYTHON

Spread the love

Module 2: Functions and Conditional Statements

INTRODUCTION – Functions and Conditional Statements

This section will help you to work with functions to carry out various useful activities on data. It will also tutor you on writing conditional statements that will let a program take action when given particular specifiedted commands. Practice also writing code that is clear, efficient, and accessible for possible reuse by other data professionals:

Learning Objectives

  • Understand the purpose and logic of conditional statements: Learn how to use if, else, and elif to guide decision-making in your programs.
  • Use Comparators and Logical Operators: Compare values using operators such as ==, !=, <, >, <=, >= and combine conditions with logical operators such as and, or, and not.
  • Benefits of commenting code: Why writing clear, concise comments in your code becomes very important to understand and allow others to collaborate on it.
  • Best practices in writing clean code: To show why writing reusable, modular, and maintainable codes through techniques such as refactoring is advocated.
  • Defining Functions in Python: learn how to define functions using the def keyword, return values by making use of the ‘return’ keyword, and organize your code into reusable blocks.
  • Comparative and logical operators: Procedures for using comparison operators and logical operators to evaluate expressions and make decisions.
  • Write comments and break the code into blocks: This simplifies your code via comment sections.
  • Refactor code for readability: You will use refactoring techniques to enhance the readability and reusability of your code.
  • Define and call functions with parameters: You shall learn how to pass arguments to the functions and return values to make your code flexible.
  • Conversions between data types: Learn to convert among the various data types and manage different data with Python built-in functions.
  • Variables, expressions, and operators: Growlin.

PRACTICE QUIZ: TEST YOUR KNOWLEDGE: FUNCTIONS

1. A data professional wants to define their own Python function. What keyword should they use at the start of the function block?

  • Return
  • for
  • def (CORRECT)
  • with

Correct: In order to define a function, it begins with the keyword def followed by the function block.

2. Modularity is the ability to write code in separate components that work together.

  • True (CORRECT)
  • False

Correct: The joining of codes in separate modules that can work harmoniously without any hitch is nothing but Modularity. This makes the reusability of other programs, easy to modify and very exciting for the data professional because it helps in easy programming as well as better organization of the code.

3. Why do data professionals use comments for their Python code? Select all that apply.

  • To execute code
  • To provide helpful explanations (CORRECT)
  • To outline the steps of a process (CORRECT)
  • To document their work for teammates (CORRECT)

Correct: Data annotator gives comments in Python functions from the point of describing the uses of functions and processes carried out well to documenting what they did for their teammates. It helps code readability so that others can understand it better and thus collaborate with the one who wrote it. It can also act for future reference reminders for improvements or further clarifications.

PRACTICE QUIZ: Conditional Statements

1. Fill in the blank: Comparators are operators that compare two values and produce _____ values.

  • string
  • boolean (CORRECT)
  • float
  • integer

Correct: Comparators are operators utilized to perform a value comparison for two objects with a Boolean result. A Boolean is understood as a data type with only two values, True or False.

2. When comparing two expressions, what logical operator requires both expressions to be true to return a True result?

  • with
  • and (CORRECT)
  • not
  • or

Correct: The and logical operator will evaluate to True if both the expressions are True, when it is used for the comparison of two expressions. One of the purposes of using logical operators is to link several statements together in order to create more complex comparisons and decisions within a code.

3. Which of the following is a reserved keyword that sets up a condition in Python?

  • As
  • else
  • with
  • if (CORRECT)

Correct: By the new definition, if is a reserved key word in Python that defines a conditional statement. It gives way to block execution of a portion of a program if the condition is True.

4. The elif keyword only works when there are exactly two possible conditions in the code.

  • True
  • False (CORRECT)

Correct: In Python, the elif keyword is used to check several conditions in the code. So many numbers of cases can be compared for checking. Rather, after all, in such situations, the programming would be much more flexible and manageable for different kinds of periostitic.

QUIZ: MODULE 2 CHALLENGE

1. A data professional wants to define a function to calculate the volume of a box. What code should they begin with?

  • return volume_box(length, width, height):
  • if volume_box(length, width, height):
  • else volume_box(length, width, height):
  • def volume_box(length, width, height): (CORRECT)

Correct!

2. Which Python keyword makes a function produce new results and save the results for later use?

  • def
  • if
  • return (CORRECT)
  • as

Correct!

3. What are best practices for writing clean code? Select all that apply.

  • Redundancy
  • Modularity (CORRECT)
  • Reusability (CORRECT)
  • Clarity (CORRECT)

Correct!

4. Fill in the blank: Refactoring is used to create _____, which is code that is written in a way that is readable and makes its purpose clear.

  • branching code
  • self-documenting code (CORRECT)
  • open source code
  • immutable code

Correct!

5. Fill in the blank: A data professional can add a _____ to the beginning of a function’s body to summarize the function’s behavior and explain its arguments and return values.

  • docstring (CORRECT)
  • logical operator
  • comparator
  • conditional statement

Correct!

6. What is the Python comparator for not equal to?

  • <=
  • >=
  • != (CORRECT)
  • ==

Correct!

7. A data professional writes the following code: print(33 > 12 or 9 < 7). What result will Python display when they run the code?

  • True (CORRECT)
  • Equal
  • False
  • Not equal

Correct!

8. Fill in the blank: In Python, the if statement branches the execution based on a specific condition being _____.

  • True (CORRECT)
  • false
  • equal
  • not equal

Correct!

9. Fill in the blank: In Python, else is a reserved keyword that executes when preceding conditions evaluate as _____.

  • true
  • not equal
  • false (CORRECT)
  • equal

Correct!

10. A data professional wants to define a function to calculate the square of a number. What code should they begin with?

  • return find_square(n):
  • def find_square(n): (CORRECT)
  • if find_square(n):
  • else find_square(n):

Correct!

11. A data professional wants to make a Python function produce new results and save the results for later use. What keyword should they use in their code?

  • and
  • if
  • else
  • return (CORRECT)

Correct!

12. What is the term for code that is written to be readable and to make its purpose clear?

  • Open source
  • Branching
  • Immutable
  • Self-documenting (CORRECT)

Correct!

13. A data professional adds a docstring to the beginning of a function’s body. What is the purpose of the docstring?

  • To define the function
  • To make the function produce new results and save the results for later use
  • To summarize the function’s behavior and explain its arguments and return values (CORRECT)
  • To run the function and print the results

Correct!

14. What is the Python comparator for equal to?

  • == (CORRECT)
  • <=
  • >=
  • !=

Correct!

15. A data professional writes the following code: print(not 4 == ‘Data’). What result will Python display when they run the code?

  • Not equal
  • Equal
  • True (CORRECT)
  • False

Correct!

16. In Python, when does an if statement execute a piece of code?

  • When the condition evaluates to not equal
  • When the condition evaluates to false
  • When the condition evaluates to equal
  • When the condition evaluates to true (CORRECT)

Correct!

17. In Python, when does an else statement execute a piece of code?

  • When the if statement contains a false condition (CORRECT)
  • When the if statement contains text data
  • When the if statement contains a true condition
  • When the if statement contains numeric data

Correct!

18. Fill in the blank: A data professional can use the _____ keyword to make a Python function produce new results and save the results for later use.

  • if
  • return (CORRECT)
  • def
  • else

Correct!

19. Fill in the blank: To reduce errors and enhance teamwork, data professionals employ clean code best practices such as _____ and modularity.

  • branching
  • redundancy
  • immutability
  • reusability (CORRECT)

Correct!

20. In Python, what is the process of restructuring code while maintaining its original functionality?

  • Refactoring (CORRECT)
  • Converting
  • Reprogramming
  • Branching

Correct!

21. A data professional wants to summarize a function’s behavior and explain its arguments and return values. What should they add to the beginning of the function’s body?

  • An algorithm
  • A docstring (CORRECT)
  • A comparator
  • A logical operator

Correct!

22. A data professional writes the following code: print(23 > 32 and 7 != 5). What result will Python display when they run the code?

  • True
  • Not equal
  • False (CORRECT)
  • Equal

Correct!

23. Fill in the blank: In Python, the _____ statement branches the execution based on a specific condition being true.

  • then
  • else
  • if (CORRECT)
  • elif

Correct!

24. Fill in the blank: A _____ is a body of reusable code for performing specific processes or tasks.

  • variable
  • data type
  • keyword
  • function (CORRECT)

Correct: A function is a body of reusable code for performing specific processes or tasks.

25. Which Python feature enables data professionals to define code once, then use it many times without having to rewrite it?

  • Dynamic typing
  • Reprogramming
  • Conversion
  • Reusability (CORRECT)

Correct: Python’s reusability feature enables data professionals to define code once, then use it many times without having to rewrite it.

26. Fill in the blank: Lines of code that begin with a _____ serve as comments and don’t get executed.

  • Hashtag (CORRECT)
  • Ampersand
  • parenthesis
  • question mark

Correct: Lines of code that begin with a hashtag serve as comments and don’t get executed.

27. Which of the following words are examples of logical operators? Select all that apply.

  • Refactoring
  • Comparing
  • Commenting
  • Branching (CORRECT)

Correct: In computing, branching means an ability of a certain program which is capable of changing its course of action in compliance with certain conditions and letting it travel on a different path in accordance with the specific input or situation.

28. Fill in the blank: _____ describes the ability of a program to alter its execution sequence.

  • Refactoring
  • Comparing
  • Commenting
  • Branching (CORRECT)

Correct: Branching is a specific characteristic of a program to modify its execution of instructions depending on certain parameters that cause the modification to different outcomes or actions based on different scenarios.

29. A data professional wants to define a function to calculate the area of a rectangle. What code should they begin with?

  • return area_rectangle(length, width):
  • def area_rectangle(length, width): (CORRECT)
  • else area_rectangle(length, width):
  • if area_rectangle(length, width):

Correct!

30. What is the Python comparator for less than or equal to?

  • >=
  • <= (CORRECT)
  • ==
  • !=

Correct!

31. Fill in the blank: In Python, the _____ statement sets a piece of code to run only when the condition of the if statement is false.

  • else (CORRECT)
  • if
  • then
  • return

Correct!

32. What are the benefits of using clean code for data work? Select all that apply.

  • Eliminate the need for hashtags when commenting
  • Reduce errors (CORRECT)
  • Enhance teamwork (CORRECT)
  • Save time and effort (CORRECT)

Correct!

Leave a Comment