NCERTCh 3Free

Brief Overview Chapter of Python

🎓 Class 11📖 Informatics Practices📖 14 notes🧠 15 Q&A⏱️ ~21 min

Brief Overview Chapter of PythonStudy Notes

NCERT-aligned · 14 notes · 3 shown free

3.1 INTRODUCTION TO PYTHON

Explanation

3.1 INTRODUCTION TO PYTHON

A program is an ordered set of instructions or commands that a computer executes to perform a specific task. These instructions are written in a programming language, which serves as a medium for humans to communicate with computers. Examples of programming languages include Python, C, C++, and Java. This chapter provides a brief overview of the Python programming language, which is widely popular and easy to learn. Python was created by Guido van Rossum in 1991. It is a versatile language used in various fields such as software development, web development, scientific computing, and big data analytics. Python's syntax emphasizes readability and simplicity, making it an excellent choice for beginners and professionals alike. The language enforces proper indentation as part of its syntax, which ensures that code is well-structured and easy to understand. This feature helps avoid common errors related to code blocks and enhances code clarity.

  • A program is a sequence of instructions executed by a computer.
  • Programming languages specify instructions to computers; Python is one such language.
  • Python was created by Guido van Rossum in 1991.
  • Python is popular due to its simplicity and wide applicability.
  • Indentation is a mandatory part of Python syntax, improving code readability.
  • Python is used in software, web development, scientific computing, and big data.
  • 📌 Program: An ordered set of instructions executed by a computer.
  • 📌 Programming Language: A language used to write programs, e.g., Python, C, Java.
  • 📌 Indentation: Leading whitespace in code used to define blocks in Python.

3.1.2 Execution Modes

Explanation

3.1.2 Execution Modes

Python programs can be executed in two primary modes: Interactive mode and Script mode. In Interactive mode, the user types Python statements directly at the Python prompt (>>>). Each statement is executed immediately, and the result is displayed. This mode is convenient for testing small snippets of code or experimenting with Python commands. However, the code typed in interactive mode is not saved automatically, so it must be retyped for future use. In Script mode, Python programs are written and saved in files with a .py extension, called scripts. These scripts can be created using any text editor, including Python's built-in editor called IDLE. After writing and saving the program, it can be executed by the Python interpreter. Script mode is suitable for larger programs that need to be saved, reused, and maintained. The IDLE editor provides an easy interface to write, save, and run Python scripts. To execute a script in IDLE, open the file and select Run > Run Module. The output of the program appears in the Python shell window. This mode supports debugging and editing, making it ideal for program development.

  • Interactive mode allows typing and executing Python statements one at a time.
  • Interactive mode is useful for quick testing but does not save code.
  • Script mode involves writing Python code in files with .py extension.
  • Scripts can be created using any editor; IDLE is Python's built-in editor.
  • Scripts can be saved and executed multiple times without retyping.
  • IDLE allows running scripts via Run > Run Module, showing output in shell.
  • 📌 Interactive Mode: Python execution mode where commands are entered and executed one at a time.
  • 📌 Script Mode: Python execution mode where programs are saved in files and executed as scripts.
  • 📌 IDLE: Integrated Development and Learning Environment, Python's built-in editor.

3.2 Python Keywords

Explanation

3.2 Python Keywords

Keywords in Python are reserved words that have special meaning to the interpreter. They form the core syntax of the language and cannot be used as identifiers (names for variables, functions, etc.). Python is case-sensitive, so keywords must be writ

Practice QuestionsBrief Overview Chapter of Python

Includes NCERT exercise questions with answers

Q1.1. Which of the following identifier names are invalid and why? a) Serial_no. b) 1st_Room c) Hundred$ d) Total Marks e) Total_Marks f) total-Marks g) Percentage h) True
A.a) Serial_no.
B.b) 1st_Room
C.c) Hundred$
D.d) Total Marks
E.e) Total_Marks
F.f) total-Marks
G.g) Percentage
H.h) True

Answer:

Invalid identifiers are: - Serial_no.: Invalid because it contains a dot (.) which is not allowed in identifiers. - 1st_Room: Invalid because identifiers cannot start with a digit. - Hundred$: Invalid because $ is not allowed in identifiers. - Total Marks: Invalid because spaces are not allowed in identifiers. - total-Marks: Invalid because hyphen (-) is not allowed in identifiers. - True: Invalid because it is a reserved keyword in Python. Valid identifiers are: - Total_Marks - Percentage

Explanation:

In Python, identifiers must start with a letter (A-Z or a-z) or underscore (_), followed by letters, digits (0-9), or underscores. They cannot contain spaces, dots, hyphens, or special characters like $. Also, keywords like True cannot be used as identifiers.

EasyNCERT
Q2.2. Write the corresponding Python assignment statements: a) Assign 10 to variable length and 20 to variable breadth. b) Assign the average of values of variables length and breadth to a variable sum. c) Assign a list containing strings 'Paper', 'Gel Pen', and 'Eraser' to a variable stationery. d) Assign the strings 'Mohandas', 'Karamchand', and 'Gandhi' to variables first, middle and last. e) Assign the concatenated value of string variables first, middle and last to variable fullname. Make sure to incorporate blank spaces appropriately between different parts of names.

Answer:

