0% found this document useful (0 votes)
104 views16 pages

Cs Notes Chapter 5 6 7 8 9

Chapter 5 introduces Python, explaining its definition, history, and basic features. It covers fundamental concepts such as running modes, variables, keywords, data types, and error types, along with practical programming exercises. The chapter also emphasizes the importance of understanding identifiers, literals, operators, and control flow in Python programming.

Uploaded by

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

Cs Notes Chapter 5 6 7 8 9

Chapter 5 introduces Python, explaining its definition, history, and basic features. It covers fundamental concepts such as running modes, variables, keywords, data types, and error types, along with practical programming exercises. The chapter also emphasizes the importance of understanding identifiers, literals, operators, and control flow in Python programming.

Uploaded by

jaaduinsts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CHAPTER 5 – GETTING STARTED WITH PYTHON

📘 1. Very Short Answer Questions (1 Mark)


Q1. What is Python?
A: Python is a high-level, interpreted, general-purpose programming language
known for its simplicity and readability.
Q2. Who developed Python and when?
A: Guido van Rossum developed Python in 1989.
Q3. What is the file extension of a Python program?
A: .py
Q4. What are keywords in Python?
A: Keywords are reserved words that have special meaning in Python, such
as if, else, while, True, def, etc.
Q5. Write two features of Python.
A:
 Easy to learn and use
 Open-source and free to use

📗 2. Short Answer Questions (2-3 Marks)


Q6. What are the two modes of running Python programs? Explain.
A:
 Interactive Mode: Code is typed and executed line by line directly in the
Python shell.
 Script Mode: Code is written in a file with .py extension and then
executed all at once.
Q7. What is the difference between a variable and a keyword?
A:
 A variable is a name used to store data. Example: x = 5
 A keyword is a reserved word with special meaning. Example: if, for, def.
Q8. Is my-name a valid Python identifier? Justify.
A: No. Hyphen - is not allowed in identifiers. Valid identifiers can contain
letters, digits, and underscores only.
Q9. Differentiate between syntax error and runtime error.
A:
 Syntax Error: Occurs when the rules of the language are broken.
Detected before execution.
 Runtime Error: Occurs during execution, such as division by zero.
Q10. What are data types in Python? Give examples.
A:
 int – integers (e.g., 5)
 float – decimal numbers (e.g., 3.14)
 str – strings (e.g., "Hello")
 bool – boolean (True, False)

📘 3. Long Answer Questions (4-5 Marks)


Q11. Explain type conversion in Python with examples.
A:
 Implicit Conversion: Python automatically converts types.
x = 5 y = 2.0 print(x + y) # Output: 7.0 (int is converted to float)
 Explicit Conversion: Manual conversion using functions
like int(), float(), str()
a = "100" b = int(a) # Converts string to integer

Q12. Write a Python program to accept name and age from the user and print
a message.
name = input("Enter your name: ") age = int(input("Enter your age: "))
print("Hello", name, "! You are", age, "years old.")
Q13. Write a Python program to calculate the area of a circle.
radius = float(input("Enter radius: ")) area = 3.1416 * radius ** 2 print("Area of
circle =", area)

Q14. Explain the difference between interactive and script mode using
examples.
A:
Mode Description Example

Interactive One line executed at a time >>> print("Hello") → Hello

Full program saved in file and then


Script executed File: hello.py with full code

🧠 4. Error Identification
Q15. Identify the type of error in each line:
a) print("Hello) → Syntax Error (missing quote)
b) x = 5 / 0 → Runtime Error (division by zero)
c) print("10" + 5) → Type Error (string + int)

🧪 5. Practice Programs
Q16. Write a Python program to swap two numbers using a third variable.
a = int(input("Enter first number: ")) b = int(input("Enter second number: "))
temp = a a = b b = temp print("After swapping: a =", a, "b =", b)
Q17. Write a Python program to multiply 2 numbers input by the user.
x = int(input("Enter first number: ")) y = int(input("Enter second number: "))
print("Product =", x * y)
Q18. Write a Python program to calculate the simple interest.
P = float(input("Enter Principal: ")) R = float(input("Enter Rate: ")) T =
float(input("Enter Time: ")) SI = (P * R * T) / 100 print("Simple Interest =", SI)

🔑 Important Keywords to Remember


Keyword Purpose

print() Displays output

input() Accepts user input

int() Converts to integer

float() Converts to float

str() Converts to string

** Exponentiation (power)

// Floor division

% Modulus (remainder)

Chapter 6: Python Fundamentals


very Short Answer (1 mark)
1. Q: What is an identifier in Python?
A: An identifier is a name used to identify a variable, function, class,
module, or other object. It must follow Python’s naming rules: begin
with a letter or underscore, contain only letters, digits, or underscores,
and not be a keyword.
2. Q: Are keywords allowed to be used as identifiers?
A: No. Keywords are reserved words in Python and cannot be used as
identifiers.
3. Q: Give two Boolean literals in Python.
A: True and False.
4. Q: Is Python case-sensitive? What does case sensitivity mean?
A: Yes, Python is case sensitive. That means Var, var, and VAR are three
different identifiers.
5. Q: What is a token in Python? Name some types of tokens.
A: A token is the smallest individual unit in a program that has meaning
to the interpreter. Types include: identifiers, keywords, literals (numeric,
string etc.), operators, delimiters.

