and Answers Latest Update Graded A+
What happens if you try to print a variable before defining it in Python?
✔✔ You’ll get a `NameError` because Python doesn’t know what that variable refers to.
Can you multiply a string by an integer? If so, what will `print("Hi" * 3)` output?
✔✔ Yes, you can multiply a string by an integer, and it will repeat the string. The output will be
`"HiHiHi"`.
How can you check if a number is negative without using `if`?
✔✔ You can use a single expression like `number < 0`, which evaluates to `True` or `False`.
What would the following code print?
```python
x = "A quick brown fox"
print(x[7:13])
```
1
,✔✔ The output will be `"brown"`, because we are slicing the string from index `7` to `13`.
What’s the simplest way to swap the values of two variables in Python?
✔✔ You can use a single line like `a, b = b, a` to swap the values of `a` and `b`.
Can a list contain different data types like strings, integers, and floats?
✔✔ Yes, lists can hold a mix of data types like strings, integers, and floats, e.g., `my_list = [1,
"Hello", 3.14]`.
How do you reverse a string in Python?
✔✔ You can reverse a string by slicing it with `[::-1]`, like `"hello"[::-1]` which would return
`"olleh"`.
What happens if you try to divide by zero in Python?
✔✔ You will get a `ZeroDivisionError` because division by zero is undefined.
What does the following code do?
2
, ```python
x = 10
y=5
print(x % y)
```
✔✔ It will print `0` because `10 % 5` is the remainder when `10` is divided by `5`, and there’s
no remainder.
How do you turn all letters of a string into uppercase?
✔✔ You can use the `.upper()` method, like `"hello".upper()` which will return `"HELLO"`.
How do you merge two lists into one?
✔✔ You can concatenate two lists using the `+` operator, like `list1 + list2`.
What does `break` do in a loop?
✔✔ It immediately stops the loop, no matter where it is, and continues with the next line of code
outside the loop.
3