|
| 1 | +Solutions: |
| 2 | + |
| 3 | +1. **What is the difference between declaring a variable using `x = 5` and `x = '5'` in Python?** |
| 4 | + - `x = 5` assigns an integer value (number) to `x`, while `x = '5'` assigns a string value (text) to `x`. They are of different data types, with one being an integer and the other a string. |
| 5 | + |
| 6 | +2. **How would you get input from the user and store it in a variable named `age` in Python?** |
| 7 | + - You can use the `input()` function like this: |
| 8 | + ```python |
| 9 | + age = input("Enter your age: ") |
| 10 | + ``` |
| 11 | + |
| 12 | +3. **What does the `print()` function do in Python, and how can you use it to display multiple variables?** |
| 13 | + - The `print()` function outputs the values to the console. You can display multiple variables by separating them with commas: |
| 14 | + ```python |
| 15 | + name = "John" |
| 16 | + age = 25 |
| 17 | + print("Name:", name, "Age:", age) |
| 18 | + ``` |
| 19 | + |
| 20 | +4. **If you input a number using `input()`, what data type is it stored as by default? How do you convert it to an integer?** |
| 21 | + - By default, the `input()` function stores the input as a string. To convert it to an integer, you can use `int()` like this: |
| 22 | + ```python |
| 23 | + number = int(input("Enter a number: ")) |
| 24 | + ``` |
| 25 | + |
| 26 | +5. **Write a Python program that asks the user for their name and age, and then prints "Hello, [name]. You are [age] years old."** |
| 27 | + ```python |
| 28 | + name = input("Enter your name: ") |
| 29 | + age = input("Enter your age: ") |
| 30 | + print(f"Hello, {name}. You are {age} years old.") |
| 31 | + ``` |
| 32 | + |
| 33 | +6. **Explain the difference between concatenating strings and adding numbers in Python. What happens if you try to concatenate a string and a number?** |
| 34 | + - Concatenating strings combines two or more strings together, while adding numbers results in the sum of the values. If you try to concatenate a string and a number, Python will raise a `TypeError`. You need to convert the number to a string using `str()` before concatenating: |
| 35 | + ```python |
| 36 | + result = "Age: " + str(25) |
| 37 | + ``` |
| 38 | + |
| 39 | +7. **What will be the output of the following code snippet? Why?** |
| 40 | + ```python |
| 41 | + x = 10 |
| 42 | + y = 20 |
| 43 | + print('x + y =', x + y) |
| 44 | + ``` |
| 45 | + - Output: |
| 46 | + ``` |
| 47 | + x + y = 30 |
| 48 | + ``` |
| 49 | + The `print()` function first prints the string `'x + y ='`, then prints the result of `x + y` which is `10 + 20 = 30`. |
| 50 | +
|
| 51 | +8. **How can you format a string using f-strings to display a sentence with variable values? Provide an example.** |
| 52 | + - F-strings allow you to embed expressions inside string literals using curly braces `{}`. Example: |
| 53 | + ```python |
| 54 | + name = "Alice" |
| 55 | + age = 30 |
| 56 | + print(f"Hello, {name}. You are {age} years old.") |
| 57 | + ``` |
| 58 | + This will output: |
| 59 | + ``` |
| 60 | + Hello, Alice. You are 30 years old. |
| 61 | + ``` |
| 62 | +
|
| 63 | +9. **What happens if you try to assign a value to a variable using `x = input("Enter a number: ")` and then add it directly to another number like this: `y = x + 5`? How can you fix it?** |
| 64 | + - Since `x` is stored as a string from `input()`, trying to add a string to a number will cause a `TypeError`. To fix it, convert `x` to an integer first: |
| 65 | + ```python |
| 66 | + x = int(input("Enter a number: ")) |
| 67 | + y = x + 5 |
| 68 | + print(y) |
| 69 | + ``` |
| 70 | +
|
| 71 | +10. **What is the difference between the following two code snippets, and what will they print?** |
| 72 | + ```python |
| 73 | + print("The answer is", 42) |
| 74 | + print("The answer is " + str(42)) |
| 75 | + ``` |
| 76 | + - In the first snippet, `print("The answer is", 42)` separates the string and number with a space and prints: |
| 77 | + ``` |
| 78 | + The answer is 42 |
| 79 | + ``` |
| 80 | + - In the second snippet, `print("The answer is " + str(42))` explicitly concatenates the string and the string version of `42` without additional spaces. It prints: |
| 81 | + ``` |
| 82 | + The answer is 42 |
| 83 | + ``` |
0 commit comments