Computer Science · Class 11 Optional
Ch 5Getting Started with Python — Class 11 Computer Science, concept-first.
A computer can only act on instructions it is given, so before a computer solves a problem for us, we must spell out the solution as a precise sequence of instructions.
Key concepts
Hover a concept to preview it and jump to its most relevant Q&A.
Variable Naming Convention
Imagine you walk into a messy kitchen. There are three identical unlabeled jars on the counter. One contains salt, one sugar, one baking soda. You need salt for your recipe.
Most relevant Q&A
Chapter contents
The NCERT structure, section by section. Open a section to see its questions, then read the concept-first solution.
Introduction to Python
A computer can only act on instructions it is given, so before a computer solves a problem for us, we must spell out the solution as a precise sequence of instructions.
Features of Python
Python's popularity as a first programming language comes from a specific set of features, each of which shapes how we write and run programs in it.
Working with Python
To write and execute a Python program, one thing is essential: a Python interpreter. There are two ways to get one —
Execution Modes
The Python interpreter can be used in two ways:
Python Keywords
Some words in Python are not available for us to use as names — the language has already claimed them.
Identifiers
In programming languages, identifiers are the names used to identify a variable, a function, or any other entity in a program. Choosing them is not free-form — Python lays down precise rules.
Variables
2 QA variable in a program is uniquely identified by a name — an identifier. In Python, a variable refers to an object: an item or element stored in the memory.
Comments
2 QComments are remarks or notes added inside source code. The interpreter does not execute comments — they exist purely for the humans reading the program.
Everything is an Object
Python treats every value or data item as an object — whether it is numeric, a string, or of any other type (the types themselves are discussed in the next section).
Data Types
Every value in Python belongs to a specific data type. A data type does two jobs at once: it identifies what kind of data values a variable can hold, and it determines which operations can be performe…
Number
The Number data type stores numerical values only. It is further classified into three different types — int, float and complex.
Sequence
2 QA Python sequence is an ordered collection of items, where each item is indexed by an integer — the position of every element is fixed and numbered.
+−Worked Examples1 question
Set
A set is an unordered collection of items, separated by commas and enclosed in curly brackets . At first glance a set looks similar to a list — both hold multiple comma-separated items — but two pr…
None
None is a special data type with a single value. It is used to signify the absence of value in a situation — a variable that exists but deliberately holds "nothing yet" can be given the value None.
Mapping
Mapping is an unordered data type in Python. Where a sequence looks items up by their integer position, a mapping looks values up by a key.
Mutable and Immutable Data Types
Programs often need to change or update the values of certain variables as they run. For some data types Python allows this freely — but for others, once a variable of that type has been created and a…
Deciding Usage of Python Data Types
Python offers four collection types — list, tuple, set, dictionary — and each has a situation in which it is the preferred choice.
Operators
An operator is a symbol that tells Python to carry out a specific mathematical or logical operation on one or more values. The values an operator acts upon are called operands.
Arithmetic Operators
Arithmetic operators let a Python program do calculation. Python supports the four basic arithmetic operations — addition, subtraction, multiplication and division — and adds three more: modulus (rema…
Relational Operators
A relational operator compares the values of the operands on either side of it and works out the relationship between them.
Assignment Operators
An assignment operator assigns a value to — or changes the value of — the variable on its left-hand side.
Logical Operators
Python supports three logical operators: and, or and not. They must be written in lower case only — AND or Or will not work, because Python is case-sensitive and its keywords are fixed.
Identity Operators
Identity operators answer a different question from the equality operator ==. Instead of asking "do these two values look the same?", they ask "is the value of a variable of a certain type?" and, more…
Membership Operators
Membership operators are used to check whether a value is a member of a given sequence or not. Instead of comparing two values, they search: "does this item occur anywhere inside that collection?" The…
Expressions
An expression is a combination of constants, variables and operators. The defining property of an expression is that it always evaluates to a value — Python works it out and produces a single result.
Precedence of Operators
4 QWhen an expression contains different kinds of operators, Python needs a rule to decide which operator to apply first.
+−Worked Examples1 question
+−Worked Examples1 question
+−Worked Examples1 question
Statement
In Python, a statement is a unit of code that the Python interpreter can execute. Where an expression evaluates to a value, a statement is an instruction — something the interpreter carries out.
Input and Output
3 QA program often needs to interact with its user — to collect some input data from the end user, process it, and present the desired output.
+−Worked Examples1 question
+−Worked Examples1 question
Type Conversion
Sometimes the data type a value arrives in is not the type our calculation needs. Type conversion is the changing of a variable's data type from one type to another — and a small program shows exactly…
Explicit Conversion
5 QExplicit conversion, also called type casting, happens when data type conversion takes place because the programmer forced it in the program. The general form of an explicit data type conversion is:
+−Programs1 question
+−Programs1 question
+−Programs1 question
+−Programs1 question
+−Programs1 question
Implicit Conversion
Implicit conversion, also known as coercion, happens when data type conversion is done automatically by Python, without any instruction from the programmer.
Debugging
A programmer can make mistakes while writing a program — and because of those mistakes the program may fail to execute at all, or may run but produce wrong output.
Syntax Errors
Like every programming language, Python has its own rules that determine its syntax — the grammar its statements must follow.
Logical Errors
A logical error is a bug in the program that causes it to behave incorrectly — the program runs, but does the wrong thing.
Runtime Error
A runtime error causes abnormal termination of the program while it is executing. It arises when a statement is syntactically correct, but the interpreter still cannot execute it.
Summary
- Python is an open-source, high-level, interpreter-based language usable for a multitude of scientific and non-scientific computing purposes.
Exercises
+−Show 21 questionsHide questions21 questions
- Q1Which of the following identifier names are invalid and why? i. Serial_no. ii. 1st_Room iii. Hundred\$ iv. Total Marks v. Total_Marks vi. to…Free
- Q2Write the corresponding Python assignment statements: a) Assign 10 to variable length and 20 to variable breadth. b) Assign the average of v…Free
- Q3Write logical expressions corresponding to the following statements in Python and evaluate the expressions (assuming variables num1, num2, n…Free
- Q4Add a pair of parentheses to each expression so that it evaluates to True. a) 0 == 1 == 2 b) 2 + 3 == 4 + 5 == 7 c) 1 < -1 == 3 > 4Preview
- Q5Write the output of the following: a) num1 = 4 num2 = num1 + 1 num1 = 2 print (num1, num2) b) num1, num2 = 2, 6 num1, num2 = num2, num1 + 2…Preview
- Q6Which 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) Mo…Preview
- Q7Give the output of the following when num1 = 4, num2 = 3, num3 = 2 a) num1 += num2 + num3 print (num1) b) num1 = num1 ** (num2 + num3) print…Preview
- Q8Categorise the following as syntax error, logical error or runtime error: a) 25 / 0 b) num1 = 25; num2 = 0; num1/num2Preview
- Q9A dartboard of radius 10 units and the wall it is hanging on are represented using a two-dimensional coordinate system, with the board’s cen…Preview
- Q10Write a Python program to convert temperature in degree Celsius to degree Fahrenheit. If water boils at 100 degree C and freezes as 0 degree…Preview
- Q11Write a Python program to calculate the amount payable if money has been lent on simple interest. Principal or money lent = P, Rate of inter…Preview
- Q12Write 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…Preview
- Q13Write a program to enter two integers and perform all arithmetic operations on them.Preview
- Q14Write a program to swap two numbers using a third variable.Preview
- Q15Write a program to swap two numbers without using a third variable.Preview
- Q16Write a program to repeat the string ‘‘GOOD MORNING” n times. Here ‘n’ is an integer entered by the user.Preview
- Q17Write a program to find average of three numbers.Preview
- Q18The volume of a sphere with radius r is 4/3πr³. Write a Python program to find the volume of spheres with radius 7cm, 12cm, 16cm, respective…Preview
- Q19Write a program that asks the user to enter their name and age. Print a message addressed to the user that tells the user the year in which…Preview
- Q20The formula E = mc² states that the equivalent energy (E) can be calculated as the mass (m) multiplied by the speed of light (c = about 3×10…Preview
- Q21Presume that a ladder is put upright against a wall. Let variables length and angle store the length of the ladder and the angle that it for…Preview