1/41
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a function in Python?
A function is a block of code that takes input, performs actions, and returns output.
How do you identify a function call in Python?
By its name followed by parentheses, e.g., print("Hello").
What does the print() function do in Python?
It outputs text or data to the terminal, e.g., print("CS50").
How do you define a function in Python?
Use def, e.g., def main():.
What does indentation signify in a function definition?
It defines the block of code that belongs to the function.
How do you run a function after defining it?
By calling it using its name with parentheses, e.g., main().
Can an entire program be a function in Python?
Yes, it can be structured around a main function for organization.
What is a variable in Python?
A variable stores data that can change during a program, e.g., x = 10.
How do you create a variable in Python?
Use the assignment operator, e.g., guess = 5.
What symbol assigns a value to a variable in Python?
The equals sign = is used for assignment.
How can functions and variables work together?
Functions can define and return variables, e.g., return guess.
How do you get input from a user in Python?
Use input(), e.g., name = input("Enter name: ").
What does input() return in Python?
It returns a string entered by the user.
What is variable scope in Python?
It defines where a variable is accessible—local or global.
Are variables inside functions local or global by default?
They are local to that function by default.
What happens if two functions use variables with the same name?
They refer to different local variables unless declared global.
How do you compare a string input to a number in Python?
Convert the input to an integer, e.g., int(input()) == 50.
What are the main variable types in Python?
Integers (int) and strings (str) are most common.
What is a string in Python?
A sequence of characters, e.g., "hello".
Why is "50" == 50 false in Python?
Because "50" is a string and 50 is an integer.
What does the return keyword do in a function?
It sends a value back to the caller, e.g., return "Hi".
What’s the difference between return values and side effects?
Return values are reusable; side effects are immediate outputs.
How do you store a function's return value?
Assign it to a variable, e.g., result = greet("Hi").
How can you modify a return value after capturing it?
Use it in expressions, e.g., print("Hey " + greeting).
Can you reuse return values in multiple places?
Yes, you can store, modify, and print them.
Why are return values more flexible than side effects?
They can be reused and manipulated later in the program.
Which function output is more versatile: print() or return?
return, because the value can be saved and reused.
What is a side effect in programming?
An action like printing or modifying a global variable.
Give an example of a side effect in Python.
print("Hello") outputs immediately without returning a value.
What’s an example of a function that causes a side effect?
def say(msg): print(msg) causes a print side effect.
How do you make a variable global? (use inside all functions)
Use the global keyword
example: global time
What are potential risks of using global variables?
They can make code harder to debug and manage.
What does the .capitalize() method do?
It capitalizes the first character of a string.
What is a limitation of .capitalize()?
It doesn't capitalize each word—only the first character of the string.
What does the .title() method do?
It capitalizes the first letter of every word in the string.
When is .title() better than .capitalize()?
When formatting titles or multi-word strings.
What does the .strip() method do?
It removes whitespace from the beginning and end of a string.
Why is .strip() useful for cleaning strings?
It gets rid of unwanted spaces that affect formatting.
What does it mean to chain string methods?
Applying multiple methods in a single line for cleaner code.
Why would you chain .strip() and .title()?
To remove spaces and format the string as a proper title.
What does the .join() method do?
It joins a list of strings into a single string using a delimiter.
Why use .join() when working with lists?
To turn multiple strings into a single, readable sentence.