Short Answer (2-3 marks)


6. Q: Which of these are valid identifiers? Explain why/why not: Data_rec,
_data, 1data, my.file, elif, lambda, break.
A:
o Data_rec → Valid (starts with letter, uses underscore)
o _data → Valid (starts with underscore)
o 1data → Invalid (starts with digit)
o my.file → Invalid (contains dot “.”)
o elif → Invalid (keyword)
o lambda → Invalid (keyword)
o break → Invalid (keyword)
7. Q: How are floating point literals represented in Python? Give examples.
A: Floating point literals can be in fractional form or exponent (scientific)
form.
Examples: 12.34, 0.3E-01, 4.56e2, 0.00123, etc.
8. Q: What is a literal? What types of literals are there in Python?
A: A literal is a notation for representing a fixed value in source code.
Types include:
o Integer literals (decimal, octal, hexadecimal)
o Floating-point literals (fractional, exponent)
o String literals (single or double quoted)
o Boolean literals (True, False)
o The special literal None

Long / Application-Type (4 marks)


9. Q: What are operators in Python? Distinguish between unary and binary
operators with examples.
A:
o Operators are symbols that perform operations on operands
(values or variables).
o Unary operators operate on a single operand. Example: -x, +x,
logical NOT: not x.
o Binary operators operate on two operands. Example: a + b, a * b,
a < b, a and b.
10.Q: What is the difference between an expression and a statement in
Python? Give examples.
A:
o An expression is a combination of values, variables, operators
which yields a value. Eg: 2 + 3, a * b, 5 > 3.
o A statement is an instruction that the Python interpreter can
execute. E.g., assignment: x = 5, print("Hello"). A statement may
contain expressions.
11.Q: Explain the role of indentation in Python. What happens if indentation
is incorrect?
A:
o Indentation in Python is used to define blocks of code (such as the
body of loops, conditionals, functions). It replaces brackets used in
some other languages.
o Correct indentation is mandatory. If indentation is incorrect (too
many / too few spaces / inconsistent), Python gives an
IndentationError or unexpected behavior.

Chapter 7:data handling


ype A & Short Answers
1. Q: What are data types? How are they important?
A: Data types define what kind of data a variable can hold, and what
operations can be performed on it. For example, integers, floats, strings,
booleans. They help in memory allocation, controlling operations, and
avoiding errors.
2. Q: How are these numbers different from one another: 33, 33.0, 33j, 33
+ j?
A:
o 33 → integer
o 33.0 → floating-point number
o 33j → purely imaginary complex number
o 33 + j (assuming j = 1j) → complex number with both real and
imaginary parts
3. Q: What are mutable and immutable types in Python? List examples of
each.
A:
o Immutable types are those whose content cannot be changed
after they are created. Examples: int, float, bool, str, tuple.
o Mutable types are those whose content can change. Examples:
list, set, dictionary.
4. Q: What does the is operator do? How is it different from ==?
A:
o is checks whether two variables refer to the same object in
memory.
o == checks whether the values of two objects are equal (even if
they are different objects).
5. Q: How do you convert between different data types in Python
explicitly? Give example.
A: Use conversion functions like int(), float(), str(), complex().
Example:
6. s = "123"
7. i = int(s) # converts string to integer
8. f = float(i) # converts integer to float

Type B & Application / “What will be Output?” Type

7. Q: What is the output of this code? Explain.


a=5-4-3
b = 3 ** 2 ** 3
print(a)
print(b)
A:
o print(a) → -2 (because 5 - 4 = 1, then 1 - 3 = -2)
o print(b) → 6561 (because exponentiation is right-associative: 2 **
3 = 8, then 3 ** 8 = 6561)
8. Q: What will be the output of:
a, b, c = 1, 1, 2
d=a+b
e = 1.0
f = 1.0
g = 2.0
h=e+f
print(c == d)
print(c is d)
print(g == h)
print(g is h)

Q: Explain what this code does and write its output:


a = 12
b = 7.4
c=1
a -= b
print(a, b)
a *= 2 + c
print(a)
b += a * c
print(b)
A:
o After a -= b: a = 12 - 7.4 = 4.6, b remains 7.4 → print: 4.6 7.4
o Then a *= 2 + c: 2 + c = 3, so a = 4.6 * 3 = 13.8 → print: 13.8
o Then b += a * c: a * c = 13.8, so b = 7.4 + 13.8 = 21.2 → print: 21.2

Type C / Programming Practice


11.Q: Write a program to take year as input and check if it is a leap year or
not.

