NCERTCh 5Free

Getting Started with Python

🎓 Class 11📖 Computer Science📖 15 notes🧠 15 Q&A⏱️ ~23 min

Getting Started with PythonStudy Notes

NCERT-aligned · 15 notes · 3 shown free

5.1 INTRODUCTION TO PYTHON

Explanation

5.1 INTRODUCTION TO PYTHON

In this section, we begin by understanding the concept of a programming language and its role in instructing computers. A program is defined as an ordered set of instructions executed by a computer to perform a specific task. The language used to write these instructions is called a programming language. Computers inherently understand only machine language, which consists of binary digits (0s and 1s). However, writing programs directly in machine language is cumbersome and error-prone for humans. This difficulty led to the development of high-level programming languages such as Python, C++, Java, and others, which are more readable and manageable by humans. A program written in a high-level language is called source code. To execute this source code, it must be translated into machine language. This translation is done by language translators like compilers and interpreters. Python uses an interpreter, which processes the program statements one by one, translating and executing them sequentially. If an error is encountered, execution stops immediately. In contrast, a compiler translates the entire source code into object code before execution, reporting all errors after scanning the whole program. Python is a high-level, interpreted programming language known for its simplicity and readability. It was created by Guido van Rossum and first released in 1991. The language emphasizes code readability with its clear syntax and indentation rules. Python supports multiple programming paradigms including procedural, object-oriented, and functional programming. The section also includes a quote by Donald Knuth highlighting programming as an art that requires skill and ingenuity, producing objects of beauty. This sets the tone for appreciating programming beyond mere coding. Overall, this section lays the foundation for learning Python by explaining what programming languages are, the need for high-level languages, and Python's role as an interpreted language.

  • A program is an ordered set of instructions for a computer to perform a task.
  • Programming languages specify these instructions in a human-readable form.
  • Machine language (0s and 1s) is difficult for humans to write and understand.
  • High-level languages like Python are easier for humans but require translation.
  • Python uses an interpreter to translate and execute code line by line.
  • Interpreters stop execution upon encountering errors; compilers translate whole code before execution.
  • 📌 Program: Ordered set of instructions executed by a computer.
  • 📌 Programming Language: Language used to write programs.
  • 📌 Source Code: Program written in a high-level language.

5.1.1 Features of Python

Explanation

5.1.1 Features of Python

This section elaborates on the distinctive features that make Python a popular programming language. Python is a high-level language, meaning it abstracts away most hardware details, allowing programmers to focus on problem-solving rather than machine specifics. It is free and open source, enabling anyone to use and contribute to its development. Python is an interpreted language, so programs are executed by an interpreter which reads and executes code line by line. This facilitates easier debugging and interactive coding. The language has a clearly defined syntax and simple structure, making Python programs easy to read and understand. Python is case-sensitive; for example, variables named NUMBER and number are considered different. It is portable and platform-independent, meaning Python programs can run on various operating systems and hardware without modification. Python boasts a rich library of predefined functions and modules, supporting a wide range of applications from web development to scientific computing. Indentation is a unique feature in Python, used to define blocks of code and nested structures instead of braces or keywords. These features collectively make Python beginner-friendly and versatile for various programming tasks.

  • Python is a high-level, free, and open source language.
  • It is interpreted, executing code line by line.
  • Python syntax is clear and programs are easy to understand.
  • It is case-sensitive; variable names differ by case.
  • Python is portable and platform independent.
  • Uses indentation to define code blocks instead of braces.
  • 📌 High-level language: Abstracts hardware details for ease of programming.
  • 📌 Interpreted language: Executes code line by line.
  • 📌 Case-sensitive: Differentiates between uppercase and lowercase letters.

5.1.3 Execution Modes

Explanation

5.1.3 Execution Modes

Python supports two modes of execution: Interactive mode and Script mode. In Interactive mode, the Python interpreter displays a prompt symbol >>> where users can type individual Python statements. Each statement is executed immediately after pressi

Practice QuestionsGetting Started with Python

Includes NCERT exercise questions with answers

Q1.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 causes a runtime error (ZeroDivisionError) because division by zero is undefined. b) num1 = 25; num2 = 0; num1/num2 also causes a runtime error (ZeroDivisionError) for the same reason.

Explanation:

Both expressions attempt division by zero, which is not allowed during program execution, causing runtime errors. There is no syntax error or logical error here.

EasyNCERT
Q2.A dartboard of radius 10 units and the wall it is hanging on are represented using a two-dimensional coordinate system, with the board’s center at coordinate (0,0). Variables x and y store the x-coordinate and the y-coordinate of a dart that hits the dartboard. Write a Python expression using variables x and y that evaluates to if the dart hits (is within) the dartboard, and then evaluate the expression for these dart coordinates: a) (0,0) b) (10,10) c) (6, 6) d) (7,8)

