1/46
Flashcards for reviewing Python programming concepts covered in the lecture.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What are variables used for in Python?
To temporarily store data in computer’s memory.
What are the four basic data types in Python as discussed in the notes?
Integer (int), Float, String, Boolean
What is the purpose of comments in Python code?
To add notes to the code, explaining the hows and whys, add reminders, explain assumptions, and reasons for specific code implementations.
How do you receive input from the user in Python?
By calling the input() function.
What data type does the input() function return?
String
How can you define strings in Python?
Using single (' ') or double (" ") quotes.
How do you define a multi-line string?
Surround the string with triple quotes (""").
How do you access individual characters in a string?
Using square brackets [] with the index of the character.
What does string slicing achieve?
Returns a substring of the original string, based on specified start and end indices.
What are formatted strings?
Formatted strings allow you to dynamically insert values into strings.
How do you convert a string to uppercase in Python?
Using the upper() method.
How do you find the index of the first occurrence of a character in a string?
Using the find() method.
How do you replace characters in a string?
Using the replace() method.
How do you check if a string contains a character or sequence of characters?
Using the 'in' operator.
List the common arithmetic operators in Python.
Which operator returns the remainder of a division?
% (Modulus operator)
What does the augmented assignment operator do?
It combines an arithmetic operation with an assignment (e.g., x += 10 is equivalent to x = x + 10).
What is operator precedence?
The order in which operations are performed in an expression. (Parenthesis, Exponentiation, Multiplication/Division, Addition/Subtraction)
What is the syntax for an if statement in Python?
if condition: … elif condition: … else: …
What are logical operators?
and, or, not
What are comparison operators?
(greater than), >= (greater than or equal to), < (less than), <= (less than or equal to), == (equals), != (not equals)
What is the syntax for a while loop in Python?
while condition: …
What is the syntax for a for loop in Python?
for i in range(start, end, step): …
What does range(5) generate?
0, 1, 2, 3, 4
What is a list in Python?
An ordered, mutable sequence of items.
How do you access the first item in a list?
numbers[0]
How do you add an item to the end of a list?
numbers.append(item)
How do you insert an item at a specific index in a list?
numbers.insert(index, item)
What are tuples?
Read-only lists. Once defined cannot add/remove/change items.
How do you unpack a tuple into separate variables?
x, y, z = coordinates
What are dictionaries used for?
To store key/value pairs.
How do you access a value in a dictionary?
customer["name"] or customer.get("name", defaultValue)
What are functions used for?
To break up code into small, reusable chunks that are easier to read, understand, and maintain.
What is the difference between parameters and arguments?
Parameters are placeholders in the function definition, while arguments are the actual values passed to the function when it's called.
What are the two types of arguments?
Positional arguments and Keyword arguments
What is returned if a function doesn't have a return statement?
None
What are exceptions?
Errors that crash programs.
How do you handle exceptions in Python?
Using try…except blocks.
What are classes used for?
To define new types.
What is a method?
A function that is part of a class.
What is an object?
An instance of a class.
What is the purpose of the init method?
It's a constructor that gets called when creating new objects to initialize them.
What is inheritance?
A technique to remove code duplication by creating a base class with common methods and having other classes inherit those methods.
What is a module?
A file with some Python code. Used to break up programs into multiple files.
How can you import modules?
import modulename or from modulename import object
What is a package?
A directory with init.py in it which contains one or more modules.
What is the Python Standard Library?
A huge library of modules for performing common tasks such as sending emails, working with date/time, generating random values, etc.