CS50 Python Week 0: Shorts

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/41

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

42 Terms

1
New cards

What is a function in Python?

A function is a block of code that takes input, performs actions, and returns output.

2
New cards

How do you identify a function call in Python?

By its name followed by parentheses, e.g., print("Hello").

3
New cards

What does the print() function do in Python?

It outputs text or data to the terminal, e.g., print("CS50").

4
New cards

How do you define a function in Python?

Use def, e.g., def main():.

5
New cards

What does indentation signify in a function definition?

It defines the block of code that belongs to the function.

6
New cards

How do you run a function after defining it?

By calling it using its name with parentheses, e.g., main().

7
New cards

Can an entire program be a function in Python?

Yes, it can be structured around a main function for organization.

8
New cards

What is a variable in Python?

A variable stores data that can change during a program, e.g., x = 10.

9
New cards

How do you create a variable in Python?

Use the assignment operator, e.g., guess = 5.

10
New cards

What symbol assigns a value to a variable in Python?

The equals sign = is used for assignment.

11
New cards

How can functions and variables work together?

Functions can define and return variables, e.g., return guess.

12
New cards

How do you get input from a user in Python?

Use input(), e.g., name = input("Enter name: ").

13
New cards

What does input() return in Python?

It returns a string entered by the user.

14
New cards

What is variable scope in Python?

It defines where a variable is accessible—local or global.

15
New cards

Are variables inside functions local or global by default?

They are local to that function by default.

16
New cards

What happens if two functions use variables with the same name?

They refer to different local variables unless declared global.

17
New cards

How do you compare a string input to a number in Python?

Convert the input to an integer, e.g., int(input()) == 50.

18
New cards

What are the main variable types in Python?

Integers (int) and strings (str) are most common.

19
New cards

What is a string in Python?

A sequence of characters, e.g., "hello".

20
New cards

Why is "50" == 50 false in Python?

Because "50" is a string and 50 is an integer.

21
New cards

What does the return keyword do in a function?

It sends a value back to the caller, e.g., return "Hi".

22
New cards

What’s the difference between return values and side effects?

Return values are reusable; side effects are immediate outputs.

23
New cards

How do you store a function's return value?

Assign it to a variable, e.g., result = greet("Hi").

24
New cards

How can you modify a return value after capturing it?

Use it in expressions, e.g., print("Hey " + greeting).

25
New cards

Can you reuse return values in multiple places?

Yes, you can store, modify, and print them.

26
New cards

Why are return values more flexible than side effects?

They can be reused and manipulated later in the program.

27
New cards

Which function output is more versatile: print() or return?

return, because the value can be saved and reused.

28
New cards

What is a side effect in programming?

An action like printing or modifying a global variable.

29
New cards

Give an example of a side effect in Python.

print("Hello") outputs immediately without returning a value.

30
New cards

What’s an example of a function that causes a side effect?

def say(msg): print(msg) causes a print side effect.

31
New cards

How do you make a variable global? (use inside all functions)

Use the global keyword

example: global time

32
New cards

What are potential risks of using global variables?

They can make code harder to debug and manage.

33
New cards

What does the .capitalize() method do?

It capitalizes the first character of a string.

34
New cards

What is a limitation of .capitalize()?

It doesn't capitalize each word—only the first character of the string.

35
New cards

What does the .title() method do?

It capitalizes the first letter of every word in the string.

36
New cards

When is .title() better than .capitalize()?

When formatting titles or multi-word strings.

37
New cards

What does the .strip() method do?

It removes whitespace from the beginning and end of a string.

38
New cards

Why is .strip() useful for cleaning strings?

It gets rid of unwanted spaces that affect formatting.

39
New cards

What does it mean to chain string methods?

Applying multiple methods in a single line for cleaner code.

40
New cards

Why would you chain .strip() and .title()?

To remove spaces and format the string as a proper title.

41
New cards

What does the .join() method do?

It joins a list of strings into a single string using a delimiter.

42
New cards

Why use .join() when working with lists?

To turn multiple strings into a single, readable sentence.