Python — Short Answer Questions

Variables • Data Types • Operators • Input & Output • Escape Sequences

Question 1

What are escape sequences in Python?

Answer

Escape sequences are special character combinations beginning with a backslash (\) that are used to represent special characters or formatting within a Python string.

Escape Sequence Meaning Example
\n New line "Hello\nWorld"
\t Horizontal tab "Name\tAge"
\\ Backslash character "C:\\Users"
\" Double quotation mark "He said \"Hello\""
Example:
print("Hello\nPython")

Output:

Hello
Python

Question 2

Write any 2 rules that must be followed while naming a variable in Python.

Answer

Any two important rules are:

  1. A variable name must begin with a letter (A–Z or a–z) or an underscore (_). It cannot begin with a digit.
  2. A variable name can contain letters, digits and underscores, but spaces and other special characters are not allowed.
Valid:
name = "Anant"
student_1 = "Riya"
_age = 14
Invalid:
1student = "Riya"
student name = "Riya"
Remember: Python variable names are case-sensitive. age and Age are different names. Python keywords such as if, for and class cannot be used as variable names.

Question 3

What is the purpose of the assignment operator (=) in Python?

Answer

The assignment operator (=) is used to assign a value or the result of an expression to a variable.

age = 13
name = "Riya"
total = 25 + 15

Here, 13 is assigned to age, "Riya" is assigned to name, and the result of 25 + 15 is assigned to total.

The assignment operator should not be confused with the equality comparison operator ==.

Operator Purpose Example
= Assignment x = 10
== Checks whether two values are equal x == 10

Question 4

Explain the role of the print() and input() functions in a Python program.

Answer

1. print() Function

The print() function is used to display information or output on the screen.

print("Welcome to Python")
print(25)

Output:

Welcome to Python
25

2. input() Function

The input() function is used to accept data from the user through the keyboard. It returns the entered value as a string unless the value is explicitly converted to another data type.

name = input("Enter your name: ")
print("Hello", name)
Example with a number:
age = int(input("Enter your age: "))
print(age)

Here, int() converts the string returned by input() into an integer.

Function Purpose
print() Displays output to the user.
input() Accepts input from the user.

Question 5

Give two examples each of integer and string data types.

Answer

Data Type Example 1 Example 2
Integer (int) 25 -10
String (str) "Hello" "Python"
Python example:
marks = 95
temperature = -10

name = "Riya"
language = "Python"

The first two values are integers, while the last two values are strings.

Quick Revision

Question Key Answer
1 Escape sequences begin with \ and represent special characters or formatting in strings.
2 Variable names must start with a letter or underscore and may contain letters, digits and underscores.
3 = assigns a value or expression result to a variable.
4 print() displays output; input() accepts user input.
5 Integers: 25, -10; Strings: "Hello", "Python".