12.Q: Write a program that reads a number of seconds (from user) and
prints it in form: minutes and seconds. For example, 200 seconds should
be printed as 3 minutes and 20 seconds.
A:
total_seconds = int(input("Enter seconds: "))
mins = total_seconds // 60
secs = total_seconds % 60
print(mins, "minutes and", secs, "seconds")
13.Q: Write a program to reverse a two-digit number. E.g., input 25, output
52.
Ans:
x = int(input("Enter a two digit number: "))
rev2 = (x % 10) * 10 + (x // 10)
print("Reversed number:", rev2)

Questions & Answers: chapter 9 Flow of Control

Very Short / Short Answer Type


1. Q: What is null (empty) statement? What is its need?
A: The null (or empty) statement is a statement that does nothing. In
Python, this is pass. It is needed when the syntax of the language expects
a statement but logically nothing needs to be done.
2. Q: What is a compound statement (also called block)? What is its
common structure in Python?
A: A compound statement (block) is a group of statements treated as a
unit. In Python, a compound statement typically has a header line ending
with a colon (:), followed by an indented block (body) of statements.
Examples include if, for, while, etc.
3. Q: What are the three main programming constructs that govern flow of
control?
A: Sequence, Selection (Decision), and Repetition (Iteration).
4. Q: What is an entry-controlled loop? Give one example.
A: An entry-controlled loop is a loop in which the test condition is
checked before entering (executing) the loop body. If the condition is
false initially, the loop body may not execute even once. An example in
Python is the while loop.
5. Q: What is the difference between an if-else statement and if-elif-else
statement?
A:
o if-else: has one condition; if the if condition is true → executes first
block, otherwise executes the else block.
o if-elif-else: allows multiple conditions to be tested sequentially;
once an earlier elif condition is found true, the rest are skipped; if
none true, the else block executes.
6. Q: What are jump statements in Python? Name them and briefly explain.
A: Jump statements are those that alter normal sequential flow by
skipping or terminating loops or parts of loops. In Python, main jump
statements are:
o break: exits the loop completely
o continue: skips the rest of the current iteration and moves to the
next iteration
7. Q: What is an algorithm? What is a flowchart? How are they related to
flow of control?
A:
o Algorithm: A step-by-step procedure or set of rules to solve a
problem.
o Flowchart: A pictorial representation of an algorithm, showing
steps and flow (using shapes and arrows).
They help plan and visualize the flow of control before coding.
8. Q: In which cases the else clause of a loop does not get executed?
A: When the loop is terminated by a break statement, i.e. prematurely,
before the loop condition becomes false naturally. In such cases, the else
attached to the loop is skipped.
✅ Learn all practical file questions
✅ Revise all the programs you did in the computer lab
1. Input / Output in Python
2. Operators & Expressions
3. Conditional statements (if, elif, else)
4. Loops (for, while)
5. Nested loops
6. Basic programs using math, strings, and numbers
7. Menu-driven programs
8. Pattern printing

✅ Step 2: Practical File Questions with Sample Programs

🔸 1. Add two numbers entered by the user


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum is:", a + b)

🔸 2. Check whether a number is even or odd


n = int(input("Enter a number: "))
if n % 2 == 0:
print("Even number")
else:
print("Odd number")

🔸 3. Find the greatest of 3 numbers


a = int(input("Enter 1st number: "))
b = int(input("Enter 2nd number: "))
c = int(input("Enter 3rd number: "))
if a >= b and a >= c:
print(a, "is greatest")
elif b >= c:
print(b, "is greatest")
else:
print(c, "is greatest")

🔸 4. Calculate factorial of a number


n = int(input("Enter a number: "))
fact = 1
for i in range(1, n+1):
fact *= i
print("Factorial:", fact)

🔸 5. Check if a number is prime


n = int(input("Enter a number: "))
if n <= 1:
print("Not Prime")
else:
for i in range(2, n):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime number")
🔸 6. Display multiplication table of a number
n = int(input("Enter a number: "))
for i in range(1, 11):
print(n, "x", i, "=", n*i)

🔸 7. Reverse a number
n = int(input("Enter a number: "))
rev = 0
while n != 0:
digit = n % 10
rev = rev * 10 + digit n = n // 10
print("Reversed number:", rev)

🔸 8. Check if a number is palindrome


n = int(input("Enter a number: "))
temp = n
rev = 0
while n > 0:
rev = rev * 10 + n % 10 n = n // 10
if temp == rev:
print("Palindrome")
else:
print("Not a palindrome")

🔸 9. Sum of digits of a number


n = int(input("Enter a number: "))
total = 0
while n > 0:
total += n % 10
n = n // 10
print("Sum of digits:", total)

🔸 10. Menu-driven program for calculator


print("1. Add\n2. Subtract\n3. Multiply\n4. Divide")
choice = int(input("Enter your choice: "))
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
if choice == 1:
print("Sum:", a + b)
elif choice == 2:
print("Difference:", a - b)
elif choice == 3:
print("Product:", a * b)
elif choice == 4:
if b != 0:
print("Quotient:", a / b)
else:
print("Cannot divide by zero")
else:
print("Invalid choice")

🔸 11. Display numbers from 1 to 100 using while loop


i = 1 while i <= 100: print(i, end=" ") i += 1

You might also like