Podcast
Questions and Answers
Given the string s = 'programming'
, what would s[3:7]
return?
Given the string s = 'programming'
, what would s[3:7]
return?
- 'og'
- 'ogra'
- 'gram'
- 'gramm'
If str1 = 'Hello World!'
, what will str1.find('o')
return?
If str1 = 'Hello World!'
, what will str1.find('o')
return?
- 5
- 4
- 0
- 7
What is the primary benefit of using functions to break down a program?
What is the primary benefit of using functions to break down a program?
- It makes the code run faster.
- It makes the code easier to read and test.
- It allows the program to interface directly with the operating system.
- It reduces the memory footprint of the program.
What does the return
statement do in a function?
What does the return
statement do in a function?
What happens if you try to access a string index that is out of range?
What happens if you try to access a string index that is out of range?
What is the difference between a void function and a value-returning function?
What is the difference between a void function and a value-returning function?
Which of the following function calls will correctly capitalize the string 'hello world' into Title Case?
Which of the following function calls will correctly capitalize the string 'hello world' into Title Case?
What will be the output of the following code?
def my_function(x):
x = x + 10
print(x)
x = 5
my_function(x)
print(x)
What will be the output of the following code?
def my_function(x):
x = x + 10
print(x)
x = 5
my_function(x)
print(x)
What is the term for a variable that is only accessible within the function it is defined in?
What is the term for a variable that is only accessible within the function it is defined in?
What does string slicing accomplish?
What does string slicing accomplish?
Given the definition str1 = 'example'
, determine the proper use of a string function to return the length of the string.
Given the definition str1 = 'example'
, determine the proper use of a string function to return the length of the string.
What does the .rstrip()
method do to a string?
What does the .rstrip()
method do to a string?
What is an argument in the context of a function?
What is an argument in the context of a function?
If a variable is declared outside of all functions, what kind of variable is it?
If a variable is declared outside of all functions, what kind of variable is it?
Given the string text = 'Hello, World!'
, what is the index of the character 'W'?
Given the string text = 'Hello, World!'
, what is the index of the character 'W'?
Given the function definition:
def greet(name):
return "Hello, " + name + "!"
What will greet("Alice")
evaluate to?
Given the function definition:
def greet(name):
return "Hello, " + name + "!"
What will greet("Alice")
evaluate to?
What is a parameter in the context of a function?
What is a parameter in the context of a function?
Consider the code:
def modify_string(s):
s = s + " World"
return s
my_string = "Hello"
new_string = modify_string(my_string)
print(my_string)
print(new_string)
What will be printed?
Consider the code:
def modify_string(s):
s = s + " World"
return s
my_string = "Hello"
new_string = modify_string(my_string)
print(my_string)
print(new_string)
What will be printed?
What is the correct way to define a function named calculate_area
that accepts two arguments, length
and width
?
What is the correct way to define a function named calculate_area
that accepts two arguments, length
and width
?
What is the term for the part of a program where a variable can be accessed?
What is the term for the part of a program where a variable can be accessed?
Flashcards
String Index
String Index
The position or index of a character in a string is identified with numbers starting from 0.
String Slicing
String Slicing
Accessing parts of a string using indices to obtain substrings.
string.find()
string.find()
A function to find the index of the first occurrence of a substring.
len(string)
len(string)
Signup and view all the flashcards
string.upper()
string.upper()
Signup and view all the flashcards
string.lower()
string.lower()
Signup and view all the flashcards
string.count()
string.count()
Signup and view all the flashcards
string.capitalize()
string.capitalize()
Signup and view all the flashcards
string.title()
string.title()
Signup and view all the flashcards
string.rstrip()
string.rstrip()
Signup and view all the flashcards
Function
Function
Signup and view all the flashcards
Void Function
Void Function
Signup and view all the flashcards
Value-Returning Function
Value-Returning Function
Signup and view all the flashcards
Variable Scope
Variable Scope
Signup and view all the flashcards
Local Variable
Local Variable
Signup and view all the flashcards
Argument
Argument
Signup and view all the flashcards
Parameter
Parameter
Signup and view all the flashcards
Global Variable
Global Variable
Signup and view all the flashcards
Study Notes
Indices and Slices
- Position or index of a character in a string is identified with numbers starting from 0.
- To access characters, you use square brackets [].
- s[0] gives the first character which is 'a'.
- s[4] gives the fifth character which is 'd'.
- Trying to access an index that is out of range, like s[5] in the example, results in an error.
- s[0:2] yields 'ab', excluding the character at index 2.
- s[2:] yields 'c d', starting from index 2 to the end of the string.
- This is called "string slicing".
String Operations
len(str1)
returns the number of characters in the string.str1.upper()
uppercases every alphabetical character in the string.str1.lower()
lowercases every alphabetical character in the string.str1.count('th')
returns the number of non-overlapping occurrences of the substring "th"."coDE".capitalize()
capitalizes the first letter of the string and lowercases the rest to give "Code"."beN hur".title()
capitalizes the first letter of each word and lowercases the rest, resulting in "Ben Hur"."ab ".rstrip()
removes spaces from the right side of the string, resulting in "ab".
User Defined Functions
- A function is a group of statements within a program designed to perform a specific task.
- Most programs benefit from being divided into subtasks, each handled by a function.
- The
return
statement indicates the value returned by the function. - The
return
statement is optional, and if a function does not return a value, it is often called a procedure.
Benefits of using Functions
- Simpler code is achieved by using smaller functions, which are easier to read.
- Code reuse is possible, the same function can be called multiple times.
- Duplication of code within a program is reduced.
- Better testing is achieved by testing functions separately to isolate and fix errors.
- Focus only on one function at a time while testing.
- Faster development due to reusing common tasks and functions in other programs.
- Examples of common tasks are security functions, displaying current date and time, and arithmetic operations.
- Teamwork is easier to facilitate when each team member is responsible for writing a specific function.
- This allows you to share the workload.
Void Functions
- Executes the statements it contains and then terminates.
Value-returning Functions
- Executes the statements it contains, and then it returns a value back to the statement that is called it.
Defining a Function
- Function name must follow variable naming rules.
- List of parameters being passed should be in parenthesis and comma separated
- Suite of the function follows the colon
- Keyword indicating the function is being defined is def
- Return statement indicates the value returned when the function finishes
Flow of program
- The program flows in order of the code
Scope of a Variable / Local Variables
- Scope describes the part of the program in which a variable can be accessed.
- A local variable is created inside a function and cannot be accessed by statements outside of that function.
- A local variable is destroyed when the function exits.
- If variables in two different functions have the same name, they are independent.
Passing Arguments to Functions
- Sometimes, one or more pieces of data need to be sent to a Function.
- An argument is any piece of data that is passed into a Function when the Function is called.
- A parameter is a variable that receives an argument that is passed into a Function.
- Multiple arguments can be passed sequentially into a parameter list.
Passing Multiple Arguments
- Python allows writing a function that accepts multiple arguments
- Parameter list replaces single parameter
- Parameter list items separated by comma
- Arguments are passed by position to corresponding parameters
- First parameter receives value of first argument, second parameter receives value of second argument, etc.
Global Variable
- Created by assignment statement written outside all the functions
- Can be accessed by any statement in the program file, including from within a function
- If a function needs to assign a value to the global variable, the global variable must be redeclared within the function
- General format: global variable name
Returning a Value
- The function will return a value to the calling function
- To return a number, you simply create a return val
- To return a string, you create a function to parse the data, and return the required string
- To Return a Boolean, the function contains a conditional statement. Will return true or false depending on the statement.
- The function must be defined before the starting point of your program.
- In Python, a function can return multiple values which is specified after the return statement separated by commas
- Format: return expression1, expression2, etc
- When you call such a function in an assignment statement, you need a separate variable on the left side of the = operator to receive each returned value
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.