Answer:

The dart hits the dartboard if the distance from the center (0,0) to the point (x,y) is less than or equal to 10 units. The condition is: x**2 + y**2 <= 10**2 Evaluations: a) (0,0): 0**2 + 0**2 = 0 <= 100 → True b) (10,10): 10**2 + 10**2 = 100 + 100 = 200 > 100 → False c) (6,6): 6**2 + 6**2 = 36 + 36 = 72 <= 100 → True d) (7,8): 7**2 + 8**2 = 49 + 64 = 113 > 100 → False

Explanation:

The dartboard is a circle with radius 10 centered at (0,0). The dart hits inside the board if the point lies within or on the circle boundary, which means the sum of squares of x and y coordinates is less than or equal to 100.

MediumNCERT
Q3.Write a Python program to convert temperature in degree Celsius to degree Fahrenheit. If water boils at 100 degree C and freezes as 0 degree C, use the program to find out what is the boiling point and freezing point of water on the Fahrenheit scale. (Hint: T(°F) = T(°C) × 9/5 + 32)

Answer:

Program: celsius = float(input("Enter temperature in Celsius: ")) fahrenheit = (celsius * 9/5) + 32 print(f"Temperature in Fahrenheit: {fahrenheit}") # Using the program for boiling point celsius = 100 fahrenheit = (celsius * 9/5) + 32 print(f"Boiling point of water in Fahrenheit: {fahrenheit}") # Using the program for freezing point celsius = 0 fahrenheit = (celsius * 9/5) + 32 print(f"Freezing point of water in Fahrenheit: {fahrenheit}")

Explanation:

The formula to convert Celsius to Fahrenheit is T(°F) = T(°C) × 9/5 + 32. For 100°C, Fahrenheit = 100 × 9/5 + 32 = 212°F. For 0°C, Fahrenheit = 0 × 9/5 + 32 = 32°F.

EasyNCERT
Q4.Write a Python program to calculate the amount payable if money has been lent on simple interest. Principal or money lent = P, Rate of interest = 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:

Program: P = float(input("Enter principal amount: ")) R = float(input("Enter rate of interest per annum: ")) T = float(input("Enter time in years: ")) SI = (P * R * T) / 100 Amount_payable = P + SI print(f"Simple Interest: {SI}") print(f"Amount payable: {Amount_payable}")

Explanation:

Simple Interest is calculated using the formula SI = (P × R × T)/100. The total amount payable is the sum of principal and simple interest.

EasyNCERT
Q5.Write a program to calculate in how many days a work will be completed by three persons A, B and C together. A, B, C take x days, y days and z days respectively to do the job alone. The formula to calculate the number of days if they work together is xyz/(xy + yz + xz) days where x, y, and z are given as input to the program.

Answer:

Program: x = float(input("Enter days taken by A alone: ")) y = float(input("Enter days taken by B alone: ")) z = float(input("Enter days taken by C alone: ")) days_together = (x * y * z) / (x * y + y * z + x * z) print(f"Number of days to complete work together: {days_together}")

Explanation:

The formula xyz/(xy + yz + xz) calculates the combined work rate of three persons working together and gives the total days required.

MediumNCERT
Q6.Write a program to enter two integers and perform all arithmetic operations on them.

Answer:

Program: num1 = int(input("Enter first integer: ")) num2 = int(input("Enter second integer: ")) print(f"Addition: {num1 + num2}") print(f"Subtraction: {num1 - num2}") print(f"Multiplication: {num1 * num2}") print(f"Division: {num1 / num2}") print(f"Floor Division: {num1 // num2}") print(f"Modulus: {num1 % num2}") print(f"Exponentiation: {num1 ** num2}")

Explanation:

The program takes two integers as input and performs addition, subtraction, multiplication, division, floor division, modulus, and exponentiation operations.

EasyNCERT
Q7.Write a program to swap two numbers using a third variable.

Answer:

Program: a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) # Swapping using third variable c = a a = b b = c print(f"After swapping: a = {a}, b = {b}")

Explanation:

The program uses a temporary variable 'c' to hold the value of 'a' while assigning 'b' to 'a' and then 'c' to 'b', effectively swapping the values.

EasyNCERT
Q8.Write a program to swap two numbers without using a third variable.

Answer:

Program: a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) # Swapping without third variable a = a + b b = a - b a = a - b print(f"After swapping: a = {a}, b = {b}")

Explanation:

The program swaps values by arithmetic operations without using a temporary variable: sum is stored in 'a', then 'b' is updated by subtracting new 'b' from 'a', and finally 'a' is updated by subtracting new 'b' from 'a'.

MediumNCERT