a) length = 10 breadth = 20 b) sum = (length + breadth) / 2 c) stationery = ['Paper', 'Gel Pen', 'Eraser'] d) first = 'Mohandas' middle = 'Karamchand' last = 'Gandhi' e) fullname = first + ' ' + middle + ' ' + last

Explanation:

Assignment statements in Python use the '=' operator. For average, sum of length and breadth is divided by 2. Lists are defined using square brackets []. String concatenation uses '+' operator and spaces are added as ' '.

EasyNCERT
Q3.3. Which data type will be used to represent the following data values and why? a) Number of months in a year b) Resident of Delhi or not c) Mobile number d) Pocket money e) Volume of a sphere f) Perimeter of a square g) Name of the student h) Address of the student

Answer:

a) Integer (int) - because number of months is a whole number. b) Boolean (bool) - because it is a yes/no (True/False) condition. c) String (str) - because mobile numbers may start with zero and are not used for calculations. d) Float (float) - because pocket money can have decimal values. e) Float (float) - volume can be a decimal value. f) Float (float) - perimeter can be decimal if side length is decimal. g) String (str) - names are textual data. h) String (str) - address is textual data.

Explanation:

Data types are chosen based on the nature of data. Whole numbers use int, true/false conditions use bool, textual data use str, and decimal numbers use float.

EasyNCERT
Q4.4. Give the output of the following when num1 = 4, num2 = 3, num3 = 2 a) num1 += num2 + num3 b) print (num1) c) num1 = num1 ** (num2 + num3) d) print (num1) e) num1 **= num2 + c f) num1 = '5' + '5' g) print(num1) h) print(4.00/(2.0+2.0)) i) num1 = 2+9*((3*12)-8)/10 j) print(num1) k) num1 = float(10) l) print (num1) m) num1 = int('3.14') n) print (num1) o) print(10 != 9 and 20 >= 20) p) print(5 % 10 + 10 < 50 and 29 <= 29)

Answer:

Given num1=4, num2=3, num3=2: a) num1 += num2 + num3 => num1 = 4 + 3 + 2 = 9 b) print(num1) => 9 c) num1 = num1 ** (num2 + num3) => num1 = 9 ** (3 + 2) = 9 ** 5 = 59049 d) print(num1) => 59049 e) num1 **= num2 + c => Error: 'c' is undefined, causes runtime error f) num1 = '5' + '5' => '55' g) print(num1) => 55 h) print(4.00/(2.0+2.0)) => 4.00/4.0 = 1.0 i) num1 = 2+9*((3*12)-8)/10 => 2 + 9*(36-8)/10 = 2 + 9*28/10 = 2 + 252/10 = 2 + 25.2 = 27.2 j) print(num1) => 27.2 k) num1 = float(10) => 10.0 l) print(num1) => 10.0 m) num1 = int('3.14') => Error: invalid literal for int() with base 10 n) print(num1) => Not reached due to error in m o) print(10 != 9 and 20 >= 20) => True and True = True p) print(5 % 10 + 10 < 50 and 29 <= 29) => (5 + 10) < 50 and True => 15 < 50 and True => True and True = True

Explanation:

Step-by-step evaluation: - += adds sum of num2 and num3 to num1. - ** is exponentiation. - String concatenation with '+' joins strings. - Division with floats returns float. - int('3.14') causes ValueError because '3.14' is not an integer string. - Logical operators and comparison operators return boolean values.

MediumNCERT
Q5.5. Categorise the following as syntax error, logical error or runtime error: a) 25 / 0 b) num1 = 25; num2 = 0; num1 / num2

Answer:

a) 25 / 0 => Runtime error (ZeroDivisionError) because division by zero is undefined. b) num1 = 25; num2 = 0; num1 / num2 => Runtime error (ZeroDivisionError) for same reason.

Explanation:

Syntax errors occur due to incorrect code structure, logical errors occur when code runs but produces wrong results, runtime errors occur during execution like division by zero.

EasyNCERT
Q6.6. Write a Python program to calculate the amount payable if money has been lent on simple interest. Principal or money lent = P, Rate = R% per annum and Time = T years. Then Simple Interest (SI) = (P x R x T) / 100. Amount payable = Principal + SI. P, R and T are given as input to the program.

Answer:

Python program: P = float(input('Enter principal amount: ')) R = float(input('Enter rate of interest: ')) T = float(input('Enter time in years: ')) SI = (P * R * T) / 100 Amount_payable = P + SI print('Simple Interest:', SI) print('Amount payable:', Amount_payable)

Explanation:

Inputs are taken as float to allow decimal values. Simple Interest formula is applied and amount payable is sum of principal and interest. Outputs are printed.

EasyNCERT
Q7.7. Write a program to repeat the string "GOOD MORNING" n times. Here n is an integer entered by the user.

Answer:

Python program: n = int(input('Enter number of times to repeat: ')) print('GOOD MORNING ' * n)

Explanation:

The string is multiplied by integer n to repeat it n times. Input is converted to int.

EasyNCERT
Q8.8. Write a program to find the average of 3 numbers.

Answer:

Python program: num1 = float(input('Enter first number: ')) num2 = float(input('Enter second number: ')) num3 = float(input('Enter third number: ')) average = (num1 + num2 + num3) / 3 print('Average:', average)

Explanation:

Inputs are taken as float to allow decimal numbers. Average is sum divided by 3.

EasyNCERT