0% found this document useful (0 votes)
14 views7 pages

Chapter 3_ Charming the Python

Chapter 3 discusses common Python syntax errors, including incorrect print statements and variable references. It also covers valid variable names, data types, and input handling, emphasizing the importance of type conversion. Additionally, the chapter explains operators and provides examples of string concatenation and type casting.

Uploaded by

Avik Sarkhel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Chapter 3_ Charming the Python

Chapter 3 discusses common Python syntax errors, including incorrect print statements and variable references. It also covers valid variable names, data types, and input handling, emphasizing the importance of type conversion. Additionally, the chapter explains operators and provides examples of string concatenation and type casting.

Uploaded by

Avik Sarkhel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 3: Charming the python

MCQ

### **1. Incorrect print statement syntax**

The given statement:

```python

Print(“The total number of geals scored by captain sunil Chhetri 14, 235)

```

Has a **syntax error** (missing closing quotation mark). Assuming the intended statement
was:

```python

Print(“The total number of goals scored by captain Sunil Chhetri is”, 235)

```

The output would be:

```

The total number of goals scored by captain Sunil Chhetri is 235

```

#### **Answer:** **(b) The total number of goals scored by captain Sunil Chhetri is 235**

### **2. Incorrect variable reference**

The given code:

```python

Marks 35 # Syntax error, should be marks = 35

Print(“The total is “, “marks”)

```
Here, `”marks”` is treated as a string, **not a variable**. The correct way to use the
variable is:

```python

Print(“The total is”, marks)

```

If the incorrect code runs, the output will be:

```

The total is marks

```

#### **Answer:** **© The total is, marks**

### **3. Valid variable name**

- `@price` → **Invalid** (Cannot start with `@`)

- `799price` → **Invalid** (Cannot start with a number)

- `_price` → **Valid** (Can start with `_`)

- `latest price` → **Invalid** (Contains a space)

#### **Answer:** **(b) _price**

### **4. Data types used in the table**

Given table:

| **Variable Name** | **Value** |

|--------------------|----------------------|

| Player_name | Christiano Ronaldo | *(String)* |

| Country | Portugal | *(String)* |


| Matches_played | 1158 | *(Integer)* |

| Total_goals | 834 | *(Integer)* |

| Height_in_m | 1.89 | *(Float)* |

| Weight_in_kg | 84.9 | *(Float)* |

**Three data types are present:**

1. **String** → `”Christiano Ronaldo”`, `”Portugal”`

2. **Integer** → `1158`, `834`

3. **Float** → `1.89`, `84.9`

#### **Answer:** **© 3**

### **5. Incorrect input type handling**

Code:

```python

Test_1 = input(“Enter marks in 1st test:”)

Test_2 = input(“Enter marks in 2nd test:”)

Print(“Total marks in two tests”, Test_1 + Test_2)

```

- `input()` returns **strings**, so `”45” + “48”` results in `”4548”` (**string


concatenation**).

- To fix this, convert to integers:

```python

Test_1 = int(input(“Enter marks in 1st test:”))

Test_2 = int(input(“Enter marks in 2nd test:”))

Print(“Total marks in two tests”, Test_1 + Test_2)

```
This would output `93`, but **with the given incorrect code**, the output will be:

```

Total marks in two tests 4548

```

#### **Answer:** **© Total marks in two tests = 4548**

FILL IN THE BLANKS:

1. The rules of writing commands in a program are known as the syntax.


2. The input() function enables a user to enter data.
3. # is the first character for a single-line comment, and ''' (triple single quotes) or
""" (triple double quotes) are used to enclose the text for multiple-line comments.
4. The bool prefix is used to typecast a data type to Boolean.
5. A digit (0-9) cannot be the first character of a variable name.

DESCRIPTIVE QUESTIONS:
1. Write the output of the following programs:

a.

Print("Python is fun")

Error: Print should be lowercase (print). The correct version:

print("Python is fun")

Output:

Python is fun

b.

Print("5+3")

Since 5+3 is inside quotes, it is treated as a string.


Output:

5+3
c.

Print("5,3")

Inside quotes, 5,3 is treated as a string.


Output:

5,3

d.

Print("5/n3")

Here, \n represents a newline character.


Output:

5
3

2. Look at the following program:


Print("The total marks")
Print(360+400)

Since Print is incorrect (it should be print), this will cause an error.
If corrected, the output will be:

The total marks


760

Explanation:

• "The total marks" is printed as a string.


• 360+400 is a mathematical operation, so Python computes the sum and prints 760.

3. Program to take inputs for boys, girls, and teachers, then display total students and
teacher-student ratio
# Taking inputs
boys = int(input("Enter the number of boys: "))
girls = int(input("Enter the number of girls: "))
teachers = int(input("Enter the number of teachers: "))

# Calculating total students


total_students = boys + girls

# Calculating teachers per student


teachers_per_student = teachers / total_students

# Displaying results
print("Total number of students:", total_students)
print("Number of teachers per student:", teachers_per_student)

Sample Input and Output:

Enter the number of boys: 20


Enter the number of girls: 25
Enter the number of teachers: 5
Total number of students: 45
Number of teachers per student: 0.1111

4. Explanation of operators:

a. == (Equality Operator)

• Used to check if two values are equal.


• Example:
• print(5 == 5) # True
• print(5 == 3) # False

b. // (Floor Division Operator)

• Divides two numbers and returns the integer quotient, discarding the decimal part.
• Example:
• print(10 // 3) # Output: 3
• print(7 // 2) # Output: 3

c. % (Modulo Operator)

• Returns the remainder after division.


• Example:
• print(10 % 3) # Output: 1
• print(7 % 2) # Output: 1

5. Programs to explain:

a. String Concatenation

• Combining two or more strings using +.

str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result)
Output:

Hello World

b. Type Casting

• Converting one data type to another.

num_str = "123"
num_int = int(num_str) # Converts string to integer
num_float = float(num_str) # Converts string to float

print(num_int) # Output: 123


print(num_float) # Output: 123.0

